使用PHP执行cURL以发送推送通知

时间:2017-04-19 17:22:42

标签: php curl firebase push-notification firebase-cloud-messaging

我正在尝试发送cURL命令来发送推送通知。我对cURL很新,似乎无法运行命令。

这是cURL命令行:

curl --header "Authorization: key=AAAAVD27CWY:APA91bE7YdKYiqTmQhErf0E3gm8lbgNt2KP5-xPQf83V7m8eKsa0ljktOLiGyzzrP0uxVNBHC6cyuJAPejkTyNl1DnoxcajesLvGXIzq3YR1l-wiFvoivRmIUkDvThTsKCJkZMomhEPp" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"fr1l051Pczw:APA91bGkyuA6iKMP6oICJ8NweijQpWTWGuo-inqvpF5-Mety0D7oL_ppvevKKdWPxIo7ev_v5sAWbprk7pEg8kz3cNCivipL9RCR3XlA1caBtahsRtnANZpaU-KYnsdjcGY3Q51xN1ny\"]}"

这是我正在尝试执行的PHP cURL:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"registration_ids\":[\"fr1l051Pczw:APA91bGkyuA6iKMP6oICJ8NweijQpWTWGuo-inqvpF5-Mety0D7oL_ppvevKKdWPxIo7ev_v5sAWbprk7pEg8kz3cNCivipL9RCR3XlA1caBtahsRtnANZpaU-KYnsdjcGY3Q51xN1ny\"]}");
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "Authorization: key=AAAAVD27CWY:APA91bE7YdKYiqTmQhErf0E3gm8lbgNt2KP5-xPQf83V7m8eKsa0ljktOLiGyzzrP0uxVNBHC6cyuJAPejkTyNl1DnoxcajesLvGXIzq3YR1l-wiFvoivRmIUkDvThTsKCJkZMomhEPp";
$headers[] = "Content-Type: \"application/json\"";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

我正在使用Google的Firebase云消息传递来运行此功能。

我可以在我的服务器上执行该命令,所以我至少知道它有效。

运行UBUNTU 16.04和Apache。 我在服务器上运行SSL并尝试添加:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

仍然没有运气。

1 个答案:

答案 0 :(得分:4)

请使用以下PHP卷曲请求

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://android.googleapis.com/gcm/send",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"registration_ids\":[\"fr1l051Pczw:APA91bGkyuA6iKMP6oICJ8NweijQpWTWGuo-inqvpF5-Mety0D7oL_ppvevKKdWPxIo7ev_v5sAWbprk7pEg8kz3cNCivipL9RCR3XlA1caBtahsRtnANZpaU-KYnsdjcGY3Q51xN1ny\"]}",
  CURLOPT_HTTPHEADER => array(
    "authorization: key=AAAAVD27CWY:APA91bE7YdKYiqTmQhErf0E3gm8lbgNt2KP5-xPQf83V7m8eKsa0ljktOLiGyzzrP0uxVNBHC6cyuJAPejkTyNl1DnoxcajesLvGXIzq3YR1l-wiFvoivRmIUkDvThTsKCJkZMomhEPp",
    "content-type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}