PHP通用型CURL


    function http_curl_v1($url, $postBody, $method = "POST")
    {
//        $all_url_arr = explode("?", $all_url);//请求的全部地址,此方法可以完全替代:file_get_contents
//        $url = $all_url_arr[0];//请求地址
//        $postBody = $this->url_to_arr($all_url_arr[1]);//转换成数组,方便提交
        $headers = array(
            'Content-Type' => 'application/x-www-form-urlencoded;text/json;charset=utf-8'
//            'Content-Type' => 'application/x-www-form-urlencoded'
//            'Content-Type' => 'multipart/form-data'
//            'Content-Type' => 'application/json'
        );

        // 初始化curl
        $ch = curl_init();
        // 设置curl参数
        curl_setopt($ch, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出,GET好像要是1
        curl_setopt($ch, CURLOPT_HEADER, 0);//改成1可以打印出结果,这样可以判断是否成功201就是成功
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        if ($method == 'POST') {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);//15秒超时
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);//15秒超时
//            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postBody));
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postBody);
        } else if ($method == 'DELETE') {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
        } else if ($method == 'PUT') {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postBody);
        }
        //采集
        $output = curl_exec($ch);
//      var_dump($output);
        // curl请求返回状态码
//      $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);//这个信息太少,弃用
        $curl_info = curl_getinfo($ch); //获取全部的,官方文档:https://www.php.net/manual/zh/function.curl-getinfo.php
        $error = curl_error($ch);
        // 关闭
        curl_close($ch);
        if ($error) {
            // 请求发生错误,返回错误信息
            $return_array = array(
                'code' => $curl_info['http_code'],
                'msg' => '网络异常',
                'data' => $error,
                'curl_info' => $curl_info);
            return json_encode($return_array, JSON_UNESCAPED_UNICODE);//返回json
        } else {
            $output_array = json_decode($output, true);//解成数组
            if (is_array($output_array)) {
                unset($output_array['curl_info']);//去掉之前的
                //增加CURL信息,方便排查错误 //demo 2022/1/19 13:40
                $output_array['curl_info'] = $curl_info;
                return json_encode($output_array, JSON_UNESCAPED_UNICODE);//返回json
            } else {
                return $output;//不是标准数组,直接返回错误。返回字符串
            }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值