FCM - 远离GAE中的curl_init()的实现

时间:2017-10-08 13:14:42

标签: php google-app-engine firebase-cloud-messaging

正如我可以注意到Google不支持Curl实现,我必须放弃Curl实现,正如我在帖子中解释的那样 - [FCM could not be sent on GAE due to PHP error - call to undefined function curl_init()

我正在尝试将网址提取作为Google云平台文档中的示例 - [https://cloud.google.com/appengine/docs/standard/php/issue-requests][1]

但由于PHP知识不足,我无法打破使用FCM的障碍。

下面是我的尝试 -

<?php

// Establishing FCM connection here to send a token received over to another device.
function send_fcm_notification_url_fetch    ($tokens,$message) {

    $url = "https://fcm.googleapis.com/fcm/send";
    $fields = array('registration_ids' => '<device_token_id>', 
    'data' => 'Hi');
    $queryParam = http_build_query($fields);


    $headers = array('Authorization:key = <firebase_server_key>',
                     'Content-Type: application/json');


    $context = array('http' => array( 'method' => 'POST', 'header' => $headers, 'content' => $queryParam ) );

$context = stream_context_create($context);
$result = file_get_contents($url,false,$context);
echo $result;
return $result;
}

?>

但是尽管如此,我仍然低于错误 -

警告:file_get_contents(https://fcm.googleapis.com/fcm/send):无法打开流:HTTP请求失败!位于##的D:\ wamp64 \ www \ androidtrials \ send_fcm_notification_url_fetch.php中的HTTP / 1.0 400错误请求。

我不确定出了什么问题。通过多次搜索,但没有任何东西让我满足我的要求。 有没有人使用PHP完成FCM消息传递,但不使用curl?感谢您的关注,寻求帮助。

1 个答案:

答案 0 :(得分:0)

下面的代码工作。我向FCM撰写JSON请求的方式不正确。以下是FCM的URL提取(非卷曲)的实现。目前已在客户端WAMP配置中进行了测试。

<?php

// Establishing FCM connection here to send a token received over to another device.
function send_fcm_notification_url_fetch    ($tokens,$message) {

    $url = "https://fcm.googleapis.com/fcm/send";
    $fields = array('registration_ids' => $tokens, 'data' => $message);

    $headers = "Authorization:key = <firebase_server_key>\r\n".
                     "Content-Type: application/json\r\n".
                     "Accept: application/json\r\n";

    $postData = json_encode($fields);

    $context = array('http' => array( 'method' => 'POST', 'header' => $headers, 'content' => $postData ) );

$context = stream_context_create($context);
$result = file_get_contents($url,false,$context);

return $result;
}

?>

即将使用Google App Engine进行测试。怀疑没有理由,为什么它不应该在那里工作......