php發(fā)送post請求的三種方法
更新時間:2014年02月11日 09:16:22 投稿:zxhpj
這篇文章主要介紹了php發(fā)送post請求的三種方法,分別使用curl、file_get_content、fsocket來實現(xiàn)post提交數(shù)據(jù),需要的朋友可以參考下
方法一:
/** * 發(fā)送post請求 * @param string $url 請求地址 * @param array $post_data post鍵值對數(shù)據(jù) * @return string */ function send_post($url, $post_data) { $postdata = http_build_query($post_data); $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type:application/x-www-form-urlencoded', 'content' => $postdata, 'timeout' => 15 * 60 // 超時時間(單位:s) ) ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); return $result; } //使用方法 $post_data = array( 'username' => 'stclair2201', 'password' => 'handan' ); send_post('http://chabaoo.cn', $post_data);
方法二:Socket版本
<?php /** * Socket版本 * 使用方法: * $post_string = "app=socket&version=beta"; * request_by_socket('chajia8.com', '/restServer.php', $post_string); */ function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30) { $socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout); if (!$socket) die("$errstr($errno)"); fwrite($socket, "POST $remote_path HTTP/1.0"); fwrite($socket, "User-Agent: Socket Example"); fwrite($socket, "HOST: $remote_server"); fwrite($socket, "Content-type: application/x-www-form-urlencoded"); fwrite($socket, "Content-length: " . (strlen($post_string) + 8) . ""); fwrite($socket, "Accept:*/*"); fwrite($socket, ""); fwrite($socket, "mypost=$post_string"); fwrite($socket, ""); $header = ""; while ($str = trim(fgets($socket, 4096))) { $header .= $str; } $data = ""; while (!feof($socket)) { $data .= fgets($socket, 4096); } return $data; } ?>
方法三:Curl版本
<?php /** * Curl版本 * 使用方法: * $post_string = "app=request&version=beta"; * request_by_curl('http://chabaoo.cn/restServer.php', $post_string); */ function request_by_curl($remote_server, $post_string) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $remote_server); curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, "jb51.net's CURL Example beta"); $data = curl_exec($ch); curl_close($ch); return $data; } ?>
下面是其他網(wǎ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){//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; } }
大家可以根據(jù)需要選擇適合自己的即可。
您可能感興趣的文章:
- php獲取通過http協(xié)議post提交過來xml數(shù)據(jù)及解析xml
- php發(fā)送get、post請求的6種方法簡明總結(jié)
- PHP防止post重復(fù)提交數(shù)據(jù)的簡單例子
- PHP函數(shù)分享之curl方式取得數(shù)據(jù)、模擬登陸、POST數(shù)據(jù)
- php過濾所有惡意字符(批量過濾post,get敏感數(shù)據(jù))
- php發(fā)送post請求函數(shù)分享
- php curl post 時出現(xiàn)的問題解決
- php curl模擬post提交數(shù)據(jù)示例
- php中用socket模擬http中post或者get提交數(shù)據(jù)的示例代碼
- php以post形式發(fā)送xml的方法
相關(guān)文章
Zend Framework教程之視圖組件Zend_View用法詳解
這篇文章主要介紹了Zend Framework教程之視圖組件Zend_View用法,較為詳細(xì)的分析了試圖組件Zend_View原理并結(jié)合實例形式分析了Zend_View的使用技巧,需要的朋友可以參考下2016-03-03Laravel框架Eloquent ORM簡介、模型建立及查詢數(shù)據(jù)操作詳解
這篇文章主要介紹了Laravel框架Eloquent ORM簡介、模型建立及查詢數(shù)據(jù)操作,結(jié)合實例形式詳細(xì)分析了laravel框架Eloquent ORM基本概念、原理、模型建立及數(shù)據(jù)查詢等相關(guān)使用技巧,需要的朋友可以參考下2019-12-12PHP實現(xiàn)用session來實現(xiàn)記錄用戶登陸信息
在本篇文章里我們給大家分享了關(guān)于PHP如何用session來實現(xiàn)記錄用戶登陸信息的知識點,有興趣的朋友們參考下。2018-10-10