FCM-GAE中远离curl_init()的实现 [英] FCM - Implementation away from curl_init() in GAE
问题描述
正如我注意到Google不支持Curl实施那样,我必须放弃Curl实施,正如我在帖子中所解释的那样- [
As I could notice Google not supporting Curl implementation, I must move away from Curl implementation as I have explained in the post - [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]
I am trying for URL fetch as an example given at Google cloud platform documentation - [https://cloud.google.com/appengine/docs/standard/php/issue-requests][1]
不过,由于对PHP的了解不足,我无法打破使用FCM的障碍.
Still, I am unable to break the barrier to work with FCM due to poor knowledge in PHP.
下面是我尝试过的-
<?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;
}
?>
但是,尽管有上述问题,但我得到的却是错误-
But despite of above, I am getting below error -
警告:file_get_contents( https://fcm.googleapis.com/fcm/send ):无法打开流:HTTP请求失败!
Warning: file_get_contents(https://fcm.googleapis.com/fcm/send): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in D:\wamp64\www\androidtrials\send_fcm_notification_url_fetch.php on line ##.
我不确定出了什么问题.经过多次搜索,但没有任何结果可以满足我的要求. 是否有人使用PHP完成了FCM消息传递,但没有使用curl?感谢您的关注,寻求帮助.
I am not sure what is going wrong. Been through multiple search, but nothing yielded me to meet my requirement. Has anybody done FCM messaging using PHP, but not using curl ? Thanks for your attention, looking for help.
推荐答案
以下代码有效.我向FCM编写JSON请求的方式不正确.以下是FCM的URL提取(非卷曲)的实现.目前已在客户端WAMP配置上进行了测试.
Below code worked. The way I was composing JSON request to FCM was incorrect. Below is implementation of URL fetch (non-curl) for FCM. At present tested at client WAMP configuration.
<?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进行测试.没有怀疑,为什么它不应该在那儿工作?
To be tested with Google App Engine soon. Suspect no reason, why it shouldnt work there ...
这篇关于FCM-GAE中远离curl_init()的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!