Jump to content

Help setting up cron job to pull HTTP request and create/cache file from results


mgunnels

Recommended Posts

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!!!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.