Guzzle - Laravel. How to make request with x-www-form-url-encoded

前端 未结 1 1010
忘掉有多难
忘掉有多难 2021-02-20 14:45

I need to integrate an API so I write function:

public function test() {

    $client = new GuzzleHttp\\Client();

try {
    $res = $client->post(\'http://ex         


        
相关标签:
1条回答
  • 2021-02-20 14:57

    The problem is that in Postman you're sending the data as a form, but in Guzzle you're passing the data in the 'json' key of the options array.

    I bet that if you would switch the 'json' to 'form_params' you would get the result you're looking for.

    $res = $client->post('http://example.co.uk/auth/token', [
        'form_params' => [
            'client_id' => 'SOMEID',
            'client_secret' => '9999jjjj67Y0LBLq8CbftgfdreehYEI=',
            'grant_type' => 'client_credentials'
        ]
    ]);
    

    Here's a link to the docs in question: http://docs.guzzlephp.org/en/stable/quickstart.html#sending-form-fields

    Also, I noticed a typo - you have cliend_id instead of client_id.

    0 讨论(0)
提交回复
热议问题