sixli sixli - 6 months ago 26
PHP Question

CURL with port and additional parameters in URL, JSON parsing

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.

Answer

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);