Caphalem's avatar

How do I use HTTP_Request2 in Laravel 5?

Hello!

So here's the thing. I'm trying to make a small website that will use an ontology stored on a Sesame Server. I found this (https://github.com/labxxi/phpsesame ) library, put it into my custom directory in app/Libraries and namespaced it. All is fine there. To use that PhpSesame library you need HTTP_Request2 (https://pear.php.net/package/HTTP_Request2 ) and semsol/ARC2 (https://github.com/semsol/arc2 ). Installing ARC2 was simple, update composer.json and do composer update. However, HTTP_Request2 requires you to install PEAR and then install it through PEAR. I believe I have done that but if I try to use phpSesame I get this: FatalErrorException in phpSesame.php line 156: Class 'App\Libraries\HTTP_Request2' not found.

If I look up the usability of HTTP_Request2 it basically says that you need to have "require_once 'HTTP/Request2.php';" in your code to use it which the phpSesame seem to have. My guess is that the fact that I'm doing this on a framework is what's causing issues which brings me here. Any ideas?

0 likes
13 replies
Caphalem's avatar

Well, that's handy! Still doesn't work though :(

Caphalem's avatar

Still the same one. FatalErrorException in phpSesame.php line 156: Class 'App\Libraries\HTTP_Request2' not found.

spekkionu's avatar

You mentioned you changed the namespace of the library you are using.

This means you will need to import every class the library uses with a use statement (including HTTP_Request2) or use the fully qualified classname (just prefix with \ if it is in the global namespace).

spekkionu's avatar

A better idea would be to install the library using composer. https://packagist.org/packages/labxxi/phpsesame

You will still need the HTTP_Request2 library installed via composer as it doesn't define it as a dependency in the composer.json.

This way the autoloading should do the work for you.

Caphalem's avatar

I remembered that when doing this with composer requires further tinkering with the app.php in the config folder which kinda melted my mind while trying to resolve it... What should I enter in the providers and aliases areas when I add packages like these in?

spekkionu's avatar

If you want one all a service provider would need to do is return an instance of the class which you would create the same way you would without laravel.

In the register method of a service provider:

$this->app->bind('PhpSesame', function($app){
    return new PhpSesame(/* parameters */);
});

If you aren't going to be passing the same arguments to the constructor each time you would skip the service provider completely and just create a new instance when you need one just like you can with any class.

You can create a facade if you want to but if you aren't going to use it don't bother.

vikdhr's avatar

Class 'App\Http\Controllers\Http_Request2' not found.

I use this as require_once $path = base_path('vendor/pear/http_request2/HTTP/Request2.php');

I used https://packagist.org/packages/pear/http_request2. to install this, and in vendor/pear/http_request2/HTTP/Request2.php there is a file Request2.php having a HTTP_Request2 how to fix this.

vikdhr's avatar

require_once $path = base_path('vendor/pear/http_request2/HTTP/Request2.php');

$request = new Http_Request2('https://api.kwikee.com/entities/v3/manufacturers?page={page}');

$url = $request->getUrl();

print_r($url); die('here');

This is my code to fetch the manufactures from kwikee api but i get the error "Class 'App\Http\Controllers\Http_Request2' not found."

Nana-Odai's avatar

Have you gotten solution on how you included the request2. I am facing same issue.

Alin3661's avatar

I had the same problem, the fix was the following:

  • install via composer: composer require pear/http_request2
  • since release 2.4 composer installation relies completely on autoloading and does not contain require_once calls or use include-path option, in your file, make sure you make use of this file:
use HTTP_Request2;
  • use as needed, default example would be:
$request = new HTTP_Request2('http://pear.php.net/', HTTP_Request2::METHOD_GET);
try {
    $response = $request->send();
    if (200 == $response->getStatus()) {
        echo $response->getBody();
    } else {
        echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
             $response->getReasonPhrase();
    }
} catch (HTTP_Request2_Exception $e) {
    echo 'Error: ' . $e->getMessage();
}
1 like

Please or to participate in this conversation.