尝试在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>
$data = json_encode( [
"title" => "Hello World",
"message" => $message,
"installIds" => [ $deviceToken, ],
] );
{
"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
}
答案 0 :(得分:2)
您需要设置curlopt_followlocation => 1
。还有什么期待回归?如果您正在考虑除了真实值之外的其他内容,则需要删除curlopt_returntransfer
。此外,如果您发送帖子,请使用CURLOPT_POST => 1
示例强>
$ch = curl_init( 'https://push.kumulos.com/notifications' );
curl_setopt_array( $ch, [
CURLOPT_POST => 1,
CURLOPT_HEADER => [
'Content-Type: application/json',
'Accept: application/json',
'Content-Length: ' . strlen( $data ),
],
CURLOPT_USERPWD => 'xxx:xxx',
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_POSTFIELDS => $data,
] );
$resp = htmlentities( curl_exec( $ch ) );
因为您正在使用
htmlentities(...)
我绝对假设你需要删除 CURLOPT_RETURNTRANSFER 。
CURLOPT_RETURNTRANSFER
TRUE,将传输作为curl_exec()返回值的字符串返回,而不是直接输出。
CURLOPT_FOLLOWLOCATION
是否可以关注任何&#34;位置:&#34;服务器作为HTTP标头的一部分发送的标头(注意这是递归的,PHP将遵循尽可能多的&#34;位置:&#34;它被发送的标头,除非设置了CURLOPT_MAXREDIRS)。
解决用户的新信息。请Kumulos文档。你确定这是一个帖子请求而不是一个put请求吗?您需要发送API密钥吗?你可以发布你要发送的JSON数据吗?
从我们的对话中看来,在实施更改后,您现在正在接收200而不是3XX。请相应更新问题。现在问题出在我相信的数据中
答案 1 :(得分:1)
来自Kumulos技术支持的Mark。
问题是由请求中缺少HTTP标头引起的。
使用 CURLOPT_HEADER
(true
或false
)来表明标头是否会包含在请求中。
要实际设置您需要使用的标头CURLOPT_HTTPHEADER
如果没有标题,请求将被重定向,如您所见。
请在下面找到一些示例PHP代码,使用Kumulos Push API向您应用的所有订阅安装发送广播推送。
#!/usr/bin/php
<?php
$postData = json_encode( array (
"broadcast" => true,
"title" => "Test Broadcast",
"message" => "Test from PHP" )
);
$curl = curl_init();
curl_setopt_array( $curl, [
CURLOPT_URL => "https://push.kumulos.com/notifications",
CURLOPT_HTTPHEADER => array (
'content-type: application/json',
'accept: application/json',
'content-length: ' . strlen($postData),
),
CURLOPT_USERPWD => 'apiKey:serverKey',
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $postData,
CURLOPT_RETURNTRANSFER => true
] );
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>