PHP CURL

作者: 孙小胖2018 | 来源:发表于2018-09-21 10:53 被阅读0次
     private function curl($url, $data, $json=true)
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_URL, $url);
            if ($json && is_array($data)) {
                $data = json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
            }
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_NOSIGNAL, 1);    //注意,毫秒超时一定要设置这个
            curl_setopt($ch, CURLOPT_TIMEOUT_MS, 2500);  //超时毫秒,cURL 7.16.2中被加入。从PHP 5.2.3起可使用
    
            if ($json) {
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8','Content-Length:'.strlen($data)));
            }
            $relt = curl_exec($ch);
            curl_close($ch);
            return $relt;
        }
    

    相关文章

      网友评论

          本文标题:PHP CURL

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