Php 尝试连接到URL时,curl_exec返回false
我正在尝试创建一个推送通知应用程序,它将从服务器向用户设备发送消息 现在我得到注册ID,但当我运行curl_exec函数时,我得到以下错误:Php 尝试连接到URL时,curl_exec返回false,php,android,Php,Android,我正在尝试创建一个推送通知应用程序,它将从服务器向用户设备发送消息 现在我得到注册ID,但当我运行curl_exec函数时,我得到以下错误: Curl failed: couldn't connect to host 我不知道为什么会这样,我做了一些检查,当我尝试将URL更改为“google.com”时,我的回答是通过。 从我的浏览器,如果我尝试访问原始URL,它会将我重定向到: "" 这是php代码: <?php class GCM { function __cons
Curl failed: couldn't connect to host
我不知道为什么会这样,我做了一些检查,当我尝试将URL更改为“google.com”时,我的回答是通过。
从我的浏览器,如果我尝试访问原始URL,它会将我重定向到:
""
这是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;
}
}
?>
我发现我的服务器包含防火墙,它阻止了我的传出连接。所以我现在就打开它,它开始工作了。 这是帮助我解决问题的解决方案,链接到相关答案 卷曲错误代码7(卷曲无法连接) 是非常明确的。。。这意味着无法将()连接到主机或代理 以下代码适用于任何系统:
$ch = curl_init("http://google.com"); // initialize curl handle
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
print($data);
如果你看不到谷歌页面,那么。。您的URL错误,或者您有防火墙或限制问题