问题描述
我尝试向Github API发出API请求只是为了进行测试.我在Laravel 5.1 APP上安装了最新的Guzzle版本("guzzle/guzzle":"^ 3.9").在我的routes.php
中,我具有以下代码:
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:
Route::get('guzzle/{username}', function($username) {
$client = new Client([
'base_uri' => 'https://api.github.com/users/',
]);
$response = $client->get("/users/$username");
dd($response);
});
如果我现在访问URL domain.dev/github/kayyyy,则会收到错误cURL error 6: Could not resolve host: users
.
If i now visit the URL domain.dev/github/kayyyy i get the Error cURL error 6: Could not resolve host: users
.
为什么会出现此错误?
如果我访问 https://api.github.com/users/kayyyy ,我可以参见json
输出.
If i visit https://api.github.com/users/kayyyy i can see the json
output.
我也在使用Homestead/Vagrant,这可能是主机无法解决的问题吗?
编辑如果我在没有base_uri的情况下尝试了此方法,那么它将起作用.
EDITIf i try this without the base_uri it works.
Route::get('guzzle/{username}', function($username) {
$client = new GuzzleHttp\Client();
$response = $client->get("https://api.github.com/users/$username");
dd($response);
});
推荐答案
好吧,我解决了这个愚蠢的错误.我使用了new Client
.
Okay i solved it, stupid mistake by me.I used new Client
.
当然应该是new GuzzleHttp\Client
因为它只是在我的routes.php
中进行测试,所以我没有Namespace
As it is just for testing in my routes.php
i did not the Namespace
感谢大家的帮助.
这篇关于Laravel-狂饮请求/cURL错误6:无法解析主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!