加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 将get_headers与代理一起使用

发布时间:2020-12-13 16:51:15 所属栏目:PHP教程 来源:网络整理
导读:为了检查URL是否是图像,我使用 PHP函数get_headers.在正常情况下,它运作良好. 但是当我在代理后面时,它会导致超时异常.我遇到了与file_put_contents相同的问题,但我通过添加上下文参数解决了这个问题.但是,get_headers函数没有类似的参数. 你知道怎么做吗?
为了检查URL是否是图像,我使用 PHP函数get_headers.在正常情况下,它运作良好.

但是当我在代理后面时,它会导致超时异常.我遇到了与file_put_contents相同的问题,但我通过添加上下文参数解决了这个问题.但是,get_headers函数没有类似的参数.

你知道怎么做吗?

解决方法

使用stream_context_set_default函数.

这blog post解释了如何使用它.这是该页面的代码.

<?php
// Edit the four values below
$PROXY_HOST = "proxy.example.com"; // Proxy server address
$PROXY_PORT = "1234";    // Proxy server port
$PROXY_USER = "LOGIN";    // Username
$PROXY_PASS = "PASSWORD";   // Password
// Username and Password are required only if your proxy server needs basic authentication

$auth = base64_encode("$PROXY_USER:$PROXY_PASS");
stream_context_set_default(
 array(
  'http' => array(
   'proxy' => "tcp://$PROXY_HOST:$PROXY_PORT",'request_fulluri' => true,'header' => "Proxy-Authorization: Basic $auth"
   // Remove the 'header' option if proxy authentication is not required
  )
 )
);

$url = "http://www.pirob.com/";

print_r( get_headers($url) );

echo file_get_contents($url);
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读