GTS Joe GTS Joe - 2 months ago 22
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:

{"ProductID":"1000096","ProductStyleID":"1001029","ProductCategoryID":"1000004"}


json.php



$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.php



$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

If you see a basic CURL example:- http://php.net/manual/en/curl.examples-basic.php

You come to know that instead of:-

"/json.php"

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