Okay, let's have a look at something else.
You probably don't want to go trawling for data every time you need to process some, so let's look at saving some data.
State your REST source as usual, but instead of a simplexml load, do:
$somefile=fopen($somecall,"r");
file_put_contents("somedata.xml",$somefile);
fclose($somefile);
Which loads your resource, saves it to the server as xml (or whatever), then closes the connection. That's all you need to put it this file - end with ?> as usual and save as whatever - fetchdata.php
Then, whenever you need to refresh your source: include("fetchdata.php");
Whenever you want to use the data that's already there:
$sxmlobject = simplexml_load_file("somedata.xml");
if it's XML. If you have to deal with JSON you can still loop if you use an array, instead of a simplexml load.
Here's one last trick. On your main page, you can make it refresh the data in time intervals. Or specifically, you tell it to refresh if it's been at least X mins/hours/days since the last refresh.
//This function refreshes the data and records the time it does it.
function refreshData()
{
$t = time();
include("fetchdata.php");
file_put_contents("lastupdate.txt",$t);
}
//This is now
$timenow = time();
//If you can't find that record, refresh anyway. This is so it works first time!
if(!file_exists("lastupdate.txt"))
{
refreshData();
}
//Find out the last time you refreshed - might have been just a few lines above though!
$timeload = file_get_contents("lastupdate.txt");
//If it's been 10 hours - 36000 seconds - since refresh, refresh!
if ($timenow > $timeload + 36000)
{
refreshData();
}
$sxmlobject = simplexml_load_file("somedata.xml");
//and do something with it!
?>
Showing posts with label simplexml. Show all posts
Showing posts with label simplexml. Show all posts
Sunday, March 15
Particularily Happy Place - REST apis part 2
Okay, if you want to start querying, you'll need a server to run your PHP code. I recommend giving XAMPP a look - you'll get Apache, MySQL and PHP support all ready to go out of the box (okay, almost - I think all I had to do was initialise openssl in apache/bin/ini.php - google it) for free!
Ready to go? I'm a big fan of PHP for it's pretty open typing - I hate having to declare everything, I find this language makes it relatively easy to juggle resources without actively having to keep track of everything and worrying about type conversions etc. Let's just jump in.
The first thing to do is define your source. Most apis will have a token system to stop malicious users abusing the system, so I'm going to add that string in here too. Something like this:
<?php
$call = "http://example.com/api/weather?token=v5onv689g35";
creates a variable called "$call" and inserts your resource address as the value. Note the dollar sign denotes a variable. You'll then want to insert some other query strings:
$call = $call."&country=uk";
$call = $call."&city=london";
Note we could have lumped all three of these together in one declaration, but we'll want to come back later to change these, as it'd be useful to actually change their values. I'll come back to this.
$xml = simplexml_load_file($call);
Keeping this really simple, in one line we've: contacted the server ( here, at example.com), retrived the response and loaded it into a simplexml object called "$xml". If you look back to the last post, we had a response which contained one level of hierarchy - so we need to drill inside the "response" tag to get to the tags we want e.g. "temp"...
We can do this easily using a foreach statement, which looks at each child of the $xml variable (calling it $xmlchild). We then call the name of the tag $xmlname - and the print $xmlname and $xmlchild, with a hyphen between them, and a break at the end.
foreach($xml->children() as $xmlchild)
{
$xmlname = $xmlchild->getName();
echo $xmlname."-".$xmlchild."<br />";
}
?>
Once the server executes this PHP page, it would output the following to the browser:
temp-12.7
windspd-34.6
winddir-170
humid-54
Of course, you'd also want to build in controls to alter the query, do calculations with the data, display it in a meaningful way etc. but this is the basic technique I've been using - use PHP to send a request to a third party server, process and parse the response on a local sever, and serve the results back to the browser.
Ready to go? I'm a big fan of PHP for it's pretty open typing - I hate having to declare everything, I find this language makes it relatively easy to juggle resources without actively having to keep track of everything and worrying about type conversions etc. Let's just jump in.
The first thing to do is define your source. Most apis will have a token system to stop malicious users abusing the system, so I'm going to add that string in here too. Something like this:
<?php
$call = "http://example.com/api/weather?token=v5onv689g35";
creates a variable called "$call" and inserts your resource address as the value. Note the dollar sign denotes a variable. You'll then want to insert some other query strings:
$call = $call."&country=uk";
$call = $call."&city=london";
Note we could have lumped all three of these together in one declaration, but we'll want to come back later to change these, as it'd be useful to actually change their values. I'll come back to this.
$xml = simplexml_load_file($call);
Keeping this really simple, in one line we've: contacted the server ( here, at example.com), retrived the response and loaded it into a simplexml object called "$xml". If you look back to the last post, we had a response which contained one level of hierarchy - so we need to drill inside the "response" tag to get to the tags we want e.g. "temp"...
We can do this easily using a foreach statement, which looks at each child of the $xml variable (calling it $xmlchild). We then call the name of the tag $xmlname - and the print $xmlname and $xmlchild, with a hyphen between them, and a break at the end.
foreach($xml->children() as $xmlchild)
{
$xmlname = $xmlchild->getName();
echo $xmlname."-".$xmlchild."<br />";
}
?>
Once the server executes this PHP page, it would output the following to the browser:
temp-12.7
windspd-34.6
winddir-170
humid-54
Of course, you'd also want to build in controls to alter the query, do calculations with the data, display it in a meaningful way etc. but this is the basic technique I've been using - use PHP to send a request to a third party server, process and parse the response on a local sever, and serve the results back to the browser.
Subscribe to:
Posts (Atom)
