get:是用來取得數據。其要傳遞過的信息是拼在url后面,因為其功能使然,有長度的限制
post:是用來上傳數據。要上傳的數據放在request的head里。沒有長度限制。主要是用於增加操作
put:也是用來上傳數據。但是一般是用在具體的資源上。主要用於修改操作
delete:用來刪除某一具體的資源上
發起POST DELETE GET POST 請求通用類
<?php class commonFunction{ function callInterfaceCommon($URL,$type,$params,$headers){ $ch = curl_init(); $timeout = 5; curl_setopt ($ch, CURLOPT_URL, $URL); //發貼地址 if($headers!=""){ curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers); }else { curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type: text/json')); } curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); switch ($type){ case "GET" : curl_setopt($ch, CURLOPT_HTTPGET, true);break; case "POST": curl_setopt($ch, CURLOPT_POST,true); curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break; case "PUT" : curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break; case "DELETE":curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break; } $file_contents = curl_exec($ch);//獲得返回值 return $file_contents; curl_close($ch); } } ?>
$params="{user:\"admin\",pwd:\"admin\"}"; $headers=array('Content-type: text/json',"id: $ID","key:$Key"); $url=$GLOBALS["serviceUrl"]."/user"; $strResult= spClass("commonFunction")->callInterfaceCommon($url,"PUT",$params,$headers);