0

我有一个问题,我有一个有效的 PHP firebase 通知脚本,但我仍然通过 XAMPP 在本地发送通知,如果我希望我的 RoR 服务器发送通知,我是否必须将整个 PHP 脚本重新编码为 ruby​​ 脚本? 或者我可以直接从 RoR 服务器调用我的 php 脚本吗?

下面是我的工作 php 脚本。

function send_notification($tokens, $message)
{
$url = "https://fcm.googleapis.com/fcm/send";
$fields = array(
    'registration_ids' => $tokens,
    'data' => array("message"=> $message) ,
);

$headers = array(
    'Authorization:key = AuthorizationKey',
    'Content-Type: application/json'
);

$ch = curl_init();
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_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields));

$result = curl_exec($ch);
if ($result === FALSE)
{
die('Curl Failed:' . curl_error($ch));
}
curl_close($ch);

return $result;
}

$conn = mysqli_connect("localhost","root","","demoflying");    

$sql = "Select Token From Users";

$result = mysqli_query($conn,$sql);
$tokens = array();

if(mysqli_num_rows($result)>0)
{
    while ($row = mysqli_fetch_assoc($result))
    {
        $tokens[] = $row["Token"];
    }
}

mysqli_close($conn);

$fromCountry = "UK";
$toCountry= "US";

$message = array(
    "FromCountry" => $fromCountry,
    "ToCountry" => $toCountry,
); 
$message_status = send_notification($tokens,$message);
echo $message_status;
?>
4

1 回答 1

0

php #{RAILS_ROOT}/public/php_script.php可以在控制器中运行 php 脚本编写此脚本

于 2016-07-12T06:33:15.137 回答