Reputation: 303
I have a URL in the form of http://www.domain.com:10001/api/Data.cgi?Scope=System
On that URL is a JSON dump which I need to parse. How do I access it? I've used get_file_contents
before which is why I am struggling with this - first occasion to have a port on the URL. It would be best if I could grab the JSON and put it in a file.txt and then parse the file.
<?php
$cURL = curl_init('http://81.1.1.1');
curl_setopt($cURL, CURLOPT_PORT, 10001);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
$fp = fopen("example_homepage.txt", "w");
curl_setopt($cURL, CURLOPT_FILE, $fp);
curl_setopt($cURL, CURLOPT_HEADER, 0);
curl_exec($cURL);
curl_close($cURL);
fclose($fp);
?>
I'm not sure how to properly add the remainder of the URL.
Upvotes: 1
Views: 898
Reputation: 1488
If you don't need to use curl, it may be easier with file_get_contents.
// Retrieve file
$data = file_get_contents("http://81.1.1.1:10001/page_url.json?query_parameters=values");
// Echo retrieved file
echo $data;
// Decode JSON
$decoded = json_decode($data);
// Dump decoded data
var_dump($decoded);
Upvotes: 2
Reputation: 1300
Try this:
function curl($url) {
$options = Array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_AUTOREFERER => TRUE
,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8",
CURLOPT_URL => $url,
);
$ch = curl_init(); // Initialising cURL
curl_setopt_array($ch, $options);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$scraped_page = curl($link);
Upvotes: 0