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

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

WBOY
WBOYOriginal
2016-06-06 20:38:581310Durchsuche

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>

在虚拟主机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>

按照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>

回复内容:

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>

在虚拟主机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>

按照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>

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn