PHP發起POST DELETE GET POST 請求

來源:互聯網
上載者:User

標籤:

原文連結:http://blog.csdn.net/lengxue789/article/details/8254667

關於POST,DELETE,GET,POST請求

get:是用來取得資料。其要傳遞過的資訊是拼在url後面,因為其功能使然,有長度的限制

post:是用來上傳資料。要上傳的資料放在request的head裡。沒有長度限制。主要是用於增加操作

put:也是用來上傳資料。但是一般是用在具體的資源上。主要用於修改操作

delete:用來刪除某一具體的資源上。

 

發起POST DELETE GET POST 請求通用類

 1 <?php 2 class commonFunction{ 3     function callInterfaceCommon($URL,$type,$params,$headers){ 4         $ch = curl_init(); 5         $timeout = 5; 6         curl_setopt ($ch, CURLOPT_URL, $URL);//目標地址 7         //要求標頭 8         if($headers!=""){ 9             curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);10         }else {11             curl_setopt ($ch, CURLOPT_HTTPHEADER, array(‘Content-type: text/json‘));12         }13         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);//返回結果,不輸出14         curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);//逾時時間15         //請求類型16         switch ($type){17             case "GET" :18             curl_setopt($ch, CURLOPT_HTTPGET, true);19             break;20             case "POST":21             curl_setopt($ch, CURLOPT_POST,true); 22             curl_setopt($ch, CURLOPT_POSTFIELDS,$params);23             break;24             case "PUT" :25             curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 26             curl_setopt($ch, CURLOPT_POSTFIELDS,$params);27             break;28             case "DELETE":29             curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 30             curl_setopt($ch, CURLOPT_POSTFIELDS,$params);31             break;32         }33         $file_contents = curl_exec($ch);//獲得傳回值34         curl_close($ch);35         return $file_contents;36     }37 }38 ?>

調用

1 <?php2 $params="{user:\"admin\",pwd:\"admin\"}";3 $headers=array(‘Content-type: text/json‘,"id: $ID","key:$Key");4 $url=$GLOBALS["serviceUrl"]."/user";5 $strResult= spClass("commonFunction")->callInterfaceCommon($url,"PUT",$params,$headers);6 ?>

$headers:如果參數值需要header傳,可以以數組格式傳遞

PHP發起POST DELETE GET POST 請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.