Home >php教程 >PHP源码 >PHP之curl put方式提交与接受数据

PHP之curl put方式提交与接受数据

PHP中文网
PHP中文网Original
2016-05-25 17:07:292032browse

PUT方式提交数据,php输入流接受数据,很完美的配合

1.php代码

<?php
 
function curlrequest($url,$data,$method=&#39;post&#39;){
    $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 &#39;Took &#39; . $info[&#39;total_time&#39;] . &#39; seconds to send a request to &#39; . $info[&#39;url&#39;]; 
    } else { 
      echo &#39;Curl error: &#39; . curl_error($ch); 
    }
    curl_close($ch);
     
    return $document;
}
 
$url = &#39;http://localhost/test/curl.php&#39;;
$data = "request from put method";
$return = curlrequest($url, $data, &#39;put&#39;);
 
var_dump($return);exit;
?>

2.php代码

<?php
$arguments = file_get_contents(&#39;php://input&#39;);
print_r($arguments);
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn