当前位置:网站首页>curlpost-php

curlpost-php

2022-07-06 00:36:00 owenzhang24

/** * 通過CURL發送HTTP請求 * * @param string $url        //請求URL * @param array  $postFields //請求參數 * @return mixed * */function curlPost($url, $data = null){//    if (is_array($data)) {//        $data = http_build_query($data);//    }    $data = json_encode($data);    $ch = curl_init();    curl_setopt($ch, CURLOPT_URL, $url);    curl_setopt($ch, CURLOPT_HTTPHEADER, array(            'Content-Type: application/json; charset=utf-8'   //json版本需要填寫  Content-Type: application/json;        )    );    curl_setopt($ch, CURLOPT_HEADER, false);//不返回頭部信息    curl_setopt($ch, CURLOPT_POST, 1);    if ($data != null) {        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    }    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  //結果是否顯示出來,1不顯示,0顯示    //判斷是否https    if (strpos($url, 'https://') !== false) {        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);        $UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';        curl_setopt($ch, CURLOPT_USERAGENT, $UserAgent);    }    $data = curl_exec($ch);    curl_close($ch);    if ($data === FALSE) {        $data = ['code' => 400, 'msg' => curl_error($ch)];    }    return ['code' => 200, 'data' => json_decode($data, true)];}
原网站

版权声明
本文为[owenzhang24]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060033327449.html