php實現(xiàn)模擬post請求用法實例
更新時間:2015年07月11日 18:16:07 作者:冥想_
這篇文章主要介紹了php實現(xiàn)模擬post請求用法,分析了php模擬post請求的三種常見用法,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了php實現(xiàn)模擬post請求的方法。分享給大家供大家參考。具體如下:
class Request{ public static function post($url, $post_data = '', $timeout = 5){//curl $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_POST, 1); if($post_data != ''){ curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); } curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_HEADER, false); $file_contents = curl_exec($ch); curl_close($ch); return $file_contents; } public static function post2($url, $data=array()){//file_get_content $postdata = http_build_query( $data ); $opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata ) ); $context = stream_context_create($opts); $result = file_get_contents($url, false, $context); return $result; } public static function post3($host,$path,$query,$others=''){//fsocket $post="POST $path HTTP/1.1\r\nHost: $host\r\n"; $post.="Content-type: application/x-www-form-"; $post.="urlencoded\r\n${others}"; $post.="User-Agent: Mozilla 4.0\r\nContent-length: "; $post.=strlen($query)."\r\nConnection: close\r\n\r\n$query"; $h=fsockopen($host,80); fwrite($h,$post); for($a=0,$r='';!$a;){ $b=fread($h,8192); $r.=$b; $a=(($b=='')?1:0); } fclose($h); return $r; } } $url='http://******/con/Inter.php'; $data=Request::post($url,array('api'=>'tag_list')); $data2=Request::post2($url,array('api'=>'tag_list')); echo $data;
希望本文所述對大家的php程序設計有所幫助。
您可能感興趣的文章:
相關文章
php實現(xiàn)的返回數(shù)據(jù)格式化類實例
這篇文章主要介紹了php實現(xiàn)的返回數(shù)據(jù)格式化類及其應用實例,包括針對XML、JSON等的格式化,非常具有實用價值,需要的朋友可以參考下2014-09-09PHP使用SOAP擴展實現(xiàn)WebService的方法
這篇文章主要介紹了PHP使用SOAP擴展實現(xiàn)WebService的方法,結(jié)合實例形式較為詳細的分析了SOAP擴展的原理及實現(xiàn)WebService的相關技巧,需要的朋友可以參考下2016-04-04PHP file_get_contents 函數(shù)超時的幾種解決方法
在使用file_get_contents函數(shù)的時候,經(jīng)常會出現(xiàn)超時的情況,在這里要通過查看一下錯誤提示,看看是哪種錯誤,比較常見的是讀取超時,這種情況大家可以通過一些方法來盡量的避免或者解決。2009-07-07PHP讀取XML文件的方法實例總結(jié)【DOMDocument及simplexml方法】
這篇文章主要介紹了PHP讀取XML文件的方法,結(jié)合實例形式總結(jié)分析了php基于DOMDocument及simplexml方法針對xml文件的載入、讀取等相關操作技巧,需要的朋友可以參考下2019-09-09php中0,null,empty,空,false,字符串關系的詳細介紹
本篇文章是對php中0,null,empty,空,false,字符串關系進行了詳細的分析介紹,需要的朋友參考下2013-06-06