1

I'm using xPath to scrape tour dates from another website (with permission of course). Since it updates on every page load, I thought about using transients to store the data.

Unfortunately I have no experience using transients and don't get it to work. This is my code:

<?php
  $html = file_get_contents('http://www.example.com');       
  $doc = new DOMDocument();

  libxml_use_internal_errors(TRUE); // disable libxml errors

  if(!empty($html)) {

    $doc->loadHTML($html);
    libxml_clear_errors();

    $xpath = new DOMXPath($doc);

    // Get only the content needed
    $termine = $xpath->query('//ul[@class="artistEvents"]/li');            

    if ($termine->length > 0) {
      foreach ($termine as $termin) { 

        $date = $xpath->query("div[@class='left']/strong", $termin);
        $location = $xpath->query("div[contains(@class,'right')]", $termin);

        echo '<tr>';

        // Date     
        if ($date->length > 0) {
          $date = substr($date->item(0)->nodeValue, 3, 10);
          $date = strftime("%d.%m.%Y", strtotime($date));
          echo '<td class="live-date">' . $date . '</td>';
        }

        // Location
        if ($location->length > 0) {
          $location = substr($location->item(0)->nodeValue, 14);
          $location = utf8_decode($location);
          echo '<td class="live-location">' . $location . '</td>';
        }

        echo '</tr>';
      }
    }

    else {
      echo '<p>No dates available.</p>';
    }

  }
?>

Any help with how to use transients to store this query is greatly appreciated! Also, I've never used xPath before, so if my code needs any improvement (although it works) I'll be happy to know about it.

Thanks so much!

6
  • Since you have permission, doesn't this "other site" have a better way for you to get content than scraping the site?
    – s_ha_dum
    Commented Apr 10, 2015 at 22:14
  • I wish they would, but the answer is no. They have no RSS feed or the like for the dates and are not planning to implement it.
    – Stefan
    Commented Apr 10, 2015 at 22:29
  • ouch! That is rough. Exactly where does your code fail?
    – s_ha_dum
    Commented Apr 10, 2015 at 22:44
  • It doesn't really fail, the code works. I just don't know how to modify it to store the scraped data using transients, so the data doesn't update on every page load.
    – Stefan
    Commented Apr 10, 2015 at 23:17
  • I don't see any transient code at all?
    – s_ha_dum
    Commented Apr 10, 2015 at 23:19

1 Answer 1

3

Try something like this, which would save in transient for 12 hours. Let me know if anything does not make sense.

<?php
$value = get_transient( 'value' );
if ( false === $value ) {
  $output = "";
  $html = file_get_contents('http://www.example.com');       
  $doc = new DOMDocument();

  libxml_use_internal_errors(TRUE); // disable libxml errors

  if(!empty($html)) {

    $doc->loadHTML($html);
    libxml_clear_errors();

    $xpath = new DOMXPath($doc);

    // Get only the content needed
    $termine = $xpath->query('//ul[@class="artistEvents"]/li');            

    if ($termine->length > 0) {
      foreach ($termine as $termin) { 

        $date = $xpath->query("div[@class='left']/strong", $termin);
        $location = $xpath->query("div[contains(@class,'right')]", $termin);

        $output .= '<tr>';

        // Date     
        if ($date->length > 0) {
          $date = substr($date->item(0)->nodeValue, 3, 10);
          $date = strftime("%d.%m.%Y", strtotime($date));
          $output .= '<td class="live-date">' . $date . '</td>';
        }

        // Location
        if ($location->length > 0) {
          $location = substr($location->item(0)->nodeValue, 14);
          $location = utf8_decode($location);
          $output .= '<td class="live-location">' . $location . '</td>';
        }

        $output .= '</tr>';
      }
    }
    else {
      $output .= '<p>No dates available.</p>';
    }
  }
  $value = $output;
  set_transient( 'value', $value, 12 * HOUR_IN_SECONDS );

}
echo $value;
?>
4
  • This works perfectly, thank you so much for your help!
    – Stefan
    Commented Apr 11, 2015 at 12:31
  • One last question: Is it possible to modify the output of the transient once it's stored? This example doesn't seem to work: $value = str_replace("Text1", "Text2", $value); echo $value;
    – Stefan
    Commented Apr 11, 2015 at 16:53
  • That should work. Don't see any reason why it wouldn't Commented Apr 13, 2015 at 17:11
  • I don't know why it didn't work in the first place but now it does. Thanks again, I really appreciate your help!
    – Stefan
    Commented Apr 13, 2015 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.