目前,我正在尝试使用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“,“错误(已截断...)
我的狂饮代码缺少了什么。
请帮帮忙。
3条答案
按热度按时间ioekq8ef1#
Laravel提供了Http客户端,它是一个围绕Guzzle的 Package 器:
dfuffjeb2#
您应该尝试将
json
替换为body
:此外,将数组转换为有效的JSON格式。
参考:https://stackoverflow.com/a/39525059/5192105
svdrlsy43#
我找到了答案。我打错了
应该用“令牌”
且可以正确运行。2谢谢您所有回复。