Caching an XML file - Does this PHP cod look okay?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lezinterracial
    Confirmed User
    • Jul 2012
    • 3117

    #1

    Tech Caching an XML file - Does this PHP cod look okay?

    I want to use Chaturbate's XML feed, But I want to save the XML file off periodically and use that instead of hitting Chaturbate's XML file every time. This code seems to work, But do you see any problems or ways to improve?

    Code:
    $feed_updated = filemtime('mysaved.xml');
    $current_time = time();
    if($current_time - $feed_updated >= 300) {
            $xml_feed_url = 'https://chaturbate.com/affiliates/api/onlinerooms/?format=xml&wm=0CLZb';
            $savefile = "yes";
    } else {
            $xml_feed_url = 'http://www.bestfreecamgirls.com/mysaved.xml';
            $savefile = "no";
            echo " testing - using saved";
    }
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $xml_feed_url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_TIMEOUT,5);
    
    
    $xml = curl_exec($ch);
    if ($savefile == "yes"){
        file_put_contents('mysaved.xml', $xml);}
    
    curl_close($ch);
    Live Sex Shows
  • EddyTheDog
    Just Doing My Own Thing
    • Jan 2011
    • 25433

    #2
    I posted something similar a while ago - I will see if I can find it...

    Comment

    • EddyTheDog
      Just Doing My Own Thing
      • Jan 2011
      • 25433

      #3
      This was my solution:

      Code:
      <?php 
      
      $cacheName = 'chatcache.xml'; 
      
      $ageInSeconds = 60; 
        
      if(!file_exists($cacheName) || filemtime($cacheName) > time() + $ageInSeconds);  
      { 
        $contents = file_get_contents('http://chaturbate.com/affiliates/api/onlinerooms/?format=xml&wm=isCir'); 
        file_put_contents($cacheName, $contents); 
      } 
      
      ?>

      Comment

      • lezinterracial
        Confirmed User
        • Jul 2012
        • 3117

        #4
        Originally posted by EddyTheDog
        This was my solution:

        Code:
        <?php 
        
        $cacheName = 'chatcache.xml'; 
        
        $ageInSeconds = 60; 
          
        if(!file_exists($cacheName) || filemtime($cacheName) > time() + $ageInSeconds);  
        { 
          $contents = file_get_contents('http://chaturbate.com/affiliates/api/onlinerooms/?format=xml&wm=isCir'); 
          file_put_contents($cacheName, $contents); 
        } 
        
        ?>
        I like that much better. The file_exists is needed or I end up defaulting to the iframe on my landing page.

        I was using Iframe's showing the top room, but I am targeting heteros, And those Voyeur_Boys are getting popular.
        Live Sex Shows

        Comment

        • sarettah
          see you later, I'm gone
          • Oct 2002
          • 14295

          #5
          Originally posted by lezinterracial
          I want to use Chaturbate's XML feed, But I want to save the XML file off periodically and use that instead of hitting Chaturbate's XML file every time. This code seems to work, But do you see any problems or ways to improve?

          Code:
          $feed_updated = filemtime('mysaved.xml');
          $current_time = time();
          if($current_time - $feed_updated >= 300) {
                  $xml_feed_url = 'https://chaturbate.com/affiliates/api/onlinerooms/?format=xml&wm=0CLZb';
                  $savefile = "yes";
          } else {
                  $xml_feed_url = 'http://www.bestfreecamgirls.com/mysaved.xml';
                  $savefile = "no";
                  echo " testing - using saved";
          }
          
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $xml_feed_url);
          curl_setopt($ch, CURLOPT_HEADER, false);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch,CURLOPT_TIMEOUT,5);
          
          
          $xml = curl_exec($ch);
          if ($savefile == "yes"){
              file_put_contents('mysaved.xml', $xml);}
          
          curl_close($ch);

          Why do the page pull (curl) each time? You have the feed saved as mysaved.xml every 5 minutes. Just use that file as the feed in the program and don't do the curl every time.

          Code:
          $feed_file='mysaved.xml';
          $xml_feed_url = 'https://chaturbate.com/affiliates/api/onlinerooms/?format=xml&wm=0CLZb';
          $feed_updated = filemtime('mysaved.xml');
          $current_time = time();
          if($current_time - $feed_updated >= 300) {
                 $ch = curl_init();
                 curl_setopt($ch, CURLOPT_URL, $xml_feed_url);
                 curl_setopt($ch, CURLOPT_HEADER, false);
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                 curl_setopt($ch,CURLOPT_TIMEOUT,5);
                 $xml = curl_exec($ch);
                 file_put_contents($feed_file, $xml);}
                 curl_close($ch);
          }
          All cookies cleared!

          Comment

          • PornDiscounts-V
            Confirmed User
            • Oct 2003
            • 5744

            #6
            Why not save it to MySQL? Use cron job every 15 minutes.
            Blog Posts - Contextual Links - Hardlinks on 600+ Blog Network
            * Handwritten * 180 C Class IPs * Permanent! * Many Niches! * Bulk Discounts! GFYPosts /at/ J2Media.net

            Comment

            • lezinterracial
              Confirmed User
              • Jul 2012
              • 3117

              #7
              Originally posted by sarettah
              Why do the page pull (curl) each time? You have the feed saved as mysaved.xml every 5 minutes. Just use that file as the feed in the program and don't do the curl every time.
              You are absolutely right. I don't even know why I need to call Curl at all if I am using file_put_contents like Eddie showed. But for some reason I am getting an error in SimpleXMLElement if I don't call Curl.

              Here is my code now. Calling Curl on my own server is stupid, But I am getting a problem in the SimpleXMLElement function if I don't. My problem is a lack of understanding of XML. I am sure it is something simple, I just need to figure out.

              Code:
              $cacheName = 'chatcache.xml'; 
              $feed_updated = filemtime('chatcache.xml');
              $ageInSeconds = 120; 
                
              if(!file_exists($cacheName) || time() > $feed_updated + $ageInSeconds)
              { 
                echo "file saved";
                $contents = file_get_contents('https://chaturbate.com/affiliates/api/onlinerooms/?format=xml&wm=0CLZb'); 
                file_put_contents($cacheName, $contents); 
              } 
              $xml_feed_url = 'http://www.bestfreecamgirls.com/chatcache.xml';
              
              $ch = curl_init();
              curl_setopt($ch, CURLOPT_URL, $xml_feed_url);
              curl_setopt($ch, CURLOPT_HEADER, false);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
              curl_setopt($ch,CURLOPT_TIMEOUT,5);
              
              
              $xml = curl_exec($ch);
              curl_close($ch);
              
              $resource =  $chat->resource;
              $gender = $chat->resource->gender;
              $female = "f";
              $counter = 0;
              
              if ($xml != NULL){
              $chat = new SimpleXMLElement($xml);
              }
              Live Sex Shows

              Comment

              • lezinterracial
                Confirmed User
                • Jul 2012
                • 3117

                #8
                Thanks all.

                Nevermind. Got it. Curl completely pulled. Had to load the file into xml this way instead of simplexmlelement used simplexml_load_file.

                Although, I may switch to using curl for the copy instead of file_put_contents, So I can set a timeout if there is an issue with Chaturbate's XML file.

                Code:
                if (file_exists($cacheName)) {
                    $chat = simplexml_load_file($cacheName);
                } else {
                    exit('Failed to open test.xml.');
                }
                Live Sex Shows

                Comment

                • lezinterracial
                  Confirmed User
                  • Jul 2012
                  • 3117

                  #9
                  Originally posted by vvvvv
                  Why not save it to MySQL? Use cron job every 15 minutes.
                  I like that idea too. That way the user doesn't have to wait if the file needs to be updated.
                  Live Sex Shows

                  Comment

                  • EddyTheDog
                    Just Doing My Own Thing
                    • Jan 2011
                    • 25433

                    #10
                    Originally posted by lezinterracial
                    I like that idea too. That way the user doesn't have to wait if the file needs to be updated.
                    The code I posted should be used in a cron as well - The user would never have wait for an update...

                    However.....

                    You have got me back into this project and I am looking at using a DB so you can build up the database with all the cammers in it - Then mark them as on or off line using the XML file - That way you can build your own 'Cam Offline' and profile pages with the data...

                    As you collect more data you can create more complex pages with lots of info for the SEs.....

                    NEED MORE COFFEE!..

                    Comment

                    • nm_
                      Confirmed User
                      • May 2011
                      • 328

                      #11
                      Originally posted by EddyTheDog
                      The code I posted should be used in a cron as well - The user would never have wait for an update...

                      However.....

                      You have got me back into this project and I am looking at using a DB so you can build up the database with all the cammers in it - Then mark them as on or off line using the XML file - That way you can build your own 'Cam Offline' and profile pages with the data...

                      As you collect more data you can create more complex pages with lots of info for the SEs.....

                      NEED MORE COFFEE!..
                      Already beat you to this haha :D.. My "version" of this has multi site functionality. Can essentially create better white labels of whitelabels

                      Comment

                      Working...