php實(shí)現(xiàn)模擬post請(qǐng)求用法實(shí)例
本文實(shí)例講述了php實(shí)現(xiàn)模擬post請(qǐng)求的方法。分享給大家供大家參考。具體如下:
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;
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- PHP基于curl模擬post提交json數(shù)據(jù)示例
- php使用CURL模擬GET與POST向微信接口提交及獲取數(shù)據(jù)的方法
- PHP socket 模擬POST 請(qǐng)求實(shí)例代碼
- php模擬post上傳圖片實(shí)現(xiàn)代碼
- PHP模擬post提交數(shù)據(jù)方法匯總
- 利用PHP fsockopen 模擬POST/GET傳送數(shù)據(jù)的方法
- php模擬post提交數(shù)據(jù)的方法
- php curl模擬post提交數(shù)據(jù)示例
- php curl模擬post請(qǐng)求小實(shí)例
- php模擬post提交請(qǐng)求調(diào)用接口示例解析
相關(guān)文章
php實(shí)現(xiàn)的返回?cái)?shù)據(jù)格式化類實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)的返回?cái)?shù)據(jù)格式化類及其應(yīng)用實(shí)例,包括針對(duì)XML、JSON等的格式化,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-09-09PHP使用SOAP擴(kuò)展實(shí)現(xiàn)WebService的方法
這篇文章主要介紹了PHP使用SOAP擴(kuò)展實(shí)現(xiàn)WebService的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了SOAP擴(kuò)展的原理及實(shí)現(xiàn)WebService的相關(guān)技巧,需要的朋友可以參考下2016-04-04PHP file_get_contents 函數(shù)超時(shí)的幾種解決方法
在使用file_get_contents函數(shù)的時(shí)候,經(jīng)常會(huì)出現(xiàn)超時(shí)的情況,在這里要通過查看一下錯(cuò)誤提示,看看是哪種錯(cuò)誤,比較常見的是讀取超時(shí),這種情況大家可以通過一些方法來盡量的避免或者解決。2009-07-07PHP讀取XML文件的方法實(shí)例總結(jié)【DOMDocument及simplexml方法】
這篇文章主要介紹了PHP讀取XML文件的方法,結(jié)合實(shí)例形式總結(jié)分析了php基于DOMDocument及simplexml方法針對(duì)xml文件的載入、讀取等相關(guān)操作技巧,需要的朋友可以參考下2019-09-09php調(diào)用C代碼的實(shí)現(xiàn)方法
本篇文章主要是對(duì)php調(diào)用C代碼的實(shí)現(xiàn)方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-03-03php中0,null,empty,空,false,字符串關(guān)系的詳細(xì)介紹
本篇文章是對(duì)php中0,null,empty,空,false,字符串關(guān)系進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06