curl (7):连接本地主机端口8000失败:拒绝连接

7

你好,我正在测试端点,但是当我运行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";
4个回答

7

这是公共/私有网络的问题吗?Curl在私有网络中没有发送请求的权限? - Amin Shojaei
是的,看起来是这样的。 - shoeab
我意识到localhost:8000无法工作是因为我正在使用Docker容器,而在内部它们不会将彼此视为localhost,而是作为它们特定的本地IP地址(例如172..)。 - VfP
无法连接到本地主机:9150。 - Harsh Vardhan

2

如果您正在使用Linux,请确保服务器正在运行

/etc/init.d/httpd status

1

检查您的本地主机:8000服务器是否正在运行。

如果没有运行,请使用终端中的以下命令启动它。

进入您项目的根目录并键入以下内容:

php -S localhost:8000 -t .\public

1
如果您像这样运行本地主机:
php -S localhost:8000
然后从不同的环境(如vagrant)运行curl,那么vagrant上的localhost与您的本地计算机上的localhost不同。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接