duanping2809 2014-08-22 17:03
浏览 113
已采纳

cURL有效,但PHP cURL无法上网

Trying to diagnose an issue using PHP to cURL to an Internet location on a RedHat Linux server.

cURL is installed and working, and:

<?php var_dump(curl_version()); ?>

shows all the correct information in the output. The issue is I can use PHP to cURL to localhost on the box itself, but not the Internet (see below).

Normally I'd suspect the firewall, but I can cURL from the command line to the Internet without a problem. The box can also update it's own software packages, etc.

What am I missing? My test is:

<?php 
    function http_head_curl($url,$timeout=30)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_NOBODY, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($ch);
        if ($res === false) {
            throw new RuntimeException("cURL exception: ".curl_errno($ch).": ".curl_error($ch));
        }
        return trim($res);
    }

    // Succeeds, displaying headers
    echo(http_head_curl('localhost'));

    // Fails:
    echo(http_head_curl('www.google.com'));
?>
  • 写回答

1条回答 默认 最新

  • douzhi4056 2014-08-24 21:37
    关注

    Are you having DNS resolution issues on the server? It will always be able to resolve localhost but may not be able to resolve www.google.com. Try this:

    var_dump(dns_get_record('www.google.com'));
    

    If DNS if not resolving, you should get:

    array(0) {}
    

    If DNS is working, you should get an array something like this:

    array(6) {
      [0]=>
      array(5) {
        ["host"]=>
        string(14) "www.google.com"
        ["type"]=>
        string(1) "A"
        ["ip"]=>
        string(13) "74.125.201.99"
        ["class"]=>
        string(2) "IN"
        ["ttl"]=>
        int(375)
      }
      [1]=>
      array(5) {
        ["host"]=>
        string(14) "www.google.com"
        ["type"]=>
        string(1) "A"
        ["ip"]=>
        string(14) "74.125.201.105"
        ["class"]=>
        string(2) "IN"
        ["ttl"]=>
        int(375)
      }
      [2]=>
      array(5) {
        ["host"]=>
        string(14) "www.google.com"
        ["type"]=>
        string(1) "A"
        ["ip"]=>
        string(14) "74.125.201.104"
        ["class"]=>
        string(2) "IN"
        ["ttl"]=>
        int(375)
      }
      [3]=>
      array(5) {
        ["host"]=>
        string(14) "www.google.com"
        ["type"]=>
        string(1) "A"
        ["ip"]=>
        string(14) "74.125.201.106"
        ["class"]=>
        string(2) "IN"
        ["ttl"]=>
        int(375)
      }
      [4]=>
      array(5) {
        ["host"]=>
        string(14) "www.google.com"
        ["type"]=>
        string(1) "A"
        ["ip"]=>
        string(14) "74.125.201.103"
        ["class"]=>
        string(2) "IN"
        ["ttl"]=>
        int(375)
      }
      [5]=>
      array(5) {
        ["host"]=>
        string(14) "www.google.com"
        ["type"]=>
        string(1) "A"
        ["ip"]=>
        string(14) "74.125.201.147"
        ["class"]=>
        string(2) "IN"
        ["ttl"]=>
        int(375)
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch