Frage

I'm trying to use unirest, a new php lib for making rest calls.

I'd like to place it in a system-wide directory above my project. I then include it:

require_once ('../unirest-php-master/lib/Unirest/Unirest.php');

loads fine. Then I use it per the readme:

$response = Unirest::post(CSWA_URL ....

I get Fatal error: Class 'Unirest' not found in ...hello_world/sign_start.php on line 23

I then try to use the namespace (see the library's code. They use a Namespace Unirest statement before declaring the Unirest class.)

$response = Unirest\Unirest::post(CSWA_URL ....

I got further. Now: Fatal error: Class 'Unirest\HttpMethod' not found in ....unirest-php-master/lib/Unirest/Unirest.php on line 26 -- This is an error in the library code!

Q: Did I do something wrong? Did the authors of Unirest make a mistake? Do I have to place the library in ./lib? What's the best fix?

War es hilfreich?

Lösung

It looks like the Unirest code in Unirest.php relies on autoloading code from the two other files in the unirest lib directory (HttpMethod.php and HttpResponse.php).

The author suggests installing the package using composer, if you were to do that composer would add the Unirest namespace to the autoloader.php script it generates. From there you need to require the autoload.php file at the top of your script and it will handle loading classes that aren't defined.

Alternatively, if you don't want to use composer, I would just require the other two files in the unirest lib directory at the top of your script as well.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top