PHP CURL used to achieve GET, POST, PUT, DELETE requests

/**
 * @param $url
 * @param $data
 * @param string $method
 * @param string $type
 * @return bool|string
 */
function curlData($url,$data,$method = 'GET',$type='json')
{
    //初始化
    $ch = curl_init();
    $headers = [
        '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;
        }
    }
    // 请求头,可以传数组
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers[$type]);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);          // After that do not directly printed 
    IF ( $ Method == 'the POST' ) { 
        curl_setopt ( $ CH , CURLOPT_CUSTOMREQUEST, 'the POST');      // request method 
        curl_setopt ( $ CH , CURLOPT_POST, to true );                // POST Submit 
        curl_setopt ( $ CH , CURLOPT_POSTFIELDS, $ Data );               // variables of post 
    }
     IF ( $ Method == 'the PUT' ) { 
        curl_setopt ( $ CH , CURLOPT_CUSTOMREQUEST, "the PUT" );
        curl_setopt ( $ CH , CURLOPT_POSTFIELDS, $ Data ); 
    } 
    IF ( $ Method == 'the DELETE' ) { 
        curl_setopt ( $ CH , CURLOPT_CUSTOMREQUEST, "the DELETE" ); 
        curl_setopt ( $ CH , CURLOPT_POSTFIELDS, $ Data ); 
    } 
    curl_setopt ( $ CH , CURLOPT_SSL_VERIFYPEER, to false ); // skip certificate checking 
    curl_setopt ( $ CH , CURLOPT_SSL_VERIFYHOST, to false ); // do not check whether there is SSL encryption certificate from 
    $ Output = the curl_exec ( $ CH); // execute and get the HTML document content 
    curl_close ( $ CH ); // release the curl handle to 
    return  $ the Output ; 
}

 

Guess you like

Origin www.cnblogs.com/yittxbug/p/11125953.html