How to call json from php -
i have written php code should print response of json call. getting null output. have checked json call working fine restclient.
here code
<?php $username = "user"; $password = "password"; $postdata = array( 'username' => $username, 'password' => $password, 'msisdn' => "111111111" ); $ch = curl_init('http://ip:<port>/xxx/yyy/zzz'); curl_setopt_array($ch, array( curlopt_post => true, curlopt_returntransfer => true, curlopt_httpheader => array( 'content-type: application/json' ), curlopt_postfields => json_encode($postdata) )); // send request $response = curl_exec($ch); // check errors if($response === false){ die(curl_error($ch)); } // decode response $responsedata = json_decode($response, true); // print date response echo $responsedata['published']; var_dump($responsedata); ?>
thanks in advance.
try:
$responsedata = json_decode($response, true); if (json_last_error() !== json_error_none) { throw new \runtimeexception('invalid json.'); }
Comments
Post a Comment