curl (7): 无法连接到本地主机端口 8000:连接被拒绝

curl (7): Failed to connect to localhost port 8000: Connection refused

您好,我正在测试端点,当我 运行 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";

如果您使用 linux,请确保服务器 运行 使用

/etc/init.d/httpd status

如果您是 运行 localhost,就像这样:

php-Slocalhost:8000

然后 运行 从不同的环境卷曲,比如 vagrant 然后你的 vagrant 上的 localhost 与你本地机器上的 localhost 不同。

我遇到了同样的问题,但 http://localhost:8000 or http://127.0.0.1:8000 都不起作用。我用我的网络 IP 解决了它

192.168.0.3:8000

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

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

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

php -S localhost:8000 -t .\public