Curl 请求在终端中有效但在 php 中无效

Curl request working in terminal but not in php

尝试在 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 中我被重定向,而在命令行中我得到预期的响应...

更新

它 returns 这个 HTML 在 PHP 中,而在命令行中我得到预期的 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
}

您需要设置curlopt_followlocation => 1。另外,您对 return 有何期待?如果您希望的不是真实的虚假值,则需要删除 curlopt_returntransfer。此外,如果您要发送 post,只需使用 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 to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.


CURLOPT_FOLLOWLOCATION

TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set).


编辑

处理来自用户的新信息。请 Kumulos 文档。您确定这是 post 请求而不是看跌请求吗?您需要发送 API 密钥吗?您可以 post 您正在发送的 JSON 数据吗?

从我们的谈话来看,实施更改后您现在收到的是 200 而不是 3XX。请相应地更新问题。我相信现在的问题出在您的数据中

来自 Kumulos 技术支持的标记。

问题是由于请求中没有HTTPheaders造成的。

CURLOPT_HEADER用于(truefalse)表示请求中是否包含headers。

要实际设置 headers,您需要使用 CURLOPT_HTTPHEADER

如果没有 headers,请求将被重定向,如您所见。

请在下面找到一些示例 PHP 使用 Kumulos 推送 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;
}
?>