PHP curl 在命令行版本工作时被拒绝


php curl gets refused while command line version is working

我是新来写的,但在过去的几年里读了很多答案,所以首先:谢谢你到目前为止给我的很多帮助!所以,这就是今天让我发疯的原因,在任何地方都找不到答案:

我有一个网络服务(一个Openfire插件(和一个基于LAMP的后台(在一个单独的盒子上(,它可以远程管理Openfire。将 Openfire 迁移到新的(第 3 个(盒子后,后台代码停止工作。下面是一个片段:

<?php
    header("Content-Type: text/plain");
    $url = "http://gbbackup.dyndns.org:9090/plugins/goldsteinAdmin/goldsteinadmin"; echo $url."'n";
    $ch = curl_init($url); echo "handle: ".$ch."'n";
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_STDERR, fopen('php://stdout', 'w'));
    $result = curl_exec($ch); echo "Result: ".$result."'n";
    if ($result === false) echo 'cURL error '.curl_errno($ch).': '.curl_error($ch)."'n";
    print_r(curl_getinfo($ch));
    curl_close($ch);
?>

Curl 的详细输出是:

* About to connect() to gbbackup.dyndns.org port 9090 (#0)
*   Trying 81.183.210.206... * Connection refused
* couldn't connect to host
* Closing connection #0

现在奇怪的是,如果我通过命令行尝试,它可以完美运行:

user@login01:~/public_html/gb$ curl -v "http://gbbackup.dyndns.org:9090/plugins/goldsteinAdmin/goldsteinadmin"
* About to connect() to gbbackup.dyndns.org port 9090 (#0)
*   Trying 81.183.210.206... connected
* Connected to gbbackup.dyndns.org (81.183.210.206) port 9090 (#0)
> GET /plugins/goldsteinAdmin/goldsteinadmin HTTP/1.1
> User-Agent: curl/7.21.0 (x86_64-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.15 libssh2/1.2.6
> Host: gbbackup.dyndns.org:9090
> Accept: */*
>
< HTTP/1.1 200 OK
< Expires: Thu, 01 Jan 1970 00:00:00 GMT
< Set-Cookie: JSESSIONID=12e1urcewodgr;Path=/
< Content-Type: application/json;charset=ISO-8859-1
< Transfer-Encoding: chunked
<
{"type":"error","msg":"RequestNotAuthorised"}
* Connection #0 to host gbbackup.dyndns.org left intact
* Closing connection #0

PHP 和命令行 curl 之间有什么区别?我已经删除了所有花哨的额外功能,只是为了测试连接本身,但必须保留一些差异。

我做了更多的测试,结果如下:

  • PHP卷曲到其他网站(例如 google.com(:有效
  • 在我自己的机器上将链接插入Chrome:有效
  • 通过 PHP 系统(( 测试的命令行 curl
  • 不起作用
  • 我也想tcpdump差异,但是在另一个盒子上,我有root权限,PHP和命令行版本都可以工作

似乎只有这两个盒子彼此不喜欢:-(

提前感谢您的帮助!

使用 PHP,您必须单独提供端口(而不是在 URL 中(,例如:

$url = "http://gbbackup.dyndns.org/plugins/goldsteinAdmin/goldsteinadmin";
...
curl_setopt($ch, CURLOPT_PORT, 9090);
...