我正在尝试向用户发送通知推送,但无法完成,
有我在php中使用的代码
$message = new gcm();
$message->sendMessageToPhone(2, $message,$valor);
class gcm
{
function sendMessageToPhone($collapseKey, $messageText, $gcmcode)
{
$apiKey = 'there is my apikey';
$headers = array('Authorization:key=' . $apiKey);
$data = array( 'registration_id' => $gcmcode, 'collapse_key' => $collapseKey, 'data.message' => $messageText);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
if ($headers) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) { return 'fail'; }
if ($httpCode != 200) { return 'status code 200'; }
curl_close($ch);
return $response;
}
当我执行 php 时出现该错误
可捕获的致命错误:gcm 类的对象无法在第 25 行的 /path/gcm.php 中转换为字符串
第 25 行 = curl_setopt($ch, CURLOPT_POSTFIELDS, $data);