【问题标题】:curl (7): Failed to connect to localhost port 8000: Connection refusedcurl (7): 无法连接到 localhost 端口 8000: 连接被拒绝
【发布时间】:2019-02-07 14:56:48
【问题描述】:

您好,我正在测试端点,当我运行 php testing.php 时出现此错误。任何帮助将不胜感激。

Fatal error: Uncaught exception 'Guzzle\Http\Exception\CurlException' with message '[curl] 7: Failed to connect to localhost port 8000: Connection refused [url] http://localhost:8000/api/programmers' guzzle\guzzle\src\Guzzle\Http\Curl\CurlMulti.php:359 ...

我也试过 127.0.0.1 还是不行

这里是 testing.php

<?php

require __DIR__.'/vendor/autoload.php';

use Guzzle\Http\Client;

// create our http client (Guzzle)
$client = new Client('http://localhost:8000', array(
    'request.options' => array(
        'exceptions' => false,
    )
));

$nickname = 'ObjectOrienter'.rand(0, 999);
$data = array(
    'nickname' => $nickname,
    'avatarNumber' => 5,
    'tagLine' => 'a test dev!'
);

$request = $client->post('/api/programmers', null, json_encode($data));
$response = $request->send();

echo $response;
echo "\n\n";

【问题讨论】:

  • 您认为我们可以帮助解决连接被拒绝错误吗?这是服务器问题,与编程无关。服务器明确拒绝连接,您需要更改它。

标签: php symfony guzzle


【解决方案1】:

http://localhost:8000http://127.0.0.1:8000 都不起作用时,我也面临同样的问题。我使用我的网络 IP 解决了它,比如

192.168.0.3:8000

【讨论】:

  • 是公网/私网的问题吗? curl无权私网发送请求?
  • 是的,好像是这样
  • 我意识到 localhost:8000 不起作用是因为我使用的是 Docker 容器,并且在内部它们不会将彼此视为 localhost,而是将它们视为特定的本地 IP 地址(例如 172.. )。
  • 无法连接到 localhost:9150
【解决方案2】:

如果您使用的是 linux,请确保服务器正在使用

/etc/init.d/httpd status

【讨论】:

    【解决方案3】:

    如果你像这样运行 localhost:

    php -S 本地主机:8000

    然后在 vagrant 等不同的环境中运行 curl,然后 vagrant 上的 localhost 与本地计算机上的 localhost 不同。

    【讨论】:

      【解决方案4】:

      检查您的 localhost:8000 服务器是否正在运行。

      如果不是,请从终端使用以下命令启动它。

      转到项目的根文件夹并输入,

      php -S localhost:8000 -t .\public
      

      【讨论】:

        猜你喜欢
        • 2017-05-12
        • 1970-01-01
        • 2021-01-27
        • 2017-11-09
        • 2020-06-26
        • 2019-02-19
        • 2019-01-07
        • 1970-01-01
        • 2021-05-23
        相关资源
        最近更新 更多