【问题标题】:why PHP + curl, HTTP POST request returns null?为什么PHP + curl,HTTP POST请求返回null?
【发布时间】:2017-11-25 21:17:24
【问题描述】:

我很困惑为什么 $response 没有返回任何东西,我是 curl 新手,请帮忙,

$post = [
        'AreaCode' => '02',
        'PhoneNumber' => '33312733',
        'InquiryBy'   => 'telephone',
    ];

    $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, 'https://billing.te.eg/api/Account/Inquiry');
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
     curl_setopt($ch, CURLOPT_HTTPHEADER, array(
       'Content-Type:application/json',
       'Access-Control-Allow-Origin: *',
     ));
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
     curl_setopt($ch, CURLOPT_TIMEOUT, 10);
     $httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); 
     $response = curl_exec($ch);
     if ($response === false) $response = curl_error($ch);
     echo stripslashes($response);
     curl_close($ch);

在邮递员中

【问题讨论】:

  • 您的数据不会自动变成 JSON,只是因为您设置了 Content-Type 标头这样说。将数组传递给 CURLOPT_POSTFIELDS 将导致它创建类似查询字符串的格式,name1=value1&name2=value2&... 改为传递 JSON 字符串。

标签: php laravel api curl


【解决方案1】:

下载 cacert.pem 文件保存在根目录并设置 curl 选项并尝试

http://curl.haxx.se/ca/cacert.pem

curl_setopt ($h, CURLOPT_CAINFO, base_path()."/cacert.pem");

【讨论】:

    【解决方案2】:

    请添加以下行来定义请求类型(GET/POST/..),然后尝试:

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    

    【讨论】:

      猜你喜欢
      • 2018-02-23
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      • 1970-01-01
      • 2015-05-13
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      相关资源
      最近更新 更多