首页 > Web开发 > 详细

php curl模拟常用的请求

时间:2021-08-12 12:01:06      阅读:13      评论:0      收藏:0      [点我收藏+]
<?php
namespace servers\serveridcims\controller\client;
/**
 * 客户端操作服务器类
 */

class serveridcims
{
   /**
     * 公共请求接口函数
     * @param $url
     * @param $data
     * @param string $method
     * @param string $type
     * @param array $headers
     * @return bool|string
     */

    public function curlpost($url,$data,$method = ‘GET‘,$type=‘json‘,$headers=[])
    {

        try{
            //初始化
            $ch = curl_init();
            $headers[] =  "cache-control: no-cache";
            $contentType = [
                ‘form-data‘ => ‘Content-Type: multipart/form-data‘,
                ‘json‘      => ‘Content-Type: application/json‘,
            ];
            if($method == ‘GET‘){
                if($data){
                    $querystring = http_build_query($data);
                    $url = $url.‘?‘.$querystring;
                }
            }

            $headers[] = $contentType[$type];

            // 请求头,可以传数组
            curl_setopt($ch, CURLOPT_URL,$url);
            curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
            curl_setopt($ch, CURLOPT_HEADER, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);         // 执行后不直接打印出来
            curl_setopt($ch, CURLOPT_NOBODY,false);

            curl_setopt($ch, CURLOPT_SSLVERSION, 3);
            curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, ‘SSLv3‘);

            if($method == ‘POST‘){
                if($type==‘json‘){
                    $data = json_encode($data);
                }

                curl_setopt($ch, CURLOPT_CUSTOMREQUEST,‘POST‘);     // 请求方式
                curl_setopt($ch, CURLOPT_POST, true);               // post提交
                curl_setopt($ch, CURLOPT_POSTFIELDS,$data);                 // post的变量
            }
            if($method == ‘PUT‘){
                if($type==‘json‘){
                    $data = json_encode($data);
                }

                curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT");
                curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
            }
            if($method == ‘DELETE‘){
               if($type==‘json‘){
                   $data = json_encode($data);
               }
                curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
                curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
            }

            curl_setopt($ch, CURLOPT_TIMEOUT, 120);  // 最大执行时间
            curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);  // 最大执行时间
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不从证书中检查SSL加密算法是否存在
            curl_setopt($ch, CURLOPT_SSLVERSION, 6);
            $content = curl_exec($ch); //执行并获取HTML文档内容
            $headerStr = curl_getinfo($ch,CURLINFO_HEADER_OUT);// 执行并获取头部内容
            list($headerinfo,$output)=explode("\r\n\r\n",$content,2);

            $err = curl_error($ch);

            if($err){
                return $this->failReturn(‘curl_errno:‘.$err);
            }

            curl_close($ch); //释放curl句柄

            $response = json_decode($output, true);
            if (json_last_error() == JSON_ERROR_NONE) {
                $response[‘headerinfo‘] = $headerinfo;
                return $response;
            } else {
                if (empty($output) || $output == ‘Success‘) {
                    return $this->successReturn($output);
                }else{
                    if (!empty($output)){
                        return $this->successReturn($output);
                    }else{
                        return $this->failReturn($output);
                    }

                }
            }
        }catch (\Exception $e){
            return $this->failReturn(‘curl_errno:‘.$err);
        }
    }

    /**
     * 返回成功
     */
    public function successReturn($data = [], $msg = "")
    {
        return [
            "result" => "success",
            "code" => 200,
            "msg" => $msg ? $msg : "Successful",
            "data" => $data,
        ];
    }

    /**
     * 返回失败
     */
    public function failReturn($msg = ‘‘, $code = 1, $data = [])
    {
        return [
            ‘result‘ => ‘faild‘,
            ‘code‘ => $code,
            ‘msg‘ => $msg,
            ‘data‘ => $data,
        ];
    }


}

php curl模拟常用的请求

原文:https://www.cnblogs.com/meetuj/p/15131505.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!