php 之curl实现 post请求

        if(!function_exists('curl_init')){  //检查服务器是否支持curl
            return 'curl not exists';
        }
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
        //如果是请求的是https页面,需要添加下面两个参数,规避ssl的证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
        //header头信息的添加--数组
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Connection: keep-alive",  "Content-Type: multipart/form-data", "Authorization:TOKEN " . $token));

        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);//上传文件或者以上传文件的形式post数据
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);//post的数据
        $data = curl_exec($ch);//得到返回的数据
        $httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);//得到的响应码
        http_response_code($httpCode);//设置响应状态码
        if (curl_errno($ch)) {
            echo 'Errno'.curl_error($ch);//捕抓异常
        }
        curl_close($ch);

猜你喜欢

转载自blog.csdn.net/weixin_42809940/article/details/81288313