gpt4 book ai didi

php - 从 MySQL 数据库循环 CURL 脚本中的 FCM token

转载 作者:行者123 更新时间:2023-11-29 09:59:46 32 4
gpt4 key购买 nike

我有一个连接到 Firebase 以便发送推送通知的curl 脚本,但问题是该通知仅发送到一个注册 token ,而实际上它应该发送到多个注册 token (设备)

下面是CURL代码

<?php

require "init.php";
$body=$_POST['message'];
$title=$_POST['title'];
$url=$_POST['url'];


$path_to_fcm='https://fcm.googleapis.com/fcm/send';
$server_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxs";

$sql="SELECT * FROM fcm_info";
$result=mysqli_query($con,$sql);

while($row = mysqli_fetch_array($result)){
$key=$row[1];

}

$headers=array(

'Authorization:key=' .$server_key,
'Content-Type:application/json'

);

$message = array(
'title' => $title,
'body' => $body,
'webUrl' => $url,
'vibrate' => 1,
'sound' => 1
);

$fields = array(

'to' =>$key,
'data' => $message,
'priority'=>'high'
);

$payload=json_encode($fields);

$curl_session=curl_init();
curl_setopt($curl_session, CURLOPT_URL, $path_to_fcm);
curl_setopt($curl_session, CURLOPT_POST, true);
curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_session, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload);

$result=curl_exec($curl_session);

mysqli_close($con);

?>

最佳答案

我注意到的一件事是您将curl 结果存储到变量$result 中。这是查询结果所在的同一变量,因此替换了它的值。这会引起问题。

尝试一下。

require 'init.php';
$body = $_POST['message'];
$title = $_POST['title'];
$url = $_POST['url'];


$path_to_fcm = 'https://fcm.googleapis.com/fcm/send';
$server_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxs";

$sql="SELECT * FROM fcm_info";
$result = mysqli_query($con, $sql);


$headers = array(

'Authorization:key=' . $server_key,
'Content-Type:application/json'

);

$message = array(

'title' => $title,
'body' => $body,
'webUrl' => $url,
'vibrate' => 1,
'sound' => 1

);

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

$fields = array(

'to' => $row[1],
'data' => $message,
'priority' =>'high'

);

$payload = json_encode($fields);

$curl_session = curl_init();

curl_setopt($curl_session, CURLOPT_URL, $path_to_fcm);
curl_setopt($curl_session, CURLOPT_POST, true);
curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_session, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload);

$curlResults[] = curl_exec($curl_session); //Changed the name of variable so it
//did not overwrite your query results.

mysqli_close($con);

}

关于php - 从 MySQL 数据库循环 CURL 脚本中的 FCM token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53244702/

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