PHP stream_context_create()函数的使用示例

这篇文章主要介绍了PHP stream_context_create()函数的使用示例,stream_context_create()函数是用来 创建打开文件的上下文件选项,用于fopen(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程,需要的朋友可以参考下

stream_context_create()函数是用来 创建打开文件的上下文件选项 ,用于fopen(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。

比如说,上篇php教程中gd库实现下载网页所有图片中,第10行:

利用了stream_context_create()设置代理服务器:

  1. //设置代理服务器
  2. $opts = array('http'=>array('request_fulluri'=>true));
  3. $context = stream_context_create($opts);
  4. $content = file_get_contents($url,false,$context);

利用了stream_context_create()设置超时时间:

  1. $opts = array(
  2. 'http'=>array(
  3. 'method'=>"GET",
  4. 'timeout'=>60,
  5. )
  6. );
  7. $context = stream_context_create($opts);
  8. $html =file_get_contents('https://www.phpfensi.com', false, $context);