GTS Joe GTS Joe - 1 year ago 163
JSON Question

Get JSON Data with PHP cURL

I have a simple script that outputs some JSON. I'm trying to retrieve the JSON contents in a variable with another PHP script, but when I do a var_dump(), I'm getting NULL instead of the JSON data.

Here's the JSON. The only output is:



$arr['ProductID'] = "1000096";
$arr['ProductStyleID'] = "1001029";
$arr['ProductCategoryID'] = "1000004";

$json_arr = json_encode($arr);
echo $json_arr;

And here's the cURL script, which right now is outputting NULL:


$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "/json.php");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec ($curl);
curl_close ($curl);
var_dump(json_decode($result, true));

How can I retrieve the JSON echoed in the json.php script?

Answer Source

If you see a basic CURL example:-

You come to know that instead of:-


You need to give full path like:-

(http://...) or (https://...) for your file (based on your server)

Note:- Rest of your code seems perfectly fine.Thanks

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download