PHP stream_context_create()函数的使用示例

前端技术 2023/09/04 PHP

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

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

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

复制代码 代码如下:

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

利用了stream_context_create()设置超时时间:
复制代码 代码如下:

 $opts = array(
    \'http\'=>array(
    \'method\'=>\"GET\",
    \'timeout\'=>60,
  )
);
$context = stream_context_create($opts);
$html =file_get_contents(\'http://www.phpstudy.net\', false, $context);

本文地址:https://www.stayed.cn/item/9569

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。