“PHP 致命错误:找不到‘HttpRequest’类”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22726419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:12:58  来源:igfitidea点击:

"PHP Fatal error: Class 'HttpRequest' not found"

phphttprequestosx-lionpecl

提问by chechab

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:

我一直在试图以多种方式解决这个问题,阅读了很多帖子,但仍然没有运气。我在 Mac、OSX 10.7 Lion 上工作,我正在使用 MAMP 为 WordPress 站点(php 文件)编写插件,并且有一次我必须发出 HTTP 请求:

$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:

我已经安装了 PEAR、PECL 和 HTTP 扩展 (pecl_http)、Xcode 及其命令行工具。这就是我所做的:

  • 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"
    
  • 修改了路径:

    $ echo "export PATH=/Applications/MAMP/bin/php/php5.5.3/bin:$PATH" >> ~/.profile`
    
  • 验证 php、pear 和 pecl 的路径是否正确:

    • /Applications/MAMP/bin/php/php5.3.6/bin/php 是php的路径
    • /Applications/MAMP/bin/php/php5.3.6/bin/pear 为梨
    • /Applications/MAMP/bin/php/php5.3.6/bin/pecl for pecl
  • 我从 MAMP 页面下载了 php 的源代码(并检查了我下载的版本是否与我正在使用的版本相同,即 php5.5.3)。然后我提取了内容并将其放入/Applications/MAMP/bin/php/php5.5.3/include/php

  • 在 /Applications/MAMP/bin/php/php5.5.3/include/php 我跑了 ./configure
  • 我执行了: pecl install pecl_http
  • 我还在 php.ini 中添加了这些扩展(最初,我修改了 MAMP/bin/php/php5.5.3/conf/php.ini 文件):

    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.

关于引号中的最后一个:当我手动添加扩展名时,我是这样做的:extension=http.so. 然后(在尝试解决我的问题时)我尝试了一个自动修改 php.ini 的替代安装,并用引号编写了扩展名。标记,但结果仍然相同,因此没有任何区别。

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):

在这一切之后,我停止了 MAMP 服务器并再次启动它,但是当我执行 php 时我仍然收到错误(在 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

我一直在关注本指南,还有很多:http: //www.lullabot.com/blog/article/installing-php-pear-and-pecl-extensions-mamp-mac-os-x-107-狮子

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

我很感激任何想法,因为我已经用完了它们。

回答by m6w6

The class HttpRequest is provided by v1of this PECL extension.

HttpRequest 类由此 PECL 扩展的v1提供。

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

通过以下方式重新安装: $ pecl install -f pecl_http-1.7.6

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

不过,您可以在此处找到v2 的文档:https: //mdref.m6w6.name/http

回答by tony gil

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).

或者,如果您无法控制某些环境变量或安装软件包,您可以尝试使用应该返回 json 对象的 curl(以下是 google api 调用的工作片段)。

$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"];