7

php发送请求,解决curl返回空白问题

 3 years ago
source link: https://www.daozhao.com/9920.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

php发送请求,解决curl返回空白问题

如果您发现本文排版有问题,可以先点击下面的链接切换至老版进行查看!!!

php发送请求,解决curl返回空白问题

自己对php不熟悉,参考了网上的的,自己做个记录吧。


function postUrl($url, $post_data){
    $post_data  = json_encode($post_data);
    $headerArray =array("Content-type:application/json", "Accept:application/json");
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    /**配置返回信息**/
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//获取的信息以文件流的形式返回,不直接输出
    curl_setopt($curl, CURLOPT_HEADER, 0);//不返回header部分
    /**配置超时**/
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);//连接前等待时间,0不等待
    curl_setopt($curl, CURLOPT_TIMEOUT, 5);//连接后等待时间,0不等待。如下载mp3
    /**配置页面重定向**/
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);//跟踪爬取重定向页面
    curl_setopt($curl, CURLOPT_MAXREDIRS, 10);//指定最多的HTTP重定向的数量
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer

    /**配置HEADER**/
    curl_setopt($curl,CURLOPT_HTTPHEADER, $headerArray);
    curl_setopt($curl, CURLOPT_ENCODING, ""); // Accept-Encoding编码,支持"identity"/"deflate"/"gzip",空支持所有编码

    /**禁止证书验证防止curl输出空白**/
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); //禁止 cURL 验证对等证书
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE); //是否检测服务器的域名与证书上的是否一致
    /**配置POST**/
    curl_setopt($curl, CURLOPT_POST, 1); //支持post提交数据
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    $code = 200; //执行成功
    $data = curl_exec($curl);
    //捕抓异常
    if (curl_errno($curl)) {
        $code = 400; //执行异常
        $data = curl_error($curl);
    }
    curl_close($curl);
    return array('code' => $code, 'data' => json_decode($data, true));
}

参考地址PHP curl函数_解决curl返回空白问题

更新时间:2020-10-18 12:54

curl获取https开头的url的内容

平时我们用curl一般都获取http页面的内容,代码如下 $theurl= "http://www.xx.com";

$_data = array( 'clie 阅读更多…

不知道自己腾讯微博,新浪微博等微博的Access token和Token secret请进

为什么我使用该插件授权时总是失败,不能绑定帐号啊?

首先查看你主机是否支持curl_init()函数和 openssl_open()函数,如果不支持将导致得不到oauth token值,会影响使 阅读更多…


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK