PHP cURL 看不到 /etc/hosts 文件
我在让 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 cli、192.168.0.20 <= Apache)。
为了完整起见,这是我正在使用的 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);
注意:这个问题看起来像这个 但 PHP 相关。
I'm having troubles on making the PHP cURL library recognize the alias I created in my /etc/hosts
file.
This is what I have in my /etc/hosts
file right now:
192.168.0.20 www.example.dev
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.
The hosts
file is on both machines (192.168.0.10 <= PHP cli, 192.168.0.20 <= Apache).
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);
Note: This issue seems like this one but PHP related.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用此网址“http://192.168.0.20/”而不是“http://www.example.dev”解决了
此外还需要“Host”标头...
Solved using this url "http://192.168.0.20/" instead of "http://www.example.dev"
Also the "Host" header is needed...
您可能在该 apache 服务器上运行 PHP,但那里没有主机文件条目。
You probably run PHP on that apache server, and there you don't have the hosts file entry.