php發(fā)送post請(qǐng)求函數(shù)分享
更新時(shí)間:2014年03月06日 16:23:33 作者:
這篇文章主要介紹了一個(gè)php發(fā)送post請(qǐng)求的函數(shù),開發(fā)中經(jīng)常會(huì)用到,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
用法如下:
復(fù)制代碼 代碼如下:
//json字符串
$data = "{...}";
//轉(zhuǎn)換成數(shù)組
$data=json_decode($data,true);
$postdata = http_build_query($data);
do_post_request("http://localhost",$postdata);
您可能感興趣的文章:
- php獲取通過http協(xié)議post提交過來xml數(shù)據(jù)及解析xml
- php發(fā)送get、post請(qǐng)求的6種方法簡明總結(jié)
- PHP防止post重復(fù)提交數(shù)據(jù)的簡單例子
- PHP函數(shù)分享之curl方式取得數(shù)據(jù)、模擬登陸、POST數(shù)據(jù)
- php過濾所有惡意字符(批量過濾post,get敏感數(shù)據(jù))
- php發(fā)送post請(qǐng)求的三種方法
- php curl post 時(shí)出現(xiàn)的問題解決
- php curl模擬post提交數(shù)據(jù)示例
- php中用socket模擬http中post或者get提交數(shù)據(jù)的示例代碼
- php以post形式發(fā)送xml的方法
相關(guān)文章
thinkphp5.1 文件引入路徑問題及注意事項(xiàng)
這篇文章主要介紹了thinkphp5.1 文件引入路徑問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
2018-06-06
smarty內(nèi)置函數(shù)capture用法分析
這篇文章主要介紹了smarty內(nèi)置函數(shù)capture用法,實(shí)例分析了capture的三種常見用法,需要的朋友可以參考下
2015-01-01
php_screw安裝使用教程(另一個(gè)PHP代碼加密實(shí)現(xiàn))
這篇文章主要介紹了php_screw安裝使用教程,php_screw是另一個(gè)PHP代碼加密實(shí)現(xiàn),和Zend的encoder類似,需要的朋友可以參考下
2014-05-05
jQuery+Ajax+PHP“喜歡”評(píng)級(jí)功能實(shí)現(xiàn)代碼
本文基于jQuery,通過PHP與mysql實(shí)現(xiàn)了一個(gè)評(píng)級(jí)功能,是一個(gè)簡單的非常好的ajax應(yīng)用實(shí)例,需要的朋友可以參考下
2015-10-10
詳解php實(shí)現(xiàn)頁面靜態(tài)化原理
本篇文章主要介紹了詳解php實(shí)現(xiàn)頁面靜態(tài)化原理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
2017-06-06 
