Reputation: 2854
I made a page that retrieves information from an API. It is taking about 10-20 seconds to get the response from their server. The request is done fro js through a PHP proxy to avoid cross-domain issues. I'm wondering, as the data is not like to be changing often (it is congressman's data) I might make a request daily and store it in my server so the page loads faster, I can provide a "refresh" button incase the user needs it. How can that be done? I'm just starting in web development and i see that is necessary knowledge of several different languages and frameworks, now I'm diving into js, but i got this PHP hack working for my requests, bottom line i know almost nothing of PHP yet.
[edit] ok I got the cron working! Now how do i save the document.xml from php script? I tried:
$c_t = fopen('file.xml', 'ab+');
fwrite($c_t, date('Y-m-d H:i:s')."\n\n"); //add timestamp to theverbose log
fwrite($c_t,$xml);
inserted just before `curl_close($session);
The file was saved, but only with the timestamp...
here's what i got...
<?php
// PHP Proxy --- only 'GET'
// Based on script from yahoo's example @ http://developer.yahoo.com/javascript/samples/proxy/php_proxy_simple.txt
// tweaked by vk
// hostname - just base domain
define ('HOSTNAME', 'http://www.camara.gov.br');
// Get the REST call path from the AJAX application - js;
$path = $_GET['yws_path1'];
// assign yo url
$url = HOSTNAME.$path;
//Open the Curl session
$session = curl_init($url);
// Don't return HTTP headers. Do return the contents of the call
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_HTTPGET, true);
//set user agent, that did it!!! copied from browsers...
curl_setopt($session, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36');
// The web service returns XML. Set the Content-Type appropriately
header("Content-Type: text/xml");
// Make the call
$xml = curl_exec($session);
// done, shutDown.
curl_close($session);
?>
Upvotes: 2
Views: 124
Reputation: 56
If you are on a Mac, or Linux; you can schedule your code to execute via cron. PHP does not have to be run by a browser.
php -f yourcode.php > results.txt
'man php' for more information... and 'man crontab -s 5' for details on scheduling using cron
Upvotes: 4