Data from a PHP POST request does not arrive as JSON

Asked

Viewed 88 times

-1

I have a problem to make a POST request directly from PHP to REST, because the data does not arrive in JSON format.

{"status":"ok","result":{ "user"{"id":"xxx","username":"xxx","email":"xxx","limit_of_sessions":1,"strategy_of_sessions":"xxx","type":"xxx"},"token":"xxxx"} }

But when I take the value of the object it returns the value 1 or true.

<?php

$url = url;

$fields = array("login"=>"xxx","password"=>"xxx");
$fields = json_encode($fields);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json; charset=utf-8' 
  ));
$result = curl_exec($ch);
print_r( "Resultado ".$result[0]['status']);

curl_close($ch);

I have tried using Json_encode() or Json_decode() and nothing.

  • it seems to me that you are converting into json only the $Fields when you should actually convert the result

  • That’s right, Marcos. I used $formatedResponse = '['. $result. ']'; and it was. Thank you so much for your help.

  • Done, Thank you!

  • thanks, for nothing!

1 answer

0


You are converting only $Fields into json when you should actually convert the result

Browser other questions tagged

You are not signed in. Login or sign up in order to post.