PHP cURL没有看到/ etc / hosts文件 [英] PHP cURL doesn't see the /etc/hosts file

查看:169
本文介绍了PHP cURL没有看到/ etc / hosts文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦使PHP cURL库识别我在 / etc / hosts 文件中创建的别名。

I'm having troubles on making the PHP cURL library recognize the alias I created in my /etc/hosts file.

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

This is what I have in my /etc/hosts file right now:

192.168.0.20 www.example.dev

.0.20)Apache配置为在 example.dev 域上运行虚拟主机。
如果我在我的浏览器上测试,但是使用PHP cURL不起作用,别名就可以工作。

On the other side (192.168.0.20) Apache is configured to run the virtual host on the example.dev domain. The alias works if I test it on my browser but with PHP cURL just doesn't work.

hosts 文件位于两台机器上(192.168.0.10< = PHP cli,192.168.0.20< = Apache)。

The hosts file is on both machines (192.168.0.10 <= PHP cli, 192.168.0.20 <= Apache).

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

For the sake of completeness this is the PHP code I'm using.

        $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);

注意:此问题类似于这是一个,但PHP相关。

Note: This issue seems like this one but PHP related.

推荐答案

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

Solved using this url "http://192.168.0.20/" instead of "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);

这篇关于PHP cURL没有看到/ etc / hosts文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆