本文實例講述了PHP使用file_get_contents發(fā)送http請求功能。分享給大家供大家參考,具體如下:
服務器端模擬 POST/GET 等請求,使用 CURL 很容易辦到(例如前面一篇《php使用CURL模擬GET與POST向微信接口提交及獲取數(shù)據(jù)的方法》),那么如果不使用 CURL 庫,又該怎么辦呢?
$data = array(
'test'=>'bar',
'baz'=>'boom',
'site'=>'www.nimip.com',
'name'=>'nimip.com');
$data = http_build_query($data);
//$postdata = http_build_query($data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $data
'timeout' => 60 // 超時時間(單位:s)
)
);
$url = "http://www.testweb.com";
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
其中http://www.testweb.com的代碼為:
$data = $_POST;
print_r( $data );
stream_context_create()
作用:創(chuàng)建并返回一個文本數(shù)據(jù)流并應用各種選項,可用于fopen()
,file_get_contents()
等過程的超時設置、代理服務器、請求方式、頭信息設置的特殊過程。
更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《php curl用法總結》、《PHP網(wǎng)絡編程技巧總結》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結》、《PHP數(shù)據(jù)結構與算法教程》、《php程序設計算法總結》、《PHP運算與運算符用法總結》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
您可能感興趣的文章:- 解決PHP curl或file_get_contents下載圖片損壞或無法打開的問題
- php中file_get_contents()函數(shù)用法實例
- PHP使用fopen與file_get_contents讀取文件實例分享
- PHP中file_get_contents函數(shù)抓取https地址出錯的解決方法(兩種方法)
- php使用file_get_contents(‘php://input‘)和$_POST的區(qū)別實例對比