gpt4 book ai didi

php - GCM Curl 连接错误

转载 作者:行者123 更新时间:2023-11-29 21:16:47 24 4
gpt4 key购买 nike

我正在尝试使用 GCM 和 curl 从服务器向客户端 (android) 发送消息,我使用的代码非常适用于本地主机(模拟器和设备),但是它不适用于常规服务器,附件是使用的代码和错误消息,

是否需要执行任何其他步骤或更改才能使代码适用于在线服务器。任何帮助表示赞赏:)

<?php

function Reg($USER_REG_ID)
{
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$response = array();
$API_Key = 'AIzaSyCQgPGhpAzuWGuUd0DCI8pYaXXAItthEsg';
$url = 'https://android.googleapis.com/gcm/send';


$result = mysql_query("SELECT * FROM users where type = 'bus'") or die(mysql_error());

while ($row = mysql_fetch_array($result)) {

$Registration_ID = $row["user_RegID"];
array_push($response, $Registration_ID);

}

//THIS PART SENDS TEH REQUEST USING GCM
$fields = array(
'registration_ids' => $response,
'data' => array(
"message" => $USER_REG_ID
)
);

print_r ($fields);

$headers = array(
'Authorization: key=' . $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_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);


}
Reg(1)
?>

字段数组

Array ( [registration_ids] => Array ( [0] => APA91bFolV5ZK2diIbTyxwPLp6BqTMdhqaMDYgEsGOlznmxn4NRV6QQzPtfr58ORrpmxq0N_UMwz6s30gWgj--w5SqHxCEHh662PnvjDpGce_4EiHDe-muflVYMOsLlbVDRybvNDubX9aF_RE4VPJdArUsqQjcrQRg ) [data] => Array ( [message] => 1 ) )

错误信息

Curl failed: Failed to connect to 2a00:1450:400c:c05::5f: Network is unreachable

最佳答案

应该这样做:

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

如果上述方法不起作用,您可以尝试在系统范围内禁用 ipv6:

echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6

关于php - GCM Curl 连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21212395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com