Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] Simple HTML DOM i wysyłanie maila
cox
post 29.01.2013, 19:25:53
Post #1





Grupa: Zarejestrowani
Postów: 9
Pomógł: 0
Dołączył: 29.10.2005

Ostrzeżenie: (0%)
-----


Witam,
niestety skrypt w którym pomógł mi ostatnio Kofel (w tym temacie http://forum.php.pl/index.php?showtopic=211830) nie spełnia swego zadania gdyż kanał rss z którego były pobierane informacje miał duże opóźnienie nawet po 2-3 godziny co jest dla mnie nie do przyjęcia. Na internecie znalazłem taki skrypt:
  1. <?php
  2. // Pull in PHP Simple HTML DOM Parser
  3. include("simplehtmldom/simple_html_dom.php");
  4.  
  5. // Settings on top
  6. $sitesToCheck = array(
  7. // id is the page ID for selector
  8. array("url" => "http://www.stronadosubskrypcji.com", "selector" => ".header")
  9. );
  10. $savePath = "cachedPages/";
  11. $emailContent = "";
  12.  
  13. // For every page to check...
  14. foreach($sitesToCheck as $site) {
  15. $url = $site["url"];
  16.  
  17. // Calculate the cachedPage name, set oldContent = "";
  18. $fileName = md5($url);
  19. $oldContent = "";
  20.  
  21. // Get the URL's current page content
  22. $html = file_get_html($url);
  23.  
  24. // Find content by querying with a selector, just like a selector engine!
  25. foreach($html->find($site["selector"]) as $element) {
  26. $currentContent = $element->plaintext;
  27. }
  28.  
  29. // If a cached file exists
  30. if(file_exists($savePath.$fileName)) {
  31. // Retrieve the old content
  32. $oldContent = file_get_contents($savePath.$fileName);
  33. }
  34.  
  35. // If different, notify!
  36. if($oldContent && $currentContent != $oldContent) {
  37. // Here's where we can do a whoooooooooooooole lotta stuff
  38. // We could tweet to an address
  39. // We can send a simple email
  40. // We can text ourselves
  41.  
  42. // Build simple email content
  43. $emailContent = "David, the following page has changed!\n\n".$url."\n\n";
  44. }
  45.  
  46. // Save new content
  47. file_put_contents($savePath.$fileName,$currentContent);
  48. }
  49.  
  50. // Send the email if there's content!
  51. if($emailContent) {
  52. // Sendmail!
  53. mail("mail@mailnajakimawyslac.com","Sites Have Changed!",$emailContent,"From: alerts@davidwalsh.name","\r\n");
  54. // Debug
  55. echo $emailContent;
  56. }
  57. ?>


Lecz też nie funkcjonuje. Każdy nagłówek newsa w kodzie jest w
  1. <SPAN CLASS="header">
  2.  
  3. Jakiś news</span>
. Na stronie z której pobieram informacje jest 10 newsów lecz otwierając plik w katalogu cachedPages znajduje się tylko nagłówek najstarszego, jednego news'a. Nawet ten jeden nagłówek nie wysyła na maila. W czym problem ?
Z góry dziękuję za pomoc.

Ten post edytował cox 29.01.2013, 19:27:09
Go to the top of the page
+Quote Post
nospor
post 30.01.2013, 09:45:54
Post #2





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Cytat
Na stronie z której pobieram informacje jest 10 newsów lecz otwierając plik w katalogu cachedPages znajduje się tylko nagłówek najstarszego, jednego news'a
Ponieważ kod co tu nam pokazałeś, trakuje daną stronę jako jedną całość. Nie rozróżnia, że na niej jest 10 newsów. Ten kod zwraca ze strony tylko ostatni selektor - w twoim wypadku ostatni news.


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
cox
post 30.01.2013, 18:21:52
Post #3





Grupa: Zarejestrowani
Postów: 9
Pomógł: 0
Dołączył: 29.10.2005

Ostrzeżenie: (0%)
-----


Kurcze w takim razie jak zrobić by wyświetlał pierwszy.
Próbowałem coś takiego
  1. <?php
  2. // Pull in PHP Simple HTML DOM Parser
  3. include("simplehtmldom/simple_html_dom.php");
  4.  
  5. // Settings on top
  6. $sitesToCheck = array(
  7. // id is the page ID for selector
  8. array("url" => "http://newway.blogabet.com", "selector" => "span.betHeader")
  9. );
  10. $savePath = "cachedPages/";
  11. $emailContent = "";
  12.  
  13. // For every page to check...
  14. foreach($sitesToCheck as $site) {
  15. $url = $site["url"];
  16.  
  17. // Calculate the cachedPage name, set oldContent = "";
  18. $fileName = md5($url);
  19. $oldContent = "";
  20.  
  21. // Get the URL's current page content
  22. $html = file_get_html($url);
  23.  
  24. // Find content by querying with a selector, just like a selector engine!
  25. foreach($html->getElementsById($site["selector"]) as $element) {
  26. $currentContent = $element->item($element->length - 1);
  27. $currentContent = $element->plaintext;
  28.  
  29. }
  30.  
  31. // If a cached file exists
  32. if(file_exists($savePath.$fileName)) {
  33. // Retrieve the old content
  34. $oldContent = file_get_contents($savePath.$fileName);
  35. }
  36.  
  37. // If different, notify!
  38. if($oldContent && $currentContent != $oldContent) {
  39. // Here's where we can do a whoooooooooooooole lotta stuff
  40. // We could tweet to an address
  41. // We can send a simple email
  42. // We can text ourselves
  43.  
  44. // Build simple email content
  45. $emailContent = "Daro, jest nowy typ\n\n".$url."\n\n";
  46. }
  47.  
  48. // Save new content
  49. file_put_contents($savePath.$fileName,$currentContent);
  50. }
  51.  
  52. // Send the email if there's content!
  53. if($emailContent) {
  54. // Sendmail!
  55. mail("daro_dar@ymail.com","Nowy typ",$emailContent,"From: typy@aktualizacja.pl","\r\n");
  56. // Debug
  57. echo $emailContent;
  58. }
  59. ?>


Lecz nic to nie dało. Możesz bardziej naprowadzić ?
Go to the top of the page
+Quote Post
nospor
post 31.01.2013, 09:49:05
Post #4





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




  1. foreach($html->find($site["selector"]) as $element) {
  2.  
  3. $currentContent = $element->plaintext;
  4.  
  5. }

Ten kawałek leci po wszystkich newsach. Skoro chcesz miec tylko pierwszy, to skoncz pętle od razu po rozpoczęciu
  1. foreach($html->find($site["selector"]) as $element) {
  2.  
  3. $currentContent = $element->plaintext;
  4. break; //TADA
  5. }

i po sprawie


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
cox
post 31.01.2013, 19:10:38
Post #5





Grupa: Zarejestrowani
Postów: 9
Pomógł: 0
Dołączył: 29.10.2005

Ostrzeżenie: (0%)
-----


No tak sciana.gif

Wielkie dzięki za pomoc.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.06.2025 - 18:30