3
votes

I have installed pecl_http, but when I try to use it, I get an error:

Fatal error: Uncaught Error: Call to undefined function http_get() in /opt/lampp/htdocs/tes_http.php:3 Stack trace: #0 {main} thrown in /opt/lampp/htdocs/tes_http.php on line 3

This is my php.ini configuration:

extension="propro.so"
extension="http.so"
extension="raphf.so"
[PHP]

;;;;;;;;;;;;;;;;;;;

Please help me to figure out why the function is not available.

1
If you're not yet you have to enable;extension=php_http.dll in your php.ini file. Simply remove semicolons and after that restart your Apache Server.S.I.

1 Answers

5
votes

The current version of http extension (the package name is pecl_http) doesn't provide http_get() function. This function has been removed in version 2.0.0 (right after version 1.7.6). You can see it by running the following commands in terminal:

git clone https://github.com/m6w6/ext-http.git
cd ext-http
git diff RELEASE_1_7_6 RELEASE_2_0_0

Although it is not mentioned explicitly in the changelog, the procedural style is completely replaced with OOP style in the second version.

The documentation on PHP's official site is obsolete. Extension's author hosted the new version on his own site. I wouldn't blame him much, as the link for documentation on the PECL site points to the right place. Undoubtedly, he should remove the old documentation from php.net/manual, or at least update it.

The new way to perform HTTP GET requests implies the use of http\Client\Request class:

$request = new http\Client\Request("GET",
  "http://example.com",
  ["User-Agent"=>"MyAgent/0.1"]
);
$request->setOptions(["timeout" => 1]);

$client = new http\Client;
$client->enqueue($request)->send();

$response = $client->getResponse();

Regarding the setup

You should load the dependencies before http.so as it is recommended in the documentation:

; obligatory deps
extension = raphf.so
extension = propro.so

; if shared deps were enabled
extension = hash.so
extension = iconv.so
extension = json.so

; finally load pecl/http
extension = http.so