php Laravel - Guzzle 请求/cURL 错误 6:无法解析主机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31923397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - Guzzle Request / cURL error 6: Could not resolve host
提问by bobbybackblech
I try to make an API Request to the Github API just for testing. I installed the latest Guzzle Version ( "guzzle/guzzle": "^3.9" ) on my Laravel 5.1 APP.
In my routes.php
i have the following Code:
我尝试向 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);
});
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 我得到错误cURL error 6: Could not resolve host: users
。
Why am i getting this Error?
为什么我会收到此错误?
If i visit https://api.github.com/users/kayyyyi 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 这可能是主机无法解决的问题吗?
EDITIf 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);
});
回答by M. Sabev
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.
我个人更喜欢第二个,因为它被认为更快。
回答by Ben Johnson
Why are you calling $client->get->()->send()
? In particular, why are you chaining the send() method at the end? The documentation does not append the send()
method when demonstrating what seems to be the same action:
你为什么打电话$client->get->()->send()
?特别是,为什么要在最后链接 send() 方法?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 组合。
回答by bobbybackblech
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.
谢谢大家的帮助。
回答by Sarmad Baloch
Thanks to Paratron https://github.com/googleapis/google-api-php-client/issues/1184#issuecomment-355295789In my case, on cakephp 3.8 & docker 19.03.5, I was facing curl error due to some network issue. I restarted my cake-server docker container, & it worked like a charm.
感谢 Paratron https://github.com/googleapis/google-api-php-client/issues/1184#issuecomment-355295789就我而言,在 cakephp 3.8 和 docker 19.03.5 上,由于某些网络,我遇到了 curl 错误问题。我重新启动了我的 cake-server docker 容器,它工作起来很有吸引力。