【问题标题】:Fatal error: Class 'HttpRequest' not found in C:\xampp\htdocs\test致命错误:在 C:\xampp\htdocs\test 中找不到类“HttpRequest”
【发布时间】:2015-09-25 20:22:58
【问题描述】:

在我的网站中,出于安全目的,我使用 infobip 2-Factor Authentication。我在他们的网站上有一些代码显示错误,例如

Fatal error: Class 'HttpRequest' not found in C:\xampp\htdocs\test\OTP\send.php on line 2

代码如下:

发送.php

<?php
$request = new HttpRequest();
$request->setUrl('https://oneapi.infobip.com/2fa/1/pin');
$request->setMethod(HTTP_METH_POST);
$request->setHeaders(array
  ('accept' => 'application/json'
  ,'content-type' => 'application/json'
  ,'authorization' => 'App SECRET_KEY'));
  $request->setBody('{
  "applicationId": "YOUR_APPLICATION_ID",
  "messageId": "YOUR_MESSAGE_ID",
  "to": "PHONE_NUMBER"
}');

try
    {
        $response = $request->send();
        echo $response->getBody();
    } 
    catch (HttpException $ex) 
    {
        echo $ex;
    }
?>

下面的请求代码,这也显示了

Fatal error: Class 'HttpRequest' not found in C:\xampp\htdocs\test\OTP\request.php on line 3

request.php

<?php
$request = new HttpRequest();
$request->setUrl('https://oneapi.infobip.com/2fa/1/pin/{PIN_ID}/verify');
$request->setMethod(HTTP_METH_POST);
$request->setHeaders(array('accept' => 'application/json', 'content-type' => 'application/json','authorization' => 'App YOUR_API_KEY'
));
$request->setBody('{"pin": "PIN_CODE"}');

try
   {
       $response = $request->send();
       echo $response->getBody();
   }   
   catch (HttpException $ex) 
   {
       echo $ex;
   }
?>

【问题讨论】:

  • 这是因为包含该类的文件尚未使用 include 或 require 加载。从字面上看,HttpRequest 类在被包含之前对 php 没有任何意义。
  • 你的意思是我想在这里添加一些文件...?
  • 您希望拥有包含该类的文件,然后添加类似 include 'pathtoHttpReques';
  • @NishaChinnapa 您是否在本地尝试这些代码?我的意思是localhost ?
  • 这可能是问题Not including the appropriate file or mismatch path, (Infobip will have a bulk of folders for sending sms) 忠告尝试在线触发文件,我相信你也安装了composer的infobip

标签: php httprequest infobip infobip-api


【解决方案1】:

好吧,假设你在同一个文件夹中有一个文件 HttpRequest.php,并且在该文件中是

 class HttpRequest {
    ...
}

然后在调用它之前的某个地方,你需要告诉 php 。这是使用四种方法之一完成的,但我会这样做

require_once __DIR__.'/HttpRequest.php'; //assuming it's in the same folder.

请记住,这不是魔术,它只是计算机代码。它只知道你告诉它什么。我还希望你获得代码的地方会有一些关于如何安装和设置的基本说明,并且你会阅读。

现在不是让你感到困惑,但选择是

include
include_once
require
require_once

我把这些放在这里只是为了说明,事情(通常)意味着他们在编程中所说的。所以包含,包含它,添加 *_once,只执行一次,如果在指定的位置找不到它,require 将抛出错误,从而使其成为必需。而 include 并不真正关心它是否真的被包括在内。

祝你好运!快乐编码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 2011-10-22
    • 2022-12-14
    相关资源
    最近更新 更多