Submitting a form post with PHP and CURL

I created a form and wrote the code to process the form data input using php/cURL in order to return the response body, when i inputed the session code and BVN in the form then clicked on submit instead of returning the response body as below it returned a different result.
What i want is for the code to return a response body, kindly look into the code and profer a solution.

This is what i want to be returned after supplying the required session code and BVN
Response Body

{
  "Success": true,
  "Errors": [],
  "SearchResult": [
    {
      "Relevance": 100,
      "BVN": "46582409734",
      "RegistryID": "68793545132457697809856",
      "Name": "Gbenga Sulaiman",
      "DOBI": "1970-10-20T00:00:00",
      "Phone": "08098764562",
      "Address": "231, Oba Akran, Allen Avenue\r\nIkeja\\ 035 Lagos",
      "EmailAddress": "me@example.com",
      "IDs": "",
      "Gender": "Male",
      "CustomerType": "Person",
      "Picture": ""
    }
  ],
  "StatusCode": 200,
  "Message": "BVN '46582409734' matched 1 customer(s)."
}

Not this, that returned instead

array(12) { [0]=> string(31) "HTTP/1.1 405 Method Not Allowed" [1]=> string(23) "Cache-Control: no-cache" 
[2]=> string(16) "Pragma: no-cache" [3]=> string(11) "Allow: POST" [4]=> string(45) "Content-Type: 
application/json; charset=utf-8" [5]=> string(11) "Expires: -1" [6]=> string(26) "Server: 
Microsoft-IIS/10.0" [7]=> string(27) "X-AspNet-Version: 4.0.30319" [8]=> string(21) 
"X-Powered-By: ASP.NET" [9]=> string(35) "Date: Sun, 12 Sep 2021 20:51:20 GMT" [10]=> string(22) 
"Connection: keep-alive" [11]=> string(18) "Content-Length: 72" } {"Success":true,
"Errors":[],"EmailAddress":"somebody@example.com","Authenticated":true,
"SessionCode":"149613","AgentID":"737838731263300529",
"AgentName":"Gare Advance Financial Limited AutoCred Test Agent","SubscriberID":"737838737107069765",
"SubscriberName":"Gare Advance Financial Limited","StatusCode":200,"Message":""}

My php and HTML Code

<?php
    session_start();
    if (empty($_SESSION['login']) || $_SESSION['login'] == false) {
        header('Location: /index2.php');
        exit;
    }
    
    $msg = empty($_SESSION['msg']) ? false : $_SESSION['msg'];
    
    if ($msg) {
       // Display the message in the dashboard
       echo $msg . "</br>" ;

       $data = array(
        'EmailAddress' => 'somebody@example.com',
        'Password' => 'Durosimi4Eti2021#',
        'SubscriberID' => '737838737107069765'
        );
    
        $url = "https://api2.creditregistry.com/nigeria/AutoCred/v7.Test/api/Agents/Login";

        $options = array(
        'http' => array(
            'method'  => 'POST',
            'content' => json_encode( $data ),
            'header'=>  "Content-Type: application/json\r\n" .
                        "Accept: application/json\r\n"
            )
        );

        $context  = stream_context_create( $options );
        $result = file_get_contents( $url, false, $context );
        $response = json_decode( $result, true );

        echo $result;
    }

    

    if (isset($_POST['submitMyBVN']) ) 
    { 
        //Wrap this code with the button click
        $data = array(
            'post_request_name_for_mybvn' => $_POST["myBVN"],
            'post_request_name_for_mysessioncode' => $_POST["mySessionCode"],
        );
    
        $url = "https://api2.creditregistry.com/nigeria/AutoCred/v7.Test/api/Customers/FindByBVN";
    
        //$nextPage = "dashboard.php";
    
        $options = array(
        'http' => array(
            'method'  => 'POST',
            'content' => json_encode( $data ),
            'header'=>  "Content-Type: application/json\r\n" .
                        "Accept: application/json\r\n"
            )
        );
    
        $context  = stream_context_create( $options );
        //$result = file_get_contents( $url, false, $context );
        file_get_contents($url, false, stream_context_create(['http' => ['ignore_errors' => true]]));
        var_dump($http_response_header);
        $response = json_decode( $result, true );
    
        //echo $result; to see the result remove comment and add comment to header('Location: '.$nextPage);
        echo $result;
    
        
    }



?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <h3> Welcome to dashboard Method 2  </h3>
    <form action="" method="POST">
        <label>Enter BVN:</label><br />
        <input type="num" name="myBVN" placeholder="Enter BVN" required/>
        <br /><br />
        <label>Enter Session Code:</label><br />
        <input type="num" name="mySessionCode" placeholder="Enter Session Code" required/>

        <br /><br />
        <button type="submit" name="submitMyBVN">Submit</button>
    </form> 
</body>
</html>

Uhhh and where are you using cURL?

Look at your unexpected response again. Notice how it says “Method not allowed” meaning that the endpoint (aka URL) you are submitting to either doesn’t except POST or expects POST but the way you are using cURL you are posting as the GET method instead. We won’t know until you show us where you are making the cURL request.

Thanks! :slight_smile:

Hi Martyr2 thanks for your reply. This code below is the new code with cURL i need it to return a response body as above but it’s not returning anything

if(isset($_GET['submitBVN']))
    {
        $bvn =  $_GET['bvn'];
        $sessionCode = $_GET['sessionCode'];
        $url = "https://api2.creditregistry.com/nigeria/AutoCred/v7.Test/api/Customers/FindByBVN"; // Where to post data
        try {
         $ch = curl_init();
       
        if (FALSE === $ch)
            throw new Exception('failed to initialize');
       
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);  // Tell cURL you want to post something
            curl_setopt($ch, CURLOPT_POSTFIELDS, "bvn=". $bvn ."&sessionCode=". $sessionCode); 
            // Define what you want to post
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            //curl_setopt(/* ... */);
            $content = curl_exec($ch);
            $info = curl_getinfo($ch);
            $json = json_decode($content, true);
          if (FALSE === $content)
           throw new Exception(curl_error($ch), curl_errno($ch));
       
         } catch(Exception $e) {
       
          trigger_error(sprintf(
            'Curl failed with error #%d: %s',
            $e->getCode(), $e->getMessage()),
            E_USER_ERROR);
         }
    }
	
	
	<form action="" method="POST">
        <label>Enter BVN:</label><br />
        <input type="num" name="bvn" placeholder="Enter BVN" required/>
        <br /><br />
        <label>Enter Session Code:</label><br />
        <input type="num" name="sessionCode" placeholder="Enter Session Code" required/>

        <br /><br />
        <button type="submit" name="submitBVN">Submit</button>
    </form>

@weblinxpro, when you post code in the forum, you need to format it. To do so you can either select all the code and click the </> button, or type 3 backticks ``` on a separate line both before and after the code block.

I have done it for you this time.