Guzzle请求/ cURL错误6:无法解析主机


Laravel - Guzzle Request / cURL error 6: Could not resolve host

我尝试向Github API发出API请求,只是为了测试。我在我的Laravel 5.1 APP上安装了最新的Guzzle版本("Guzzle/Guzzle":"^3.9")。在我的routes.php中,我有以下代码:

Route::get('guzzle/{username}', function($username) {
    $client = new Client([
        'base_uri' => 'https://api.github.com/users/',
    ]);
    $response = $client->get("/users/$username");
    dd($response);
});

如果我现在访问URL域。dev/github/kayyyy,我得到错误cURL error 6: Could not resolve host: users

为什么我得到这个错误?

如果我访问https://api.github.com/users/kayyyy,我可以看到json输出。

我也在使用Homestead/Vagrant,这可能是主机无法解决的问题吗?

编辑如果我尝试不使用base_uri,它可以工作。

Route::get('guzzle/{username}', function($username) {
   $client = new GuzzleHttp'Client();
    $response = $client->get("https://api.github.com/users/$username");
    dd($response);
});

为什么呼叫$client->get->()->send() ?特别是,为什么要在末尾链接send()方法?在演示看似相同的操作时,文档没有附加send()方法:

http://guzzle.readthedocs.org/en/latest/quickstart.html创建客户机

还有,你考虑过上面引用的手册页上的这句话的含义吗?

当一个相对URI提供给客户端时,客户端将使用RFC 3986第2节中描述的规则将基本URI与相对URI组合在一起。

实际上在单引号内不可能有变量插值。这意味着您当前正在调用users/$username,并且$username变量没有被其值替换。

为了得到它,你应该以以下方式之一使用它:

$response = $client->get("users/$username")->send();
$response = $client->get('users/' . $username)->send();

我个人更喜欢第二个,因为它被认为更快。

好吧,我解决了,我犯了一个愚蠢的错误。我使用new Client

当然应该是new GuzzleHttp'Client

因为它只是为了测试在我的routes.php我没有Namespace

谢谢大家的帮助。

您的Laravel安装程序非常过时。获得最新版本的唯一方法是删除并重新安装:

  1. composer global remove laravel/installer
  2. composer global require laravel/installer

感谢Paratronhttps://github.com/googleapis/google-api-php-client/issues/1184#issuecomment-355295789在我的例子中,在cakephp 3.8 &docker 19.03.5,由于一些网络问题,我面临curl错误。我重新启动了我的蛋糕服务器docker容器。