PECL_HTTP not recognised php ubuntu

2019-07-19 04:24发布

I am trying to use the HttpRequest class which should be in the PECL_HTTP extension of php. I have php5 and used the following to install pecl_http.

sudo pecl install pecl_http

pecl/pecl_http is already installed and is the same as the released version 2.0.1
install failed

After that i enter into my php.ini:

[PHP]
extension=http.so
[...]

Then i restart my apache2 server, and try using the HttpRequest class, but it gives me the following error:

PHP Fatal error:  Class 'HttpRequest' not found

What are possible error's i might have missed?

UPDATE: (Changed the title)

The Extension is not shown in phpinfo(), i set

extension=http.so

And checked if the http.so file is in my extension_dir. I really don't know how to make php recognise the extension.

UPDATE 2: I managed to install extension, but the class still does not exist. For others, i had to reference the other extensions pecl_http needs. (For me: propro.so, raphfr.so)

UPDATE 3: I did not manage to make the Class visible, the answers below show some approaches with other classes.

I solved this issue by using CURL.

2条回答
男人必须洒脱
2楼-- · 2019-07-19 04:53

You have installed the new version of the extension which uses a completely different API. I still don't know how it works, but I will update my answer one I know. The Documentation of the new version is to be found at http://devel-m6w6.rhcloud.com/mdref/http.

To Install the old version, first uninstall the new verion and then execute

pecl install http://pecl.php.net/get/pecl_http-1.7.6.tgz

UPDATE: here are two examples from the documentation, both should work well:

one request (can be found at http://devel-m6w6.rhcloud.com/mdref/http/Client/enqueue):

<?php
(new http\Client)->enqueue(new http\Client\Request("GET", "http://php.net"), 
    function(http\Client\Response $res) {
        printf("%s returned %d\n", $res->getTransferInfo("effective_url"), $res->getResponseCode());
        return true; // dequeue
})->send();

multiple requests (can be found at http://devel-m6w6.rhcloud.com/mdref/http/Client/once):

<?php
$client = new http\Client;
$client->enqueue(new http\Client\Request("HEAD", "http://php.net"));
$client->enqueue(new http\Client\Request("HEAD", "http://pecl.php.net"));
$client->enqueue(new http\Client\Request("HEAD", "http://pear.php.net"));

printf("Transfers ongoing");
while ($client->once()) {
    // do something else here while the network transfers are busy
    printf(".");
    // and then call http\Client::wait() to wait for new input
    $client->wait();
}
printf("\n");

while ($res = $client->getResponse()) {
    printf("%s returned %d\n", $res->getTransferInfo("effective_url"),
        $res->getResponseCode());
}

In both examples you can use $res->getBody() to return the body of the response.

I couldn't test these examples but I have heard of other people that they work.

查看更多
老娘就宠你
3楼-- · 2019-07-19 05:16

I had the same problem and solved it by arrange the order in wich extensions are loaded in php.ini

All my other extensions was loaded using their own .ini files in /etc/php5/mods_available. In my apache2 error.log I noticed that json_decode was needed by http.so to load.

I created a file for http (http.ini) and a symlink in /etc/php5/apache/conf.d with a higher prefix than json.

my http.ini


; configuration for php http module
; priority=50

extension=raphf.so

extension=propro.so

extension=http.so

use the new pecl_http.2.0.6 like this

new http\Message();

or extend in your own class as extends http\Message

查看更多
登录 后发表回答