如何提出发布请求以获取数据Laravel Guzzle?

发布于 2025-02-11 01:02:06 字数 1388 浏览 1 评论 0 原文

Let say, the Secret Key is XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX and md5key is YYYYYYYY. 我做了一个查询字符串QS Qs = “method=RegUserInfo&Key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&Time=20140101123456&Username=DemoUser001”;

urlencode后,我得到 q ='j4tjorjwarfj3trash0safrwg2wt4awari0fwjfeoh'

我制作了用于构建签名的MD5字符串(QS + MD5Key + time + time +键): s = buildmd5(QS +“ Yyyyyyyyy” +“ 20140111123456” +“ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx x”); 我得到了

因此将获得 q = j4tjorjwarfj3trass0safrwg2wt4awari0fwjfeoh&s = 12334567890abcdef

;形式上的编码”))) 通过发布到

http://xxxxx.com/api/api/api.aspx

   $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);
    }
    
}

我被禁止403

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃气十足 2025-02-18 01:02:06

如果要 content-type:application/x-www-form-urlencoded 您需要使用form_params请求选项。

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 
}

了解 form_params

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文