我正在尝试创建一个推送通知应用程序,它将从服务器向用户设备发送消息。
现在,我得到注册ID,但是当我运行curl_exec函数时,出现以下错误:
Curl failed: couldn't connect to host
我不知道为什么,我进行了一些检查,当我尝试将URL更改为“ google.com”时通过了。从我的浏览器中,如果我尝试到达原始URL,则将其重定向到:“ https://developers.google.com/cloud-messaging/ ”这是php代码:
<?php
class GCM {
function __construct() {
}
/**
* Sending Push Notification
*/
public function send_notification($registatoin_ids, $message) {
// include config
include_once 'connection.php';
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' .GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 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;
}
}
?>
我发现我的服务器包含阻止我的传出连接的防火墙。所以我只是打开它,现在开始工作。这是通过链接到相关答案帮助我解决问题的解决方案。
CURL错误代码7(CURLE_COULDNT_CONNECT)非常明确...这表示无法将connect()连接到主机或代理。
以下代码可在任何系统上运行:
$ch = curl_init("http://google.com"); // initialize curl handle
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
print($data);
如果看不到Google页面,则说明您的网址错误,或者您遇到了防火墙或限制问题
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句