cURL模拟HTTP请求(支持HTTPS)

function setHttpRequest($url,$headers,$params=array(),$method="GET") {
        $ci = curl_init();
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ci, CURLOPT_TIMEOUT, 30);
        
        curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method);

        curl_setopt($ci, CURLOPT_URL, $url);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ci, CURLOPT_HEADER, FALSE);
		
		curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查  
		curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在

        if($headers!=""){
            curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        }else {
            curl_setopt($ci, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
        }
        switch (strtoupper($method)){
            case 'POST':
                    curl_setopt($ci, CURLOPT_POST, TRUE);
                
                    curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
                break;
            case 'PUT':
                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'PUT');
             
                curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
    
                break;
            case 'PATCH':
                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'PATCH');
             
                curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
            
                break;
            case 'DELETE':
                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
               
                curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
             
                break;
            default:
                curl_setopt($ci, CURLOPT_HTTPGET, TRUE);
                break;
        }
        $response = curl_exec($ci);
        $httpCode = curl_getinfo($ci);
        curl_close($ci);
        return $response;
    }

posted @   selfim  阅读(3902)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
· SQL Server 内存占用高分析
阅读排行:
· DeepSeek V3 两周使用总结
· 回顾我的软件开发经历(1)
· C#使用yield关键字提升迭代性能与效率
· 低成本高可用方案!Linux系统下SQL Server数据库镜像配置全流程详解
· 4. 使用sql查询excel内容
点击右上角即可分享
微信分享提示