当前位置:Gxlcms > PHP教程 > cURL无法获取本地其他虚拟主机的内容

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

时间:2021-07-01 10:21:17 帮助过:26人阅读

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

hosts:
127.0.0.1   a.cn
127.0.0.1   b.cn

在虚拟主机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包含下面的内容)

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

按照stackoverflow更改的代码

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

回复内容:

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

hosts:
127.0.0.1   a.cn
127.0.0.1   b.cn

在虚拟主机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包含下面的内容)

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

按照stackoverflow更改的代码

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

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

人气教程排行