【问题标题】:file_get_content() : "No such file or directory" behind a proxyfile_get_content() :代理后面的“没有这样的文件或目录”
【发布时间】:2017-11-14 09:39:26
【问题描述】:

我正在尝试使用 PHP 从 Internet 获取数据。在工作中,我使用了一个受密码保护的代理,这似乎会造成麻烦。

我在 StackOverflow 和其他几个平台上查阅了许多帖子,每个帖子都给了我不同的解决方案,但没有一个可以满足我的需求。

var_dump(stream_get_wrappers());

$opts = array("http" => ['proxy'=>'user:password@webproxy.xxx.intra:####',
                        'request_fulluri' => True]
        );

stream_context_set_default($opts);

$homepage = file_get_contents("www.google.com");
echo $homepage;

按照我之前阅读的一篇文章中的建议,我添加了var_dump(stream_get_wrappers());,返回:[https, ftps, compress.zlib, compress.bzip2, php, file, glob,数据、http、ftp、phar、zip]

我得到的警告如下:

PHP 警告:file_get_contents(www.google.com):打开流失败:没有这样的文件 或第 15 行 /home/user/project/app/connector.php 中的目录

第 15 行是 $homepage = file_get_contents("www.google.com");

我被困在那里太久了,非常感谢任何帮助。

谢谢。


编辑:我在地址的开头添加了“http://”,给了我这个错误:

PHP 警告:file_get_contents(google.com):未能打开流: php_network_getaddresses:getaddrinfo 失败:名称或服务没有 在第 15 行的 /home/user/project/app/connector.php 中已知

由于某种原因,此错误显示了两次,第二次几乎相同,但信息较少。

【问题讨论】:

  • www.google.com 实际上是一个文件名,它确实不存在。如果要执行 HTTP 请求,请确保 URI 以该方案开头。
  • 我不认为 www.google.com 是一个有效的文件名..?
  • @CodeCaster 什么方案? http:// ?这就是我得到的回报:PHP 警告:file_get_contents(google.com):未能打开流:php_network_getaddresses:getaddrinfo 失败:在线/home/user/project/app/connector.php 中的名称或服务未知15
  • @ThePlusProgrammer 总的来说,我是 PHP 新手,我真的不知道我应该做什么。我知道“www.google.com”不是一个有效文件,但是我怎样才能获取该网站的内容呢?
  • 是的,这是一个新错误。您对该错误的研究表明了什么? stackoverflow.com/questions/20064372/…

标签: php http proxy


【解决方案1】:

在搜索了更多之后,这是我想出的:

$auth = base64_encode('login:password');

$opts = array(
    'http' => array(
        'proxy' => 'tcp://proxy_host:proxy_port',
        'request_fulluri' => true,
        'header' => "Proxy-Authorization: Basic $auth",
    ),
);
$context = stream_context_create($opts);

$file = file_get_contents("http://www.google.com", False, $context);
echo $file;

这似乎工作正常。 感谢您的帮助和建议。

【讨论】:

    猜你喜欢
    • 2018-04-11
    • 2021-06-24
    • 2017-04-03
    • 2017-01-04
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多