发送通知消息给android设备curl错误 [英] Sending notification message to android device curl error
本文介绍了发送通知消息给android设备curl错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Curl失败:无法连接到android.googleapis.com端口443:
连接超时
我的服务器是亚马逊ec2。
我的代码:
public function send_notification($ registatoin_ids,$ message){
//设置POST变量
$ url ='https://android.googleapis.com/gcm/send';
$ fields = array(
'registration_ids'=> $ registatoin_ids,
'data'=> $ message,
);
$ headers [] ='Content-Type:application / json';
$ headers [] ='Authorization:key = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
//打开连接
$ ch = curl_init();
//设置网址,POST变量的数量,POST数据
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_POST,true);
curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,false);
curl_setopt($ ch,CURLOPT_HEADER,true);
//临时禁用SSL证书支持
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ ch,CURLOPT_POSTFIELDS,json_encode($ fields));
//执行后
$结果= curl_exec($ ch);
if($ result === FALSE){
die('Curl failed:'。curl_error($ ch));
}
//关闭连接
curl_close($ ch);
echo $ result;
}
我刚试过其他服务器上的代码
{multicast_id:5322863685330856855,success:1,failure:0,canonical_ids:0,
results :[{message_id:0:1401820119301203%a5dcde78f9fd7ecd}]}
amazon ec2设置有问题
解决方案
我发现问题我的代码是正确的,端口443未在服务器上打开
I'am trying to send notification message to android device using the code below the out put form the result is
Curl failed: Failed to connect to android.googleapis.com port 443:
Connection timed out
my server is amazon ec2.
my code :
public function send_notification($registatoin_ids, $message) {
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers[] = 'Content-Type:application/json';
$headers[] = 'Authorization:key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_HEADER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
I just tried the code on another server it's working
{"multicast_id":5322863685330856855,"success":1,"failure":0,"canonical_ids":0,
"results":[{"message_id":"0:1401820119301203%a5dcde78f9fd7ecd"}]}
so i think there is something wrong with amazon ec2 settings
解决方案
I figured out the problem my code was correct, port 443 was not open on the sever
这篇关于发送通知消息给android设备curl错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文