dongsonghen9931 2017-08-20 07:42
浏览 110
已采纳

PHP CURL调用始终显示NULL

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

enter image description here

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

  • 写回答

2条回答 默认 最新

  • douluolan9101 2017-08-20 08:16
    关注

    Just now figured out. If I comment the following line of code in the curl_call.php it will work fine.

    <?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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。