我对Guzzle发出了这样的POST请求:
// Return a collection
$cart = $this->getCart('2019-10-08 07:08:39');
//Return first entry of the collection with first()
$template = $this->getTemplate($config->key);
$isDetail = null;
foreach ($cart as $item) {
try {
$client = $this->getClient();
$headers = ['Content-Type' => 'application/json'];
$body = [
'user_id' => $item->mystore_user_id,
'title' => $template->title,
'message' => $template->message,
'avatar' => $template->avatar,
'detail_id' => $isDetail,
'schedule' => null
];
print_r($body);
$response = $client->post('push-noti/unicast', $headers, $body);
print_r(response()->json(json_decode($response->getBody(), true)));
} catch (QueryException | \Exception $ex) {
echo "Error!";
}
}
我的身体变量值在打印时存在于每个循环中。但是,当我在$ client-> post中使用它时,我的请求返回错误必须是user_id,title,message。我真的不知道为什么吗?
您能告诉我我的代码有什么问题吗?
谢谢!
答案 0 :(得分:0)
尝试
$response = $client->post('push-noti/unicast', ['body' => $body , 'headers' => $headers]);
如果您正在调用第三方API,请用完整的URL替换push-noti/unicast
。