php發(fā)送post請求函數(shù)分享
更新時間:2014年03月06日 16:23:33 作者:
這篇文章主要介紹了一個php發(fā)送post請求的函數(shù),開發(fā)中經(jīng)常會用到,需要的朋友可以參考下
復(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請求的6種方法簡明總結(jié)
- PHP防止post重復(fù)提交數(shù)據(jù)的簡單例子
- PHP函數(shù)分享之curl方式取得數(shù)據(jù)、模擬登陸、POST數(shù)據(jù)
- php過濾所有惡意字符(批量過濾post,get敏感數(shù)據(jù))
- php發(fā)送post請求的三種方法
- php curl post 時出現(xiàn)的問題解決
- php curl模擬post提交數(shù)據(jù)示例
- php中用socket模擬http中post或者get提交數(shù)據(jù)的示例代碼
- php以post形式發(fā)送xml的方法
相關(guān)文章
讓CodeIgniter的ellipsize()支持中文截斷的方法
CodeIgniter的Text Helper有一個ellipsize()方法,用來過濾HTML標簽并且截斷文字十分好用。但是它對中文支持的特別不好,在中文中使用就有亂碼出現(xiàn)。這篇文章主要介紹了讓CodeIgniter的ellipsize()支持中文截斷的方法,需要的朋友可以參考下2014-06-06
smarty內(nèi)置函數(shù)capture用法分析
這篇文章主要介紹了smarty內(nèi)置函數(shù)capture用法,實例分析了capture的三種常見用法,需要的朋友可以參考下2015-01-01
php_screw安裝使用教程(另一個PHP代碼加密實現(xiàn))
這篇文章主要介紹了php_screw安裝使用教程,php_screw是另一個PHP代碼加密實現(xiàn),和Zend的encoder類似,需要的朋友可以參考下2014-05-05
jQuery+Ajax+PHP“喜歡”評級功能實現(xiàn)代碼
本文基于jQuery,通過PHP與mysql實現(xiàn)了一個評級功能,是一個簡單的非常好的ajax應(yīng)用實例,需要的朋友可以參考下2015-10-10

