Search code examples
phplaravellaravel-4httpful

Httpful and Laravel, error Class 'Httpful' not found


I've installed Httpful as described with Composer adding to composer.json the following:

{
    "require": {
        "nategood/httpful": "*"
    }
}

I'm using Laravel 4 so i ran composer install I've checked if the plugin is installed and is there, in fact under the vendor folder of laravel i can find it. But i keep getting the following error:

ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class 'Httpful' not found'

I'm missing some steps?

Thank you in advance


Solution

  • The class related to this package name conflicts with Laravel's Response class, so this is how you use it in Laravel:

    $url = "http://api.openweathermap.org/data/2.5/weather?lat=22.569719&lon=88.36972";
    
    $response = \Httpful\Request::get($url)->send();
    
    echo $response->body->name."<br>";
    echo $response->body->weather[0]->description;
    

    The class is not Httpful, but Response, so you have to add the correct Namespace so it doesn't get confused by Laravel's Response class.

    EDIT:

    In Laravel you can create aliases for classes. Edit your app/config/app.php and in the aliases array, add:

    'aliases' => array(
             ....
    
         'Httpful'      => '\Httpful\Request',
    ),
    

    And you'll be able to use it this way:

    $response = Httpful::get($url)->send();