2

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?

4
  • provide full path instead of "/json.php" Commented Sep 21, 2016 at 15:24
  • 1
    Well, that was fast. Didn't know I had to use the full URL as I'm used to using partial paths instead. Thanks a lot, guys. :) Commented Sep 21, 2016 at 15:25
  • @GTSJoe glad to help you.:):) Commented Sep 21, 2016 at 15:34
  • curl runs on the server. it has NO idea what your browser is doing. Commented Sep 22, 2016 at 21:16

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

This is the URL to get the data:-

$url="https://.../api.php?action";

Using cURL:

//  Initiate curl
$cuh = curl_init();
// Disable SSL verification
curl_setopt($cuh, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($cuh, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($cuh, CURLOPT_URL,$url);
// Execute
$result=curl_exec($cuh);
// Closing
curl_close($cuh);

// Will dump a beauty json :3
var_dump(json_decode($result, true));

1 Comment

not really helpful to OP. far away from actual answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.