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

34,446

Solution 1

Okay i solved it, stupid mistake by me. I used new Client.

And it should be of course new GuzzleHttp\Client

As it is just for testing in my routes.php i did not the Namespace

Thanks for your help everybody.

Solution 2

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:

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.

Solution 3

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.

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.

Share:
34,446
bobbybackblech
Author by

bobbybackblech

Updated on July 09, 2022

Comments

  • bobbybackblech
    bobbybackblech almost 2 years

    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);
    });
    

    If i now visit the URL domain.dev/github/kayyyy i get the 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.

    I am also using Homestead / Vagrant is this maybe a Problem that the host cant be resolved?

    EDIT If 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);
    });
    
  • bobbybackblech
    bobbybackblech over 8 years
    Thanks for your reply. Yep, but this didnt worked, still the same error.