【发布时间】:2017-06-16 04:11:17
【问题描述】:
尝试在 php 中执行此操作
curl -X POST
-H "Content-Type: application/json"
-H "Accept: application/json"
-u xxx:xxx
-d '{
"broadcast": true,
"title": "Hello World",
"message": "from Kumulos Push"
}' "https://push.kumulos.com/notifications"
在php中我有这个...
$ch = curl_init( 'https://push.kumulos.com/notifications' );
curl_setopt_array( $ch, [
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HEADER => [
'Content-Type: application/json',
'Accept: application/json',
'Content-Length: ' . strlen( $data ),
],
CURLOPT_USERPWD => 'xxx:xxx',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $data,
] );
$resp = htmlentities( curl_exec( $ch ) );
仍然在 php 中我被重定向,而在命令行中我得到预期的响应......
更新
它在 PHP 中返回这个 HTML,而在命令行中我得到预期的 JSON
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="1;url=https://push.kumulos.com" />
<title>Redirecting to https://push.kumulos.com</title>
</head>
<body>
Redirecting to <a href="https://push.kumulos.com">https://push.kumulos.com</a>.
</body>
</html>
更新:通过 php 请求发送的数据
$data = json_encode( [
"title" => "Hello World",
"message" => $message,
"installIds" => [ $deviceToken, ],
] );
更新:预期输出(在命令行 curl 中收到)
{
"appId":9999,
"source":2,
"status":1,
"filters":{
"installIds":[
"xxx"
]
},
"title":"Test",
"message":"3 new questions on Tomorrow Times!",
"data":null,
"isBackgroundData":false,
"url":null,
"targetedRecipients":0,
"expectedResolutionSteps":0,
"completedResolutionSteps":0,
"expectedBatches":0,
"completedBatches":0,
"updatedAt":"2017-06-16T04:58:54+0000",
"createdAt":"2017-06-16T04:58:54+0000",
"id":13976
}
【问题讨论】: