免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
PHP函數(shù)補完:stream
userphoto

2016.01.18

關注

有時候,我們需要在服務器端模擬 POST/GET 等請求,也就是在 PHP 程序中去實現(xiàn)模擬,改怎么做到呢?或者說,在 PHP 程序里,給你一個數(shù)組,如何將這個數(shù)組 POST/GET 到另外一個地址呢?當然,使用 CURL 很容易辦到,那么如果不使用 CURL 庫,又該怎么辦呢?其實,在 PHP 里已經(jīng)有相關的函數(shù)實現(xiàn)了,這個函數(shù)就是接下來要講的 stream_context_create()。

直接 show you the code,這是最好的方法:

01$data = array(
02    'foo'=>'bar',
03    'baz'=>'boom',
04    'site'=>'www.nowamagic.net',
05    'name'=>'nowa magic');
06     
07$data = http_build_query($data);
08 
09//$postdata = http_build_query($data);
10$options = array(
11    'http' => array(
12        'method' => 'POST',
13        'header' => 'Content-type:application/x-www-form-urlencoded',
14        'content' => $data
15        //'timeout' => 60 * 60 // 超時時間(單位:s)
16    )
17);
18 
19$url = ";
20$context = stream_context_create($options);
21$result = file_get_contents($url, false, $context);
22 
23echo $result;

http://www.nowamagic.net/test2.php 的代碼為:

1$data = $_POST;
2 
3echo '<pre>';
4print_r( $data );
5echo '</pre>';

運行結果為:

1Array
2(
3    [foo] => bar
4    [baz] => boom
5    [site] => www.nowamagic.net
6    [name] => nowa magic
7)

一些要點講解:

1. 以上程序用到了 http_build_query() 函數(shù),如果需要了解,可以參看 PHP函數(shù)補完:http_build_query()構造URL字符串。

2. stream_context_create() 是用來創(chuàng)建打開文件的上下文件選項的,比如用POST訪問,使用代理,發(fā)送header等。就是創(chuàng)建一個流,再舉一個例子吧:

01$context = stream_context_create(array(
02    'http' => array(
03        'method'  => 'POST',
04        'header'  => sprintf("Authorization: Basic %s\r\n", base64_encode($username.':'.$password)).
05        "Content-type: application/x-www-form-urlencoded\r\n",
06        'content' => http_build_query(array('status' => $message)),
07        'timeout' => 5,
08    ),
09));
10$ret = file_get_contents(', false, $context);

3. stream_context_create創(chuàng)建的上下文選項即可用于流(stream),也可用于文件系統(tǒng)(file system)。對于像 file_get_contents、file_put_contents、readfile直接使用文件名操作而沒有文件句柄的函數(shù)來說更有用。stream_context_create增加header頭只是一部份功能,還可以定義代理、超時等。這使得訪問web的功能不弱于curl。

4. stream_context_create() 作用:創(chuàng)建并返回一個文本數(shù)據(jù)流并應用各種選項,可用于fopen(),file_get_contents()等過程的超時設置、代理服務器、請求方式、頭信息設置的特殊過程。

5. stream_context_create 還能通過增加 timeout 選項解決file_get_contents超時處理:

01$opts = array(
02    'http'=>array(
03    'method'=>"GET",
04    'timeout'=>60,
05  )
06);
07//創(chuàng)建數(shù)據(jù)流上下文
08$context = stream_context_create($opts);
09 
10$html =file_get_contents(', false, $context);
11 
12//fopen輸出文件指針處的所有剩余數(shù)據(jù):
13//fpassthru($fp); //fclose()前使用

延伸閱讀

此文章所在專題列表如下:

  1. PHP函數(shù)補完:get_magic_quotes_gpc()
  2. PHP函數(shù)補完:error_reporting()
  3. PHP函數(shù)補完:preg_match()
  4. PHP函數(shù)補完:urlencode()
  5. PHP函數(shù)補完:array_multisort()
  6. PHP函數(shù)補完:array_splice()
  7. PHP函數(shù)補完:isset()
  8. PHP函數(shù)補完:getenv()
  9. PHP函數(shù)補完:header()
  10. PHP函數(shù)補完:mysql_num_rows()
  11. PHP函數(shù)補完:list()
  12. PHP函數(shù)補完:mysql_query()
  13. PHP函數(shù)補完:mysql_fetch_array()
  14. PHP函數(shù)補完:number_format()
  15. PHP函數(shù)補完:explode()
  16. PHP函數(shù)補完:call_user_func()
  17. PHP函數(shù)補完:ImageCopyResamples()
  18. PHP函數(shù)補完:import_request_variables()
  19. PHP函數(shù)補完:parse_url()
  20. PHP函數(shù)補完:移除HTML標簽strip_tags()
  21. PHP函數(shù)補完:輸出數(shù)組結構與內(nèi)容var_dump()
  22. PHP函數(shù)補完:var_export()
  23. PHP函數(shù)補完:判斷變量是否為數(shù)字is_numeric()
  24. PHP函數(shù)補完:session_name()
  25. PHP函數(shù)補完:session_id()
  26. PHP函數(shù)補完:nl2br()與nl2p()函數(shù)
  27. PHP函數(shù)補完:shuffle()取數(shù)組若干個隨機元素
  28. PHP函數(shù)補完:http_build_query()構造URL字符串
  29. PHP函數(shù)補完:stream_context_create()模擬POST/GET
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
file_get_contents高級用法
cURL超時設置
php多路復用(多線程)
PHP多線程的實現(xiàn)方法詳解
用PHP發(fā)送POST請求
超強功能file_put_contents()函數(shù)(集成了fopen、fwrite、fclose)
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服