【问题标题】:Using proxy with file_get_contents将代理与 file_get_contents 一起使用
【发布时间】:2013-01-10 04:58:33
【问题描述】:

我正在从网站(例如 x.com)获取我的应用程序数据。 我使用 php 函数 file_get_contents() 来获取数据。 可以肯定的是,我的服务器的 ip 地址将显示在 x.com 的日志中。 有什么方法可以在不使用代理的情况下隐藏我的服务器 IP?

如果我有代理,如何将它与 file_get_contents() 一起使用?

我需要同时使用 HTTP POST 和 HTTP GET 方法发送请求

【问题讨论】:

  • file_get_contents 带有一个参数 stream_or_context 你可以把你的代理放进去看看这个php.net/manual/en/context.http.php
  • 我试过了,但我得到以下错误,内容长度未指定。所以我给出了参数内容长度和偏移量。但它仍然不起作用。
  • 服务器端总是知道你的IP,否则它不能和你交换数据(我的意思是http)。在某些情况下,您可以放置​​像 X-Forward-For: fakeip 这样的标头来告诉服务器您的 fakeip,但是服务器端将确定要使用哪个 ip
  • @J你用的是什么代理
  • @J 想为你做一个

标签: php proxy file-get-contents


【解决方案1】:

test.php 使用http://ifconfig.me/ip

代码修改自http://www.php.net/manual/en/function.file-get-contents.php

<?php

// Create a stream
$opts = array(
        'http'=>array(
            'method'=>"GET",
            'header'=>"Accept-language: en\r\n" .
            "Cookie: foo=bar\r\n",
            'proxy' => 'tcp://221.176.14.72:80',
            )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://ifconfig.me/ip', false, $context);

var_dump($file);

【讨论】:

  • 非常感谢..它正在工作..对于发布请求,我必须提供内容类型和长度,对吗?
  • 我在我的服务器上对其进行了测试..并在同一台服务器上请求了一个文件并检查了日志..在日志中我的服务器的 ip 是可见的:(
  • 代理服务器似乎将您的 ip 添加到 X-Forward-For 并且您的服务器使用了它
  • 隐藏基于你的服务器端如何读取你的 ip。如果你知道的话,你可以定制一种方式
【解决方案2】:

完全同意farmer1992。

对于通过 SSL + 代理遇到file_get_contents 问题的任何人,PHP 的 stream_context_create 存在一个已知错误:

https://bugs.php.net/bug.php?id=63519

幸运的是,解决方法很简单。基本上,上下文创建者在将“https”目标 URL 解析为代理和 SSL 配置时会感到困惑。您只需在 SSL 配置中设置 SNI_server_name:

$targetUrl = "https://something.com";
$sniServer = parse_url($targetUrl, PHP_URL_HOST);
$params = array('your'=>'post','params'=>'here');
$ctxConfig = array(
    'http' => array(
        'method' => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded'."\r\n",
        'content' => http_build_query($params),
        'proxy' => 'tcp://12.34.56.78:3128',
        'request_fulluri' => true
    ),
    'ssl' => array( 
        'SNI_enabled' => true,
        'SNI_server_name' => $sniServer
    )
);
$context = stream_context_create($ctxConfig);
file_get_contents($targetUrl,false,$context)

希望这可以节省一些时间!

【讨论】:

  • 谢谢。 $params 只是您打算发送到服务器的 POST 参数。我在上面添加了一个澄清声明。
  • 未定义变量 $domain
  • 谢谢。我已经在上面更正了这一点。以前的 $domain 现在是 $sniServer。
猜你喜欢
  • 2019-01-30
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 2018-11-02
  • 2014-03-06
  • 2016-12-09
  • 2013-06-11
  • 2012-06-08
相关资源
最近更新 更多