0
我需要连接到一个API来声明一个优惠券。在API文档我看到:Laravel函数只返回NULL
POST /api/v1/vouchers/{voucherId}/claim
{
"Participant": {
"FirstName": "John",
"LastName": "James,
"Telephone": "08456 127 127",
"EmailAddress": "[email protected]",
"PostCode": "ASD 9HX",
"HouseNameNumber": "2",
"Street": "Bridge Road2",
"Locality": "LONDON",
"Town": "Aylesbury",
"County": "Bucks"
},
"ExperienceDate": "2017-10-01T00:00:00"
}
在此基础上我使用Laravel框架写我的功能:
public function testclaim()
{
$client = new GuzzleHttp\Client;
$headers = ['Content-Type' => 'application/json'];
try {
$res = $client->post('https://api.example.com/api/v1/vouchers/244775_2-H8SC/claim', [
'headers'=>$headers,
'auth' => [
'JAMES-JJ', 'ajhsdajsdhaj32423'
],
'json' => [
'Participant' => [
"FirstName"=> "asdasd",
"LastName"=> "asdasd",
"Telephone"=> "08456 127 127",
"EmailAddress"=> "[email protected]",
"PostCode"=> "HP18 9HX",
"HouseNameNumber"=> "1",
"Street"=> "Bridge Road",
"Locality"=> "Ickford",
"Town"=> "Aylesbury",
"County"=> "Bucks"
],
'ExperienceDate' => '2017-11-01T00:00:00'
]
]);
$res = json_decode($res->getBody()->getContents(), true);
return response()->json(['data' => $res]);
//dd($res);
}
catch (GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$result = json_decode($response->getBody()->getContents());
return response()->json(['data' => $result]);
}
}
现在,当我运行这个功能,我只是得到:
{"data":null}
人看我的代码有什么问题? 如何解决这个问题?
我也尝试没有头来发送请求,但我再次从API获得相同的响应。
所以做一个调试。你知道这是什么? '$ res'的价值是什么?在'json_decode'之前,'json_decode'之后。 –
'$ res'的输出是什么? – ajtrichards
做一个var的res_dump()或dd()res来查看API是否返回了一些东西,如果它可能只是返回错误的东西。 – Peter