0

I’ve got Parse error: syntax error, unexpected ‘public’ (T_PUBLIC), when I’ve tried to run this script.

<?php
/*  
Parameter Example
    $data = array('post_id'=>'12345','post_title'=>'A Blog post');
    $target = 'single token id or topic name';
    or
    $target = array('token1','token2','...'); // up to 1000 in one request
*/
public function sendMessage($data,$target){
    //FCM api URL
    $url = 'https://fcm.googleapis.com/fcm/send';
    //api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
    $server_key = AAAABF8gcMk:APA91bFd2Uiq1GZL2VmyqPoqSv_KOyvHNiKTLOGdYxKhTMUzjlhXosrqRPvOzcnxh4Y-BKPyLxjCYQ0dTmQPApIv87uL_sTbrOOenO2wQuAsBQO605UuCX_uL5kKSlCd4hnRibdTm;


    $fields = array();
    $fields['data'] = $data;
    if(is_array($target)){
        $fields['registration_ids'] = $target;
    }else{
        $fields['to'] = $target;
    }
    //header with content_type api key
    $headers = array(
        'Content-Type:application/json',
      'Authorization:key='.$server_key
    );

    $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('FCM Send Error: ' . curl_error($ch));
    }
    curl_close($ch);
    return $result;
}
$message = array(
          "id"=>$guide->getId(),
          "title"=>$guide->getTitle(),
          "time"=>$view['time']->diff($guide->getCreated()),
        "category"=>$guide->getCategory()?$guide->getCategory()->getTitle(): null,
            "image"=> $this['imagine']->filter($view['assets']->getUrl($guide->getMedia()->getLink()), 'api_img'),
        );
$key=$this->container->getParameter('fire_base_key');
$message_status = send_notification($tokens, $message,$key);
echo $message_status;
header("Location: ".$view['router']->path('app_guides_view',array("id"=>$guide->getId())));
die();

 ?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • public is used to scope a class method, not a function – RiggsFolly Jul 31 '17 at 23:24
  • `public` is a method and property access modifier that can only be used in the context of a class definition in PHP - you're not doing that here. Remove the `public` keyword. – Darragh Enright Jul 31 '17 at 23:25
  • `public function sendMessage($data,$target){` remove `public` from it. Sorry i saw that a bit late – Alive to die - Anant Jul 31 '17 at 23:25
  • after remove public now error is Parse error: syntax error, unexpected ':' in /home1/news/login/src/AppBundle/Resources/views/Guides/notif.html.php on line 13 – Mithun Deshmukh Jul 31 '17 at 23:29
  • `$server_key = AAAAB....bdTm;` that's considered as a constant here http://php.net/manual/en/function.constant.php and it should be wrapped in quotes. The dupllcate questions that were used to close this with, contains everything you need to debug this. – Funk Forty Niner Jul 31 '17 at 23:45

0 Answers0