小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

PHP函數(shù)補(bǔ)完:stream

 yliu277 2016-01-18

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

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

01$data = array(
02    'foo'=>'bar',
03    'baz'=>'boom',
04    'site'=>'www.',
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 // 超時(shí)時(shí)間(單位:s)
16    )
17);
18 
20$context = stream_context_create($options);
21$result = file_get_contents($url, false, $context);
22 
23echo $result;

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

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

運(yùn)行結(jié)果為:

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

一些要點(diǎn)講解:

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

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

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)建的上下文選項(xiàng)即可用于流(stream),也可用于文件系統(tǒng)(file system)。對(duì)于像 file_get_contents、file_put_contents、readfile直接使用文件名操作而沒(méi)有文件句柄的函數(shù)來(lái)說(shuō)更有用。stream_context_create增加header頭只是一部份功能,還可以定義代理、超時(shí)等。這使得訪問(wèn)web的功能不弱于curl。

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

5. stream_context_create 還能通過(guò)增加 timeout 選項(xiàng)解決file_get_contents超時(shí)處理:

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('http://www.', false, $context);
11 
12//fopen輸出文件指針處的所有剩余數(shù)據(jù):
13//fpassthru($fp); //fclose()前使用

延伸閱讀

此文章所在專(zhuān)題列表如下:

  1. PHP函數(shù)補(bǔ)完:get_magic_quotes_gpc()
  2. PHP函數(shù)補(bǔ)完:error_reporting()
  3. PHP函數(shù)補(bǔ)完:preg_match()
  4. PHP函數(shù)補(bǔ)完:urlencode()
  5. PHP函數(shù)補(bǔ)完:array_multisort()
  6. PHP函數(shù)補(bǔ)完:array_splice()
  7. PHP函數(shù)補(bǔ)完:isset()
  8. PHP函數(shù)補(bǔ)完:getenv()
  9. PHP函數(shù)補(bǔ)完:header()
  10. PHP函數(shù)補(bǔ)完:mysql_num_rows()
  11. PHP函數(shù)補(bǔ)完:list()
  12. PHP函數(shù)補(bǔ)完:mysql_query()
  13. PHP函數(shù)補(bǔ)完:mysql_fetch_array()
  14. PHP函數(shù)補(bǔ)完:number_format()
  15. PHP函數(shù)補(bǔ)完:explode()
  16. PHP函數(shù)補(bǔ)完:call_user_func()
  17. PHP函數(shù)補(bǔ)完:ImageCopyResamples()
  18. PHP函數(shù)補(bǔ)完:import_request_variables()
  19. PHP函數(shù)補(bǔ)完:parse_url()
  20. PHP函數(shù)補(bǔ)完:移除HTML標(biāo)簽strip_tags()
  21. PHP函數(shù)補(bǔ)完:輸出數(shù)組結(jié)構(gòu)與內(nèi)容var_dump()
  22. PHP函數(shù)補(bǔ)完:var_export()
  23. PHP函數(shù)補(bǔ)完:判斷變量是否為數(shù)字is_numeric()
  24. PHP函數(shù)補(bǔ)完:session_name()
  25. PHP函數(shù)補(bǔ)完:session_id()
  26. PHP函數(shù)補(bǔ)完:nl2br()與nl2p()函數(shù)
  27. PHP函數(shù)補(bǔ)完:shuffle()取數(shù)組若干個(gè)隨機(jī)元素
  28. PHP函數(shù)補(bǔ)完:http_build_query()構(gòu)造URL字符串
  29. PHP函數(shù)補(bǔ)完:stream_context_create()模擬POST/GET

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多