• php curl模拟常用的请求


    <?php
    namespace serversserveridcimscontrollerclient;
    /**
     * 客户端操作服务器类
     */
    
    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("
    
    ",$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,
            ];
        }
    
    
    }
    
  • 相关阅读:
    1-7周成绩总结
    1-6周成绩总结
    第七周学习笔记
    前五周测验成绩总结
    第六周学习笔记
    第五周学习笔记
    2018-2019-2 20189206 Python3学习
    2018-2019-2 20189206 安全工具的学习
    2018-2019-2 20189206 《网络攻防实践》 第一周作业
    2018-2019-1 20189206 《Linux内核原理与分析》第九周作业
  • 原文地址:https://www.cnblogs.com/meetuj/p/15131505.html
Copyright © 2020-2023  润新知