简体   繁体   中英

PHP - How to get httpful phar to work

From http://phphttpclient.com I followed "Install option 1" and the first "quick snippet".

I end up with the following, with Request undefined.

在此处输入图片说明

在此处输入图片说明

Additionally, and perhaps relatedly, I am confused by the fact that one of the code samples says "$response = Request::get" and another says "$response = \\Httpful\\Request::get". Is the latter valid PHP?

I have PHP 5.6.7.

在此处输入图片说明

What am I doing wrong?

Yes, \\Httpful\\Request::get() is valid PHP. It tells PHP that you're looking for the class Request in the namespace Httpful . More on namespaces: http://php.net/manual/en/language.namespaces.php

The reason you can call \\Httpful\\Request::get() , but can't call Request::get() is namespace related. In your index.php , you're not defining a namespace. Therefor, PHP just looks for a class Request in the global space (when calling Request::get() ). PHP does not check if there's a Request class in another namespace.

You can use (import) a class, that will prevent you from having to type the entire namespace everytime you want to use the Request class:

<?php

use Httpful\Request;
$request = Request::get()

# you can also rename the class if you have multiple Request classes
use Httpful\Request as Banana;
$request = Banana::get()

More on that subject: http://php.net/manual/en/language.namespaces.importing.php

I just followed the 'quick-hack' install suggested by the author and got the same result. I then used the fully qualified namespace and got it working.

as :

$response = \Httpful\Request::get($uri)->send(); // qualified namespace here

I'll stick to the hack while i kick the tires, then if i adopt the lib, i'll go the composer route (much much better imo).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM