1

This is a simple JSON but it cannot access an item. I send the that JSON to get.php and there just print_r the JSON and in parsing JSON response is problem.

My code:

$url = "http://localhost/get.php";    
    $data = array(
        'item1'      => 'value1',
        'item2'      => 'value2',
        'item3'      => 'value3'
    );


$content = json_encode($data);

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ( $status == 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " curl_errno($curl));
}


curl_close($curl);
$response = json_decode($json_response, false);
$result=json_encode($json_response);
$data=json_decode($result);

How can I echo item 1 or 2 ?

This is get.php

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
  $data = json_decode(file_get_contents("php://input"));
  print_r($data);
}
16
  • 1
    the json is irrelevant. once it's decoded, it's a PHP data structure, like any other PHP data structure. there will be absolutely NO trace within it that says "I used to be json". You access the data like any other php structure. Commented Sep 9, 2016 at 19:22
  • 2
    Simple echo $result['item1'];, echo $result['item2']; will do. BTW there's no point encoding it, and then decoding it again. Commented Sep 9, 2016 at 19:22
  • 2
    What does this have to do with stdClass Object? When you use true as the second argument to json_decode() it returns an associative array, not an object. Commented Sep 9, 2016 at 19:23
  • As Rajdeep just said, echo it based upon the name you gave the index, as you would any array. If you want to loop through, use a loop. Commented Sep 9, 2016 at 19:23
  • @Barmar i did not get answer guys !! Commented Sep 9, 2016 at 19:51

4 Answers 4

4

Since get.php uses print_r(), it's not returning JSON. It needs to use json_encode().

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
  $data = json_decode(file_get_contents("php://input"));
  echo json_encode($data);
}

Then your curl script can decode it properly.

$response = json_decode($json_response, false);
echo 'Item1 = ' . $response->item1 . '<br>';
echo 'Item2 = ' . $response->item2 . '<br>';
echo 'Item3 = ' . $response->item3;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You Very Much Bro . :X :X
0

You don't need set second param in json_decode function for getting stdClass.

$data = array(
    'item1' => 'value1',
    'item2' => 'value2',
    'item3' => 'value3'
);
$json=json_encode($data);
$result=json_decode($json);
echo $result->item1;
echo $result->item2;
echo $result->item3;

Or you can work with array

$json=json_encode($data);
$result=json_decode($json, true);
echo $result['item1'];
echo $result['item2'];
echo $result['item3'];

Comments

0

use json_encode() to encode the data , then print the json

  $data = json_decode(file_get_contents("php://input"));
  echo json_encode($data);

Comments

-1
curl_close($curl);
$result = json_decode($jsonResponse, true);
echo $result['item1']; //Echo item 1.

3 Comments

it returns nothing
You can see the keys in the question, they're not numeric.
That's why i added the other answer. It looks like he doesn't get array as result though. http://php.net/manual/en/function.json-decode.php says json_decode($json, true) returns array...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.