“PHP Fatal error: Class 'HttpRequest' not found”

孤者浪人 提交于 2019-12-27 17:40:14

问题


I've been stuck trying to solve this problem in many ways, reading a lot of posts but still having no luck. I work on a Mac, OSX 10.7 Lion, and I'm writing a plugin for a WordPress site (php files) using MAMP, and at one point I have to make an HTTP request:

$request = new HttpRequest('something'); 
$request->setMethod(HTTP_METH_GET);

There's an error when executing this request and when I checked the log file, here's the message:

"PHP Fatal error:  Class 'HttpRequest' not found in (the_php_file)"

I've already installed PEAR, PECL and the HTTP extension (pecl_http), Xcode and its command line tools. This is what I did:

  • modified the PATH:

    $ echo "export PATH=/Applications/MAMP/bin/php/php5.5.3/bin:$PATH" >> ~/.profile`
    
  • verified that the paths for php, pear and pecl are correct:

    • /Applications/MAMP/bin/php/php5.3.6/bin/php is the path for php
    • /Applications/MAMP/bin/php/php5.3.6/bin/pear for pear
    • /Applications/MAMP/bin/php/php5.3.6/bin/pecl for pecl
  • I downloaded the source for php from the MAMP page (and checked that the version I downloaded was the same one I'm using, which is php5.5.3). Then I extracted the content and put it into /Applications/MAMP/bin/php/php5.5.3/include/php

  • Inside /Applications/MAMP/bin/php/php5.5.3/include/php I ran ./configure
  • I executed: pecl install pecl_http
  • I also added these extensions to the php.ini (initially, I modified the MAMP/bin/php/php5.5.3/conf/php.ini file):

    extension=raphf.so
    extension=propro.so
    extension="http.so"
    

About that last one in quotation marks: when I added the extension manually, I did it like this: extension=http.so. Then (when trying to fix my problem) I tried an alternative installation that modified the php.ini automatically, and wrote the extension with the quot. marks, but the result was still the same, so it didn't make a difference.

After all of this, I stopped the MAMP server and started it again, but when I executed the php I still got the error (visible in the php_error.log):

PHP Fatal error:  Class 'HttpRequest' not found

I've been following this guide mostly, among so many more: http://www.lullabot.com/blog/article/installing-php-pear-and-pecl-extensions-mamp-mac-os-x-107-lion

I'd appreciate any idea because I've run out of them.


回答1:


The class HttpRequest is provided by v1 of this PECL extension.

Re-install via: $ pecl install -f pecl_http-1.7.6

You can find documentation for v2 here, though: https://mdref.m6w6.name/http




回答2:


alternatively, in case you cannot control certain environmental variables or install packages, you might try using curl which should return a json object (below is a working snippet of a google api call).

$url = 'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=TOKEN_DATA_123';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$json = json_decode($response, true);
curl_close($ch);
print_r($json);
$userEmail = $json["email"];


来源:https://stackoverflow.com/questions/22726419/php-fatal-error-class-httprequest-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!