Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Simple HTML DOM i wysyłanie maila
Forum PHP.pl > Forum > Przedszkole
cox
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.
nospor
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.
cox
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ć ?
nospor
  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
cox
No tak sciana.gif

Wielkie dzięki za pomoc.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.