【问题标题】:curl Failed to connect Connection refusedcurl 连接失败 连接被拒绝
【发布时间】:2019-08-22 03:50:06
【问题描述】:

我正在尝试在安装了 wamp 的服务器上连接 curl,但连接被拒绝 Failed to connect to centrala.ratt.ro port 47654: Connection refused我不知道为什么。如果我在浏览器中访问链接它可以工作,使用 php curl 它不工作。这是我的代码:

$url = "http://centrala.ratt.ro:47654/webhook/coordonate.php"; 



$ch = curl_init();
curl_setopt($ch, CURLOPT_PORT, 47654);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode ($result));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, 0);

// This should be the default Content-type for POST requests
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/json"));

$rezult = curl_exec($ch);
echo $rezult;
if(curl_exec($ch) === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
else
{
    echo 'Operation completed without any errors';
}

curl_close($ch);

【问题讨论】:

    标签: php curl server wamp


    【解决方案1】:

    看了https://gustavostraube.wordpress.com/2016/08/31/debugging-requests-with-curl/;

    /*
     * We're going to use the output buffer to store the debug info.
     */
    ob_start();
    $out = fopen('php://output', 'w');
    
    $handler = curl_init($url);
    
    /*
     * Here we set the library verbosity and redirect the error output to the 
     * output buffer.
     */
    curl_setopt($handler, CURLOPT_VERBOSE, true);
    curl_setopt($handler, CURLOPT_STDERR, $out);
    curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($handler);
    fclose($out);
    /*
     * Joining debug info and response body.
     */
    $data = ob_get_clean();
    $data .= PHP_EOL . $response . PHP_EOL;
    echo $data;
    

    查看/发布 $out 的输出以确定后续步骤;如果可以,您应该尝试在命令提示符下使用“-vvv”选项运行 curl 以获得类似的输出。

    【讨论】:

    • curl -vvv POST http://centrala.ratt.ro:47654 * getaddrinfo(3) failed for POST:80 * Couldn't resolve host 'POST' * Closing connection #0 curl: (6) Couldn't resolve host 'POST' * About to connect() to centrala.ratt.ro port 47654 (#0) * Trying 86.125.113.237... Connection refused * couldn't connect to host * Closing connection #0 curl: (7) couldn't connect to host
    • 尝试:curl -vvv -X POST -H "Content-Type: application/json" -d "{ \"key1\": \"value1\" }" http://centrala.ratt.ro:47654/webhook/coordonate.php(在 Windows 上);现在我看到它返回一个 200 结果代码而不返回其他内容
    • 来自 Windows 上的命令它正在工作。我的脚本有什么问题?
    • 如果它可以在 Windows 上运行,并且您仍然从 PHP 获得 Connection refused,请尝试按照上述方法添加 $out。这应该提供一些关于发生了什么的想法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    相关资源
    最近更新 更多