使用php curl从google fcm HTTP v1 api获取411错误响应

时间:2018-05-23 10:16:07

标签: firebase http-headers google-cloud-messaging firebase-cloud-messaging php-curl

我正在尝试将通知从服务器推送到所有Android设备,使用相同的主题调用Google firebase云消息api,但是,http响应说411错误代码代表POST请求需要Content-length标头,但我确实添加在http post请求header中的内容长度。我已经google了问题,并尝试了几种方法,但似乎不起作用。我使用php版本7.0。我使用postman发送请求,它做得很好,即使没有标题:内容类型..

 $topic = 'topic';
    $projectId = 'projectid';
    $title = 'hahaha';
    $content = 'lol';

    $payload = array(
        'message' => array(
            'topic' => $topic,
            'notification' => array(
                'title' => $title,
                'body' => $content,
            )
        )
    );

    $json = json_encode(trim($payload));
    $headers = array(
        'Authorization:Bearer '.$this->getFcmApiAccessToken(),
        'Content-Type: application/json; UTF-8',
        'Content-length:'.strlen($json),//'Content-length:0'
    );

    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,"https://fcm.googleapis.com/v1/projects/{$projectId}/messages:send");
    curl_setopt($ch,CURLOPT_POST, true );
    curl_setopt($ch,CURLOPT_HEADER, 1);
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($ch);
    curl_close($ch);

    var_dump($result);exit;

0 个答案:

没有答案