Reputation: 547
First thanks for any help that you can provide. I am somewhat new to PHP and this is the first "Web Service" I have had to create.
THE GOAL: I need to pull XML data from another server. This company's API is setup so that you must give an IP and so you can only pull data from server to server instead of client to server. The data is pulled from the API using HTTP requests...very similar to YQL. (in essence the structured query is located in the URL). This API also requires that my server only ping's their server every 10-15 mins, in order to cut down on server requests.
The logical thought in my head was to setup: a cron job to run a PHP script every 10 mins. The PHP scripts would then do the following: 1. Make the HTTP request 2. Open an existing file or create one (on my server) 3. Take the returned XML data from the API and write to the newly opened file. 4. Convert that XML to JSON. 5. Save the JSON 6. Cache the JSON file 7. STOP
My thought was to use curl and fopen for the first 3 steps. I found a basic script that will do this on PHP.net (as shown below). After that I am pretty much lost on how to proceed.
<?php
$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
I would REALLY appreciate any help on this. Also, if you have the time to please comment and explain what you are doing in any code examples and why....that would HELP out a lot. I truly want to learn not just grab a snippet and run. So, your comments are vital for me.
thank!!!
Upvotes: 0
Views: 2522
Reputation: 2974
You could do this with more convenient functions, such as file_get_contents and file_put_contents.
Converting XML to JSON is a kind of ambiguous statement, since XML data structures don't necessarily map to JSON directly, since XML can contain attributes, etc. So, you'd have to figure out how to do that on your own depending on the structure of your data. You could use either the SimpleXML PHP module (http://php.net/manual/en/book.simplexml.php) or the DOM module (http://www.php.net/manual/en/book.dom.php).
At a high level, your code could then look something like this: (This assumes that you don't need any special authentication for that API by using HTTP headers or anything like that)
<?php
$remote_data_xml = file_get_contents("http://www.example.com/file.xml");
$parsed_data = my_custom_xml_parsing_function($remote_data_xml);
$parsed_data_json = json_encode($parsed_data);
file_put_contents("my_json_cache.json", $parsed_data_json);
?>
Upvotes: 3