pushcrew - 用PHP curl发送通知

时间:2017-11-11 17:36:37

标签: php curl

这是代码:

    $title = 'Du hast neue Nachricht';
    $message = 'Besuch meine Website';
    $url = 'https://www.bla.com';
    $subscriberId = 'xxx51a002dec08a1690fcbe6e';

    $apiToken = 'xxxe0b282d9c886456de0e294ad';

    $curlUrl = 'https://pushcrew.com/api/v1/send/individual/';

    //set POST variables
    $fields = array(
      'title' => $title,
      'message' => $message,
      'url' => $url,
      'subscriber_id' => $subscriberId
    );

    $httpHeadersArray = Array();
    $httpHeadersArray[] = 'Authorization: key='.$apiToken;

    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $curlUrl);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
    curl_setopt($ch, CURLOPT_HTTPSHEADER, $httpHeadersArray);

    //execute post
    $result = curl_exec($ch);

    $resultArray = json_decode($result, true);

    if($resultArray['status'] == 'success') {
        echo $resultArray['request_id']; //ID of Notification Request
    }
    else if($resultArray['status'] == 'failure')
    {
        echo 'fail';
    }
    else
    {
        echo 'dono';
    }

    echo '<pre>';
    var_dump($result);
    echo '</pre>';

我得到了:

dono
string(36) "{"message":"You are not authorized"}"

控制台中没有任何内容,也没有其他错误。 apitoken 100%正确。这可能是什么麻烦?我是否必须等到pushcrew决定允许我的网站或什么?

忽略这一点:我必须添加更多文字才能提出这个问题。

1 个答案:

答案 0 :(得分:0)

这里有拼写错误:

curl_setopt($ch, CURLOPT_HTTPSHEADER, $httpHeadersArray);

正确与

CURLOPT_HTTPHEADER

(没有S