Welcome

首页 / 网页编程 / PHP / PHP整理的关于Curl数据提交的方法

function http_query($url,$params,$type="POST")
{
$jsonStr=json_encode($params);
        $ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;',$this->token));
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        switch ($type)
{
            case "GET" : 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
break;
            case "POST": 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST,true);
                curl_setopt($ch, CURLOPT_POSTFIELDS,$jsonStr);
break;
            case "PUT" : 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
                curl_setopt($ch, CURLOPT_POSTFIELDS,$jsonStr);
break;
            case "PATCH" : 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PATCH");  
curl_setopt($ch, CURLOPT_POSTFIELDS,$jsonStr);
break;
            case "DELETE":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
                curl_setopt($ch, CURLOPT_POSTFIELDS,$jsonStr);
break;
        }
        $response = curl_exec($ch);
        curl_close($ch);
        return $response;
    }