php發(fā)送post請(qǐng)求的三種方法
方法一:
/** * 發(fā)送post請(qǐng)求 * @param string $url 請(qǐng)求地址 * @param array $post_data post鍵值對(duì)數(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 // 超時(shí)時(shí)間(單位: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://www.dbjr.com.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://www.dbjr.com.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獲取通過(guò)http協(xié)議post提交過(guò)來(lái)xml數(shù)據(jù)及解析xml
- php發(fā)送get、post請(qǐng)求的6種方法簡(jiǎn)明總結(jié)
- PHP防止post重復(fù)提交數(shù)據(jù)的簡(jiǎn)單例子
- PHP函數(shù)分享之curl方式取得數(shù)據(jù)、模擬登陸、POST數(shù)據(jù)
- php過(guò)濾所有惡意字符(批量過(guò)濾post,get敏感數(shù)據(jù))
- php發(fā)送post請(qǐng)求函數(shù)分享
- php curl post 時(shí)出現(xiàn)的問(wèn)題解決
- php curl模擬post提交數(shù)據(jù)示例
- php中用socket模擬http中post或者get提交數(shù)據(jù)的示例代碼
- php以post形式發(fā)送xml的方法
相關(guān)文章
php設(shè)計(jì)模式之享元模式分析【星際爭(zhēng)霸游戲案例】
這篇文章主要介紹了php設(shè)計(jì)模式之享元模式,結(jié)合星際爭(zhēng)霸游戲案例形式分析了PHP享元模式的相關(guān)原理與使用技巧,需要的朋友可以參考下2020-03-03PHP實(shí)現(xiàn)一個(gè)多功能購(gòu)物網(wǎng)站的案例
下面小編就為大家?guī)?lái)一篇PHP實(shí)現(xiàn)一個(gè)多功能購(gòu)物網(wǎng)站的案例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09Zend Framework教程之視圖組件Zend_View用法詳解
這篇文章主要介紹了Zend Framework教程之視圖組件Zend_View用法,較為詳細(xì)的分析了試圖組件Zend_View原理并結(jié)合實(shí)例形式分析了Zend_View的使用技巧,需要的朋友可以參考下2016-03-03Laravel框架Eloquent ORM簡(jiǎn)介、模型建立及查詢數(shù)據(jù)操作詳解
這篇文章主要介紹了Laravel框架Eloquent ORM簡(jiǎn)介、模型建立及查詢數(shù)據(jù)操作,結(jié)合實(shí)例形式詳細(xì)分析了laravel框架Eloquent ORM基本概念、原理、模型建立及數(shù)據(jù)查詢等相關(guān)使用技巧,需要的朋友可以參考下2019-12-12PHP實(shí)現(xiàn)用session來(lái)實(shí)現(xiàn)記錄用戶登陸信息
在本篇文章里我們給大家分享了關(guān)于PHP如何用session來(lái)實(shí)現(xiàn)記錄用戶登陸信息的知識(shí)點(diǎn),有興趣的朋友們參考下。2018-10-10PHP樹(shù)-不需要遞歸的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇PHP樹(shù)-不需要遞歸的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06