将get_headers与代理一起使用 [英] Using get_headers with a proxy
本文介绍了将get_headers与代理一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为了检查URL是否是图像,我使用PHP函数get_headers
.在正常情况下,效果很好.
In order to check if an URL is an image, I use the PHP function get_headers
. In normal conditions, it works very well.
但是当我落后于代理服务器时,它将导致超时异常.我在file_put_contents
上也遇到了同样的问题,但是我通过添加上下文参数解决了它.但是,get_headers
函数没有类似的参数.
But when I'm behind a proxy, it causes a timeout exception. I had the same problem with file_put_contents
but I solved it by adding a context parameter. However, the get_headers
function hasn't a similar argument.
你知道该怎么做吗?
推荐答案
使用stream_context_set_default
函数.
此博客帖子后面的PHP函数get_headers和file_get_contents解释了如何使用它.这是该页面上的代码.
This blog post explains how to use it. Here is the code from that page.
<?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);
?>
这篇关于将get_headers与代理一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文