php發(fā)送post請(qǐng)求的三種方法
更新時(shí)間:2014年02月11日 09:16:22 投稿:zxhpj
這篇文章主要介紹了php發(fā)送post請(qǐng)求的三種方法,分別使用curl、file_get_content、fsocket來(lái)實(shí)現(xiàn)post提交數(shù)據(jù),需要的朋友可以參考下
方法一:
/**
* 發(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.dhdzp.com', $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.dhdzp.com/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í)現(xiàn)可逆加密算法和解密分享
對(duì)于大部分密碼加密,我們可以采用md5、sha1等方法??梢杂行Х乐箶?shù)據(jù)泄露,但是這些方法僅適用于無(wú)需還原的數(shù)據(jù)加密。對(duì)于需要還原的信息,則需要采用可逆的加密解密算法,下面一組PHP函數(shù)是實(shí)現(xiàn)此加密解密的方法2014-01-01
Yii2框架dropDownList下拉菜單用法實(shí)例分析
這篇文章主要介紹了Yii2框架dropDownList下拉菜單用法,結(jié)合實(shí)例形式分析了Yii框架中dropDownList下拉菜單的定義、實(shí)現(xiàn)方法與使用技巧,需要的朋友可以參考下2016-07-07
php實(shí)現(xiàn)大文件斷點(diǎn)續(xù)傳下載實(shí)例代碼
這篇文章主要介紹了php實(shí)現(xiàn)大文件斷點(diǎn)續(xù)傳下載實(shí)例,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
php檢測(cè)用戶是否用手機(jī)(Mobile)訪問(wèn)網(wǎng)站的類
php檢測(cè)用戶是否用手機(jī)(Mobile)訪問(wèn)網(wǎng)站的類,引入這個(gè)類就可以直接使用了,程序基于MIT License協(xié)議開源,使用方法在下面2014-01-01
php+layui數(shù)據(jù)表格實(shí)現(xiàn)數(shù)據(jù)分頁(yè)渲染代碼
今天小編就為大家分享一篇php+layui數(shù)據(jù)表格實(shí)現(xiàn)數(shù)據(jù)分頁(yè)渲染代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
解決在laravel中auth建立時(shí)候遇到的問(wèn)題
今天小編就為大家分享一篇解決在laravel中auth建立時(shí)候遇到的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10

