PHP CURL call is displaying NULL all the time -


this question has been asked. of didn't work out me. getting null when curl call made time.

the following list_clients.php file.

<?php  require_once 'init.php'; header('content-type: application/json'); $clientresults  = mysqli_query($link, "select * clients"); $clients        = array(); while($client = mysqli_fetch_assoc($clientresults)){     $clients[] = $client; } echo json_encode($clients); exit; 

so output of above :

[{"ip_address":"192.168.177.137","mac_address":"3a:1a:cf:7c:92:89","added_on":"2017-08-19 12:48:34"},{"ip_address":"192.168.177.137","mac_address":"3a:1a:cf:7c:92:89","added_on":"2017-08-20 08:09:29"}] 

the following curl_call.php file

<?php $url    = 'http://127.0.0.1/testing/list_clients.php'; $curl   = curl_init();  curl_setopt($curl, curlopt_url, $url); curl_setopt($curl, curlopt_returntransfer, true); curl_setopt($curl, curlopt_httpget, true); curl_setopt($curl, curlopt_header, 0); //curl_setopt($curl, curlopt_ssl_verifypeer, false); //curl_setopt($curl, curlopt_ssl_verifyhost, false); curl_setopt($curl, curlopt_httpheader, array(                 'content-type : application/json',                 'accept: application/json'         )); $clientresult   = curl_exec($curl); if($clientresult == false) {     var_dump(curl_error($curl)); } curl_close($curl);  var_dump($clientresult); //for line getting following image eror  $clients        = json_decode($clientresult, true);  var_dump($clients); 

if var_dump($clientresult); getting following error

enter image description here

it's displaying null time. might causing error.

you missing warning being thrown $ch. it's undeclared, yet reference on line 17:

if($clientresult == false) {     var_dump(curl_error($ch)); } 

(as side note, i'd use === false, valid return values cast false don't wrongly trigger code.)

you're carrying on execution after error (failed be) handled. null you're seeing because curl request failed.

correct typo in code , stop (or appropriately) if error thrown:

if($clientresult == false) {     var_dump(curl_error($curl));     exit(); } 

update in response op being updated:

the other reason null might returned because response not valid json. check php documentation:

returns value encoded in json in appropriate php type. values true, false , null returned true, false , null respectively. null returned if json cannot decoded or if encoded data deeper recursion limit.

that html output you're debugging shows (in question update) won't parse json.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -