PHP CURL call is displaying NULL all the time

This question has been asked. But any of those didn’t work out for me. I am getting NULL when CURL call is made all the time.

The following is my list_clients.php file.

<?php

require_once 'init.php';
header('Content-Type: application/json');
$clientResults  = mysqli_query($link, "SELECT * FROM clients");
$clients        = array();
while($client = mysqli_fetch_assoc($clientResults)){
    $clients[] = $client;
}
echo json_encode($clients);
exit;

So the output of the above is :

[{"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 is my 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 this line I am getting the following image eror

$clients        = json_decode($clientResult, TRUE);

var_dump($clients);

If I var_dump($clientResult); then I am getting the following error

It’s displaying NULL all the time. What might be causing the error.

You are missing a warning being thrown about $ch. It’s undeclared, yet you reference it on line 17:

if($clientResult == FALSE) {
    var_dump(curl_error($ch));
}

(As a side note, I’d use === FALSE, so valid return values which cast to FALSE don’t wrongly trigger that code.)

You’re also then carrying on with execution after the error is (failed to be) handled. The NULL you’re seeing could be because the curl request failed.

Correct the typo in your code and stop (or do appropriately) if an error is thrown:

if($clientResult == FALSE) {
    var_dump(curl_error($curl));
    exit();
}

Update in response to the OP being updated:

The other reason NULL might be returned is because the response is not valid json. Check the PHP documentation:

Returns the value encoded in json in appropriate PHP type. Values
true, false and null are returned as TRUE, FALSE and NULL
respectively. NULL is returned if the json cannot be decoded or if the
encoded data is deeper than the recursion limit.

That HTML output you’re debugging now shows (in your question update) won’t parse as JSON.

just now figured out. If I comment the following line of code in the curl_call.php

<?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);
$clients        = json_decode($clientResult);

var_dump($clients);