如何将PHP cURL格式转换为Guzzle Laravel格式

unguejic  于 2022-11-13  发布在  PHP
关注(0)|答案(3)|浏览(180)

目前,我正在尝试使用postman测试一些API端点。我们知道postman提供了信息来将请求集成到许多不同语言的代码中。所以我选择了我的laravel框架上的代码格式PHP-cURL,它工作了。然而,我想将PHP-cURL格式转换为laravel Guzzle。但没有工作。
以下是PHP-cURL代码

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://sandbox.plaid.com/asset_report/create',
  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_POSTFIELDS =>'{
   "client_id": "xxxx",
   "secret": "xxxx",
   "access_tokens": ["access-sandbox-xxx"],
   "days_requested": 30,
   "options": {
      "client_report_id": "ENTER_CLIENT_REPORT_ID_HERE",
      "webhook": "https://www.example.com/webhook",
      "user": {
        "client_user_id": "ENTER_USER_ID_HERE",
        "first_name": "ENTER_FIRST_NAME_HERE",
        "middle_name": "ENTER_MIDDLE_NAME_HERE",
        "last_name": "ENTER_LAST_NAME_HERE",
        "ssn": "111-22-1234",
        "phone_number": "1-415-867-5309",
        "email": "ENTER_EMAIL_HERE"
      }
   }
 }',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

这个PHP-cURL格式可以成功运行并在我的Laravel上显示响应.但是当我改为Laravel Guzzle时..它显示错误.这里是Guzzle代码

use GuzzleHttp\Client;

$guzzle = new Client;
$getAccestToken = $guzzle->request('POST', 'https://sandbox.plaid.com/asset_report/create', [
    'headers' => ['Content-Type' => 'application/json'],
    'json' => [
                "client_id" => "xxx",
                "secret" => "xxx",
                "access_token" => [ "access-sandbox-xxx" ] ,
                "days_requested" => 30,
                "options" => [
                        "client_report_id" => "ENTER_CLIENT_REPORT_ID_HERE",
                        "webhook" => "https://www.example.com/webhook",
                        "user" => [
                                "client_user_id" => "ENTER_USER_ID_HERE",
                                "first_name" => "ENTER_FIRST_NAME_HERE",
                                "middle_name" => "ENTER_MIDDLE_NAME_HERE",
                                "last_name" => "ENTER_LAST_NAME_HERE",
                                "ssn" => "111-22-1234",
                                "phone_number" => "1-415-867-5309",
                                "email" => "ENTER_EMAIL_HERE"
                            ]
                    ]
                ]   
]);

显示误差
客户端错误:POST https://sandbox.plaid.com/asset_report/create导致400 Bad Request响应:{“显示消息”:空,“文档URL”:“https://plaid.com/docs/?ref=error#invalid-request-errors“,“错误(已截断...)
我的狂饮代码缺少了什么。
请帮帮忙。

ioekq8ef

ioekq8ef1#

Laravel提供了Http客户端,它是一个围绕Guzzle的 Package 器:

$response = Http::withToken($token)->post('yourUrl', [ params ]);

dd($response->body());
dfuffjeb

dfuffjeb2#

您应该尝试将json替换为body
此外,将数组转换为有效的JSON格式。
参考:https://stackoverflow.com/a/39525059/5192105

use GuzzleHttp\Client;

$guzzle = new Client;
$getAccestToken = $guzzle->request('POST', 
'https://sandbox.plaid.com/asset_report/create', [
'headers' => ['Content-Type' => 'application/json'],
'body' => json_encode([
            "client_id" => "xxx",
            "secret" => "xxx",
            "access_token" => [ "access-sandbox-xxx" ] ,
            "days_requested" => 30,
            "options" => [
                    "client_report_id" => "ENTER_CLIENT_REPORT_ID_HERE",
                    "webhook" => "https://www.example.com/webhook",
                    "user" => [
                            "client_user_id" => "ENTER_USER_ID_HERE",
                            "first_name" => "ENTER_FIRST_NAME_HERE",
                            "middle_name" => "ENTER_MIDDLE_NAME_HERE",
                            "last_name" => "ENTER_LAST_NAME_HERE",
                            "ssn" => "111-22-1234",
                            "phone_number" => "1-415-867-5309",
                            "email" => "ENTER_EMAIL_HERE"
                        ]
                ]
            ])   
]);
svdrlsy4

svdrlsy43#

我找到了答案。我打错了

"access_token" => [ "access-sandbox-xxx" ] ,

应该用“令牌”

"access_tokens" => [ "access-sandbox-xxx" ] ,

且可以正确运行。2谢谢您所有回复。

相关问题