bob*_*ech 3 php curl laravel guzzle
我尝试向 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);
});
Run Code Online (Sandbox Code Playgroud)
如果我现在访问 URL domain.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);
});
Run Code Online (Sandbox Code Playgroud)
你为什么打电话$client->get->()->send()
?特别是,为什么要在最后链接 send() 方法?send()
在演示似乎是相同的操作时,文档没有附加该方法:
http://guzzle.readthedocs.org/en/latest/quickstart.html#creating-a-client
另外,您是否考虑过上述手册页中此声明的含义?
当向客户端提供相对 URI 时,客户端将使用 RFC 3986 第 2 节中描述的规则将基本 URI 与相对 URI 结合起来。