问题描述
我正在尝试使用fopen从另一个网站读取远程文件.我想使用代理来做到这一点,据我所知:
I'm trying to use fopen to read a remote file from another website. I want to use a proxy to do this, and as far as I know I can do:
$context = stream_context_create(array(
'http' => array(
'proxy' => 'tcp://192.168.10.10:80' // The proxy server address and port
),
));
$file = fopen($url, 'r', false, $context)
但是有没有办法使用该代理的用户名和密码进行身份验证?还是我仅限于使用公共代理?
but is there a way to authenticate using a username and password for that proxy? Or am I limited to having to use public proxies?
此外,代理服务器定义为tcp://192.168.10.10:80
.如果我想使用HTTP代理,是否可以随意将其更改为http://192.168.10.10:80
?
Also, the proxy definition is tcp://192.168.10.10:80
. If I wanted to use a HTTP proxy, am I free to just change it to http://192.168.10.10:80
?
谢谢.
推荐答案
您可以使用$context中的rel ="nofollow">代理授权(如果代理需要身份验证).示例来自 PHP.Net :
You can use the Proxy-Authorization in the $context
if the proxy requires authentication.Example from PHP.Net:
$opts= array(
'http' => array(
'proxy' => 'tcp://proxyip:8080',
'header' => array(
"Proxy-Authorization: Basic $auth"
)
)
);
$context = stream_context_create($opts);
此外,我认为您不能将tcp://192.168.10.10:80
中的tcp更改为http,因为代理所做的是代表客户端与另一台服务器建立TCP连接,然后在客户端与服务器之间来回路由所有流量.显然,它与TCP(有时为UDP)而不是HTTP有关.Http代理只是了解" HTTP协议传递的流量.
Also,I think you cannot change tcp to http in tcp://192.168.10.10:80
since what the proxy does is establishing a TCP connection to another server on behalf of a client then route all the traffic back and forth between the client and the server.Obviously it concerns with TCP(sometimes UDP) instead of HTTP.Http proxies just "understand" the traffic delivered by HTTP protocol.
这篇关于在fopen中使用代理服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!