Title: How to call json from php
Tags: php;json;api;curl
Question: I have written a php code that should print the response of a json call. But I am getting NULL output. I have checked my json call is working fine from a restclient.
Here is my code
<?php
$username = "user";
$password = "password";
$postData = array(
'username' =&gt; $username,
'password' =&gt; $password,
'msisdn' =&gt; "111111111"
);
$ch = curl_init('http://ip:<port&gt;/xxx/yyy/zzz');
curl_setopt_array($ch, array(
CURLOPT_POST =&gt; TRUE,
CURLOPT_RETURNTRANSFER =&gt; TRUE,
CURLOPT_HTTPHEADER =&gt; array(
'Content-Type: application/json'
),
CURLOPT_POSTFIELDS =&gt; json_encode($postData)
));
// Send the request
$response = curl_exec($ch);
// Check for errors
if($response === FALSE){
die(curl_error($ch));
// Decode the response
$responseData = json_decode($response, TRUE);
// Print the date from the response
echo $responseData['published'];
var_dump($responseData);
?&gt;
Thanks in advance.
Comment: have you checked, if your $response actually contains JSON-code, and if so, if it is valid?

Comment: `var_dump($response)` and see what response you are getting.

Comment: Try `print_r(json_last_error());`

Comment: Yep. Actually I meant `json_last_error_msg()`, but yes, 4 means syntax error. So yes, use @Vladimir Kovpak's method to automatically check if server has an error.

Comment: `var_dump($response)` is giving HTTP Status 500 with following exception -
`javax.servlet.ServletException: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "username"`

Comment: @Pemapmodder `print_r(json_last_error());` gave 4 as output which is a syntax error right?

Comment: Well, this means you have provided a wrong key in your *$postData*. *"username"* is not a valid key it seems, so find out what the right key is.

Here is the accepted answer: okay so i have solved my problem and re-wrote the code. I was actually not providing the correct form of username and password in the json fields which is really a silly mistake.
I am giving my updated code here
$data = array("user" =&gt; "$username", "pass" =&gt; "$password", "msisdn" =&gt; "$msisdn");
$data_string = json_encode($data);
$ch = curl_init('http://ip:port/call_center/account/general');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
echo $result;
?&gt;`
Thanks everyone.

Here is another answer: $responseData = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \RuntimeException('Invalid JSON.');
Comment for this answer: It mean that you don't receive json, or not valid json.
Comment for this answer: i got `Stack trace: #0 {main} thrown in` exception

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 7, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:01/28/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><json><api><curl>', 'Title': 'How to call json from php', 'ViewCount': 440, 'id': 35059520, 'user_AboutMe': None, 'user_AccountId': 7727324.0, 'user_DisplayName': 'parsa', 'user_Reputation': 45.0, 'user_Views': 5.0, 'user_id': 5851553.0}}