GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Tech Caching an XML file - Does this PHP cod look okay? (https://gfy.com/showthread.php?t=1178255)

lezinterracial 11-08-2015 07:18 AM

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);


EddyTheDog 11-08-2015 07:26 AM

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

EddyTheDog 11-08-2015 07:29 AM

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);
}

?>


lezinterracial 11-08-2015 07:35 AM

Quote:

Originally Posted by EddyTheDog (Post 20629001)
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);
}

?>


:thumbsup 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.

sarettah 11-08-2015 07:35 AM

Quote:

Originally Posted by lezinterracial (Post 20628994)
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.

Quote:

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);
}



PornDiscounts-V 11-08-2015 08:08 AM

Why not save it to MySQL? Use cron job every 15 minutes.

lezinterracial 11-08-2015 09:10 AM

Quote:

Originally Posted by sarettah (Post 20629005)
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);
}


lezinterracial 11-08-2015 09:39 AM

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.');
}


lezinterracial 11-08-2015 09:59 AM

Quote:

Originally Posted by vvvvv (Post 20629022)
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.

EddyTheDog 11-08-2015 10:28 AM

Quote:

Originally Posted by lezinterracial (Post 20629120)
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:Oh crap!..

nm_ 11-08-2015 10:51 AM

Quote:

Originally Posted by EddyTheDog (Post 20629135)
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:Oh crap!..

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


All times are GMT -7. The time now is 11:29 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123