phplaravelhttp-postguzzle

How to make a POST request via GuzzleRequest?


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 instantiated, I want to pass data inside the body parameters. The body parameters can take: string|resources|null|StreamInterface, but this is the data I want to pass:

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

This the full function code:

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