[英]Symfony - Guzzle request: cURL error 6: Could not resolve host
[英]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 domain.dev/github/kayyyy,我會收到 Error 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#creating-a-client
另外,您是否考慮過上述手冊頁中此聲明的含義?
當向客戶端提供相對 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
謝謝大家的幫助。
感謝 Paratron https://github.com/googleapis/google-api-php-client/issues/1184#issuecomment-355295789就我而言,在 cakephp 3.8 和 docker 19.03.5 上,由於某些網絡,我遇到了 curl 錯誤問題。 我重新啟動了我的 cake-server docker 容器,它工作起來很有吸引力。
您的 Laravel 安裝程序已經過時了。 獲取最新版本的唯一方法是刪除並重新安裝:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.