Home > Backend Development > PHP Tutorial > cURL无法获取本地其他虚拟主机的内容

cURL无法获取本地其他虚拟主机的内容

WBOY
Release: 2016-06-06 20:38:58
Original
1320 people have browsed it

windows 7 64bit, nginx 1.5.8, php 5.4.22,2个虚拟主机,a.cn,b.cn

<code>hosts:
127.0.0.1   a.cn
127.0.0.1   b.cn
</code>
Copy after login
Copy after login

在虚拟主机b.cn,通过curl获取a.cn下某个文件的内容。
结果页面返回504 Gateway Time-out

stackoverflow上有个帖子,我按照他做了,但是依旧没有解决
http://stackoverflow.com/questions/9276114/php-curl-doesnt-see-the-etc-hosts

这是另一个帖子,不过我也没有解决问题
http://stackoverflow.com/questions/3390549/set-curl-to-use-local-virtual-hosts

请问如何解决?

之前的代码(命令行下用php执行,是可以返回结果 php test.php, test.php包含下面的内容)

<code>    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://a.cn' . '/u/ne.php');
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

    $data = curl_exec($ch);
    curl_close($ch);
    echo $data;
</code>
Copy after login
Copy after login

按照stackoverflow更改的代码

<code>    $header = array(
        "Host: a.cn",
        "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",
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1' . '/u/ne.php');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

    $data = curl_exec($ch);
    curl_close($ch);
    echo $data;
</code>
Copy after login
Copy after login

回复内容:

windows 7 64bit, nginx 1.5.8, php 5.4.22,2个虚拟主机,a.cn,b.cn

<code>hosts:
127.0.0.1   a.cn
127.0.0.1   b.cn
</code>
Copy after login
Copy after login

在虚拟主机b.cn,通过curl获取a.cn下某个文件的内容。
结果页面返回504 Gateway Time-out

stackoverflow上有个帖子,我按照他做了,但是依旧没有解决
http://stackoverflow.com/questions/9276114/php-curl-doesnt-see-the-etc-hosts

这是另一个帖子,不过我也没有解决问题
http://stackoverflow.com/questions/3390549/set-curl-to-use-local-virtual-hosts

请问如何解决?

之前的代码(命令行下用php执行,是可以返回结果 php test.php, test.php包含下面的内容)

<code>    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://a.cn' . '/u/ne.php');
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

    $data = curl_exec($ch);
    curl_close($ch);
    echo $data;
</code>
Copy after login
Copy after login

按照stackoverflow更改的代码

<code>    $header = array(
        "Host: a.cn",
        "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",
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1' . '/u/ne.php');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

    $data = curl_exec($ch);
    curl_close($ch);
    echo $data;
</code>
Copy after login
Copy after login

测试了下,代码没有问题。
504错误可能是web服务器的事情,具体信息用 curl_error() 查看下。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template