【问题标题】:PHP cURL doesn't see the /etc/hosts filePHP cURL 看不到 /etc/hosts 文件
【发布时间】:2012-03-05 18:36:29
【问题描述】:

我无法让 PHP cURL 库识别我在 /etc/hosts 文件中创建的别名。

这就是我现在的/etc/hosts 文件中的内容:

192.168.0.20 www.example.dev

另一方面 (192.168.0.20) Apache 被配置为在 example.dev 域上运行虚拟主机。 如果我在我的浏览器上测试别名,但使用 PHP cURL 就不起作用了。

hosts 文件在两台机器上(192.168.0.10 。

为了完整起见,这是我正在使用的 PHP 代码。

        $this->url = 'http://www.example.dev/';
        $this->ch = curl_init();
        $header = array
        (
            "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3",
            "Accept-Encoding: gzip,deflate,sdch",
            "Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4",
            "Cache-Control: max-age=0",
            "Connection: keep-alive",
        );

        $sUserAgent = $this->tor ? UserAgents::getRandom() : UserAgents::CHROME16_LINUX;

        curl_setopt($this->ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($this->ch, CURLOPT_USERAGENT, $sUserAgent);
        curl_setopt($this->ch, CURLOPT_URL, $this->url);
        curl_setopt($this->ch, CURLOPT_HEADER, false);
        curl_setopt($this->ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, (bool) $waitResponse);
        curl_setopt($this->ch, CURLOPT_VERBOSE, (bool) $this->verbose);
        curl_setopt($this->ch, CURLOPT_PORT, $this->port);
        curl_setopt($this->ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($this->ch, CURLOPT_MAXREDIRS, 10);
        curl_setopt($this->ch, CURLOPT_ENCODING, '');
        curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($this->ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($this->ch, CURLOPT_DNS_CACHE_TIMEOUT, 120);
        curl_setopt($this->ch, CURLOPT_COOKIESESSION, true);
        curl_setopt($this->ch, CURLOPT_COOKIEFILE, 'cookie');

        foreach ($this->files as $k => $file) {
            $this->data['_file_' . $k] = '@' . $file;
        }

        curl_setopt($this->ch, CURLOPT_POST, true);
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->data);

        $this->result = curl_exec($this->ch);

注意:这个问题看起来像this one,但与 PHP 相关。

【问题讨论】:

  • 你重启服务器了吗?只是为了确定?
  • 你能发布确切的代码吗?应该没有问题。
  • hosts 文件在正确的机器上吗?你在这台机器上运行 PHP 吗?
  • 我也期望没有问题,你能ping或telnet到它吗?curl配置为使用代理吗?如果是,它将忽略 /etc/hosts
  • hosts文件在所有机器上(192.168.0.10

标签: php curl vhosts


【解决方案1】:

使用此网址“http://192.168.0.20/”而不是“http://www.example.dev”解决

还需要“主机”标头...

$header = array
(
    "Host: www.example.dev", // IMPORTANT
    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3",
    "Accept-Encoding: gzip,deflate,sdch",
    "Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4",
    "Cache-Control: max-age=0",
    "Connection: keep-alive",
);

curl_setopt($this->ch, CURLOPT_URL, 'http://192.168.0.20/');
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $header);

【讨论】:

    【解决方案2】:

    您可能在该 apache 服务器上运行 PHP,但那里没有 hosts 文件条目。

    【讨论】:

    • hosts文件在所有机器上(192.168.0.10
    • @karoly 谢谢!这启发我!我使用 docker 进行测试,容器没有 hosts 文件中的条目!
    猜你喜欢
    • 2017-03-14
    • 1970-01-01
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多