phphttprequestinfobipinfobip-api

Fatal error: Class 'HttpRequest' not found in C:\xampp\htdocs\test


In my website am using infobip 2-Factor Authentication for security purpose. I got some code in their website it showing error like

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

Code is following:

Send.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;
    }
?>

This following code for request and this also showing

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;
   }
?>

Solution

  • Ok say you have a file HttpRequest.php in the same folder and in that file is

     class HttpRequest {
        ...
    }
    

    Then somewhere before calling it, you need to tell php about it. This is done using one of four ways, but I would do it this way

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

    Remember this isn't magic, it's just computer code. It only knows what you tell it. I would also hope that the place you got the code from would have some kind of basic instructions on how to install set it up, and that you read that.

    Now not to confuse you but the choices are

    include
    include_once
    require
    require_once
    

    I only put these here to illustrate that, things ( generally ) mean what they say in programing. So include, includes it, adding *_once, only does it once, and require will throw an error if it's not found in the location specified, thus making it required. Whereas include doesn't really care if it was actually included or not.

    Good Luck! happy coding.