天天看點

curl實作put請求

<?php
 
function curlrequest($url,$data,$method='post'){
    $ch = curl_init(); //初始化CURL句柄 
    curl_setopt($ch, CURLOPT_URL, $url); //設定請求的URL
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //設為TRUE把curl_exec()結果轉化為字串,而不是直接輸出 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //設定請求方式
     
    curl_setopt($ch,CURLOPT_HTTPHEADER,array("X-HTTP-Method-Override: $method"));//設定HTTP頭資訊
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//設定送出的字元串
    $document = curl_exec($ch);//執行預定義的CURL 
    if(!curl_errno($ch)){ 
      $info = curl_getinfo($ch); 
      echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url']; 
    } else { 
      echo 'Curl error: ' . curl_error($ch); 
    }
    curl_close($ch);
     
    return $document;
}
 
$url = 'http://localhost/test/curl.php';
$data = "request from put method";
$return = curlrequest($url, $data, 'put');
 
var_dump($return);exit;
?>      
<?php
$arguments = file_get_contents('php://input');
print_r($arguments);      

轉載于:https://www.cnblogs.com/dongruiha/p/8707534.html