美文网首页
php发送curl请求失败原因总结

php发送curl请求失败原因总结

作者: 张清柏 | 来源:发表于2020-02-23 11:02 被阅读0次

首先贴一下代码

   public static function  http_post_json($url, $jsonStr)
    {
        $jsonStr=json_encode($jsonStr,JSON_UNESCAPED_UNICODE);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($jsonStr)
            )
        );
        $response = curl_exec($ch);

        var_dump(curl_errno($ch),curl_error($ch));die;
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        return array($httpCode, $response);
    }

这段代码是没有问题的,但是我发送请求就是不成功,返回false,
最后打印了一下信息

        var_dump(curl_errno($ch),curl_error($ch));die;

发现是url前面多了一个空格,nmd,去掉空格解决

相关文章

网友评论

      本文标题:php发送curl请求失败原因总结

      本文链接:https://www.haomeiwen.com/subject/cqndqhtx.html