Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to instantiate GuzzleRequest?

I am new at php/symphony/laravel and i working on a project. For that projet, i’m trying to configure an API.
I don’t know how i can instantiate a GuzzleRequest to post data and generate authorization header for the API.
This is the GuzzleRequest created

  $request = new GuzzleRequest(
            method: 'POST',
            uri: $url,
            //body:null,
        );

Inside the GuzzleRequest that i’ve instantiate, i want to pass data inside the body parameters.
The body parameters can take : string|resources|null|StreamInterface but this data i want to pass :

{
    "payItemId": "S-112-948-MTNMOMO-20052-200040001-1",
    "amount": 1000
}

This the full function code :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function headerauthorization()
    {

        $token = "716c3fcf-98bd-435e-a16d-befc3b96da35";
        $secret = "9f96a7ed-338f-4609-83f8-27ce60fe87f4";
        //$url = "https://s3p.smobilpay.staging.maviance.info/v2/cashin?serviceid=20052";
        $url = "https://s3p.smobilpay.staging.maviance.info/v2/quotestd";

        $config = new \Maviance\S3PApiClient\Configuration();
        $config->setHost($url);
        $client = new \Maviance\S3PApiClient\ApiClient($token, $secret, ['verify' => false]);


        $request = new GuzzleRequest(
            method: 'POST',
            uri: $url,
            //body:null, 
        );

        $curl = curl_init();
        curl_setopt_array(
            $curl,
            array(
                CURLOPT_URL => 'https://s3p.smobilpay.staging.maviance.info/v2/quotestd',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_HTTPHEADER => array(
                    'Authorization:' . $client->buildAuthorizationHeader($request) . ''
                ),
            )
        );
        $response = curl_exec($curl);
        curl_close($curl);
        $response = json_decode($response, true);

    }

Why do i use GuzzleRequest ? Because the function buildAuthorizationHeader($request) only takes RequestInterface type as parameters. i firstly tried the following code(the method is GET and there’s no body’s parameters) for the first Api request and it works fine for me. But now i have a Post method and a body parameters to insert.

This is the first code which works fine for me

 function headerauthorization()
    {

        $token = "716c3fcf-98bd-435e-a16d-befc3b96da35";
        $secret = "9f96a7ed-338f-4609-83f8-27ce60fe87f4";
        $url = "https://s3p.smobilpay.staging.maviance.info/v2/cashin?serviceid=20052";
       

        $config = new \Maviance\S3PApiClient\Configuration();
        $config->setHost($url);
        $client = new \Maviance\S3PApiClient\ApiClient($token, $secret, ['verify' => false]);
        
        
        $request = new GuzzleRequest(
            method: 'GET',
            uri: $url,
            // headers: [],
            // body: null,
        );   
        $curl = curl_init();
        curl_setopt_array($curl, array(
           CURLOPT_URL => 'https://s3p.smobilpay.staging.maviance.info/v2/cashin?serviceid=20052',
           CURLOPT_RETURNTRANSFER => true,
           CURLOPT_ENCODING => '',
           CURLOPT_MAXREDIRS => 10,
           CURLOPT_TIMEOUT => 0,
           CURLOPT_FOLLOWLOCATION => true,
           CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
           CURLOPT_CUSTOMREQUEST => 'GET',
           CURLOPT_HTTPHEADER => array(
                'Authorization:'.$client->buildAuthorizationHeader($request).''
            ),
        )
        );
        $response = curl_exec($curl);
        curl_close($curl);
        $response = json_decode($response, true);
        $payItemId = $response[0]["payItemId"];
       
    } 

>Solution :

Set the body-data in an array and then json_encode like this.

$body = json_encode([
    "payItemId" => "S-112-948-MTNMOMO-20052-200040001-1",
    "amount" => 1000,
]);

Then add another field just after the CURLOPT_CUSTOMREQUEST => 'POST', like this

CURLOPT_POSTFIELDS => $body,

Finally set the CURLOPT_HTTPHEADER as

CURLOPT_HTTPHEADER => array(
            'Content-Type: application/json',
            'Authorization: ' . $client->buildAuthorizationHeader($request)
        ),

Try this it will work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading