简体   繁体   English

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

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

I try to make an API Request to the Github API just for testing.我尝试向 Github API 发出 API 请求只是为了测试。 I installed the latest Guzzle Version ( "guzzle/guzzle": "^3.9" ) on my Laravel 5.1 APP.我在我的 Laravel 5.1 APP 上安装了最新的 Guzzle 版本 ( "guzzle/guzzle": "^3.9" )。 In my routes.php i have the following Code:在我的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);
});

If i now visit the URL domain.dev/github/kayyyy i get the Error cURL error 6: Could not resolve host: users .如果我现在访问 URL domain.dev/github/kayyyy,我会收到 Error cURL error 6: Could not resolve host: users

Why am i getting this Error?为什么我会收到此错误?

If i visit https://api.github.com/users/kayyyy i can see the json output.如果我访问https://api.github.com/users/kayyyy我可以看到json输出。

I am also using Homestead / Vagrant is this maybe a Problem that the host cant be resolved?我也在使用 Homestead / Vagrant 这可能是主机无法解决的问题吗?

EDIT If i try this without the base_uri it works.编辑如果我在没有 base_uri 的情况下尝试这样做,它就可以工作。

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

Why are you calling $client->get->()->send() ?你为什么打电话给$client->get->()->send() In particular, why are you chaining the send() method at the end?特别是,为什么要在最后链接 send() 方法? The documentation does not append the send() method when demonstrating what seems to be the same action:在演示似乎是相同的操作时,文档没有附加send()方法:

http://guzzle.readthedocs.org/en/latest/quickstart.html#creating-a-client http://guzzle.readthedocs.org/en/latest/quickstart.html#creating-a-client

Also, did you consider the implications of this statement on the above-cited manual page?另外,您是否考虑过上述手册页中此声明的含义?

When a relative URI is provided to a client, the client will combine the base URI with the relative URI using the rules described in RFC 3986, section 2.当向客户端提供相对 URI 时,客户端将使用 RFC 3986 第 2 节中描述的规则将基本 URI 与相对 URI 组合。

Actually a variable interpolation is not possible within single quotes.实际上,单引号内不可能进行变量插值。 This means that you currently are calling users/$username and the $username variable gets not replaced with its value.这意味着您当前正在调用users/$username并且$username变量不会替换为其值。

In order to get it, you should use it in one of the following ways:为了获得它,您应该通过以下方式之一使用它:

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

I personally prefer the second one as it is assumed to be faster.我个人更喜欢第二个,因为它被认为更快。

Okay i solved it, stupid mistake by me.好的,我解决了,我犯了一个愚蠢的错误。 I used new Client .我使用了new Client

And it should be of course new GuzzleHttp\\Client它当然应该是new GuzzleHttp\\Client

As it is just for testing in my routes.php i did not the Namespace因为它只是为了在我的routes.php测试我没有Namespace

Thanks for your help everybody.谢谢大家的帮助。

Thanks to Paratron https://github.com/googleapis/google-api-php-client/issues/1184#issuecomment-355295789 In my case, on cakephp 3.8 & docker 19.03.5, I was facing curl error due to some network issue.感谢 Paratron https://github.com/googleapis/google-api-php-client/issues/1184#issuecomment-355295789就我而言,在 cakephp 3.8 和 docker 19.03.5 上,由于某些网络,我遇到了 curl 错误问题。 I restarted my cake-server docker container, & it worked like a charm.我重新启动了我的 cake-server docker 容器,它工作起来很有吸引力。

Your Laravel installer is very out of date.您的 Laravel 安装程序已经过时了。 The only way to get the latest version is to remove and install again:获取最新版本的唯一方法是删除并重新安装:

  1. composer global remove laravel/installer composer 全局删除 laravel/installer
  2. composer global require laravel/installer composer global 需要 laravel/installer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM