cURL:PHP拒绝连接,但是从命令行

时间:2017-05-10 17:11:58

标签: php curl https connection-refused

我有一个奇怪的cURL行为。

当我尝试使用PHP函数发出请求时,如下所示:

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://<url_here>",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => "<body here>",
    CURLOPT_HTTPHEADER => array(
        "content-type: application/xml"
    ),
));

我得到Failed to connect to <url_here> port 443: Connection refused

但是当我尝试从命令行(在PHP脚本所在的服务器上)进行完全相同的调用时,我得到了一个有效的响应。所以,环境很好,没有什么能阻挡443端口。

此外,当我在另一台服务器上运行相同的PHP代码时,它也可以运行。

某些PHP配置选项是否可能阻止cURL工作?或者我应该检查别的什么?

感谢。

输出本地curl_version()

array(9) {
  ["version_number"]=>
  int(470784)
  ["age"]=>
  int(3)
  ["features"]=>
  int(968605)
  ["ssl_version_number"]=>
  int(0)
  ["version"]=>
  string(6) "7.47.0"
  ["host"]=>
  string(19) "x86_64-pc-linux-gnu"
  ["ssl_version"]=>
  string(14) "OpenSSL/1.0.2g"
  ["libz_version"]=>
  string(5) "1.2.8"
  ["protocols"]=>
  array(21) {
    [0]=>
    string(4) "dict"
    [1]=>
    string(4) "file"
    [2]=>
    string(3) "ftp"
    [3]=>
    string(4) "ftps"
    [4]=>
    string(6) "gopher"
    [5]=>
    string(4) "http"
    [6]=>
    string(5) "https"
    [7]=>
    string(4) "imap"
    [8]=>
    string(5) "imaps"
    [9]=>
    string(4) "ldap"
    [10]=>
    string(5) "ldaps"
    [11]=>
    string(4) "pop3"
    [12]=>
    string(5) "pop3s"
    [13]=>
    string(4) "rtmp"
    [14]=>
    string(4) "rtsp"
    [15]=>
    string(3) "smb"
    [16]=>
    string(4) "smbs"
    [17]=>
    string(4) "smtp"
    [18]=>
    string(5) "smtps"
    [19]=>
    string(6) "telnet"
    [20]=>
    string(4) "tftp"
  }
}

输出本地curl -V

curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP UnixSockets

输出服务器curl_version()

array(9) {
  ["version_number"]=>
  int(462597)
  ["age"]=>
  int(2)
  ["features"]=>
  int(1597)
  ["ssl_version_number"]=>
  int(0)
  ["version"]=>
  string(6) "7.15.5"
  ["host"]=>
  string(23) "x86_64-redhat-linux-gnu"
  ["ssl_version"]=>
  string(15) " OpenSSL/0.9.8b"
  ["libz_version"]=>
  string(5) "1.2.3"
  ["protocols"]=>
  array(9) {
    [0]=>
    string(4) "tftp"
    [1]=>
    string(3) "ftp"
    [2]=>
    string(6) "telnet"
    [3]=>
    string(4) "dict"
    [4]=>
    string(4) "ldap"
    [5]=>
    string(4) "http"
    [6]=>
    string(4) "file"
    [7]=>
    string(5) "https"
    [8]=>
    string(4) "ftps"
  }
}

2 个答案:

答案 0 :(得分:0)

你忽略了提供端口......

检查以下更正后的代码:

  <!-- Player -->
  <a-entity camera="userHeight: 1.6"
            universal-controls="movementControls: checkpoint"
            checkpoint-controls="mode: teleport">
    <a-entity cursor
              position="0 0 -1"
              geometry="primitive: ring; radiusInner: 0.02; radiusOuter: 0.03;"
              material="color: #CCC; shader: flat;">
    </a-entity>
  </a-entity>

  <!-- Terrain -->
  <a-grid></a-grid>

  <!-- Checkpoints -->
  <a-entity position="1 0 1">
    <a-cylinder checkpoint="offset: 0 1.6 0" radius="1" height="0.1" position="0 0 -5.2" color="#39BB82"></a-cylinder>
    <a-cylinder checkpoint="offset: 0 1.6 0" radius="1" height="0.1" position="3 0 0" color="#39BB82"></a-cylinder>
    <a-cylinder checkpoint="offset: 0 1.6 0" radius="1" height="0.1" position="-3 0 0" color="#39BB82"></a-cylinder>
  </a-entity>

(参见this post也......)

答案 1 :(得分:0)

感谢Alex Blexcomment我为PHP cURL启用了详细输出,并使用-vvv选项运行命令行cURL。

它让我看到,命令行请求实际上是通过代理发送的,而PHP cURL试图直接调用,但失败了。

然后我使用CURLOPT_PROXY选项作为我的PHP请求,它也开始工作。