0

Let say, the Secret Key is XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX and md5key is YYYYYYYY. I made a Query String QS Qs = “method=RegUserInfo&Key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&Time=20140101123456&Username=DemoUser001”;

After urlencode I got q='j4tjorjwarfj3trwise0safrwg2wt4awari0fwjfeoh'

I made MD5 String for building the signature (QS + md5key + Time + Key): s = BuildMD5(QS + “YYYYYYYY” + “20140101123456” + “XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”); I got s='1234567890abcdef'

So will get q=j4tjorjwarfj3trwise0safrwg2wt4awari0fwjfeoh&s=1234567890abcdef

How to resulting POST method query (using “Content-Type: application/x-www-form-urlencoded”) by POST to http://xxxxx.com/api/api.aspx

My code is

   $param = "q=".$q."&s=".$s;
 
    $client = new Client(['headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
        ]]);
    try{
        $response = $client->request('POST','http://xxxxxx.com/api/api.aspx', [
            'query' => [$param],
    ]);
    }catch(ClientException $e){
        $response = $e->getResponse();
        $responseBodyAsString = $response->getBody()->getContents();
        dd($responseBodyAsString);
    }
    
}

but I get 403 Forbidden

1 Answers1

0

If you want Content-Type: application/x-www-form-urlencoded you need to use form_params request option.

try{
  $client = new \GuzzleHttp\Client(['headers' => ['Authorization' => 'Bearer ' . $your_token]]);
  
  $guzzleResponse = $client->post(
                $api_url, [
                'form_params' => [
                    'grant_type' => 'xxxxx',
                    'key' => 'xxx',
                    'time' => 'xxxx', 
                    'username' => 'xxxxxx'
                ]
            ]);
    if ($guzzleResponse->getStatusCode() == 200) {
         $response = json_decode($guzzleResponse->getBody(),true);
         //perform your action with $response 
    } 
}
catch(\GuzzleHttp\Exception\RequestException $e){
   // you can catch here 40X response errors and 500 response errors

}catch(Exception $e){
   //other errors 
}

know more about form_params

bhucho
  • 3,903
  • 3
  • 16
  • 34
  • Where can I put my $param = "q=".$q."&s=".$s; ? I don't think I have any Authorization or token. I have my IP whitelist already. –  Jun 29 '22 at 02:07
  • We use form_param in case we want to create record right? In case, I only want to get data with POST method. –  Jun 29 '22 at 02:07
  • 1
    put it inside in form_params as array – bhucho Jun 29 '22 at 08:41
  • 1
    we use form_params if asked to send via `application/x-www-form-urlencoded`. The `application/x-www-form-urlencoded` content type describes form data that is sent in a single block in the HTTP message body. Unlike the query part which you see in URL in a GET request, the length of the data is unrestricted when in body in form_data – bhucho Jun 29 '22 at 08:43
  • also you can read [application/x-www-form-urlencoded or multipart/form-data?](https://stackoverflow.com/q/4007969/9471283) – bhucho Jun 29 '22 at 08:44
  • Oh.. I got it clear now. Thank you. –  Jun 29 '22 at 09:21