Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jagdishabhiandroid's avatar

how to take out data from response

'''function sendNotification($token,$dataArr) { $key = env('FCM_KEY',''); $fcmApiKey = $key; $url = 'https://fcm.googleapis.com/fcm/send';

    $registrationIds = $token;//Fcm Device ids array

    $message = $dataArr['message'];
    $title = $dataArr['title'];
    $image=$dataArr['image'];
    $news_id=$dataArr['station_id'];

$random=$dataArr['random_id'];

    $msg = array('message' => $message,'title' => $title,"image"=>$image,"station_id"=>$news_id,"random_id"=>$random);
    $fields = array('registration_ids' => $registrationIds,'data' => $msg);


    $headers = array(
        'Authorization: key=' . $fcmApiKey,
        '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_VERIFYPEER, false );
    curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
    $result = curl_exec($ch );
    // Execute post
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }
    // Close connection
    curl_close($ch);    
   return $result;
   
    
    
}'''
0 likes
7 replies
jagdishabhiandroid's avatar

i am getting response from the api i want to check that if success object has value more then zero how to take that off from the response

{"multicast_id":5497393008180546563, "success":0, "failure":990, "canonical_ids":0, "results":[ {"error":"InvalidRegistration"} ]

Snapey's avatar

no idea what this means

if success object has value more then zero how to take that off from the response

Yorki's avatar
Yorki
Best Answer
Level 11
$json = json_decode($result);

if (isset($json->success) && $json->success) {
    //This call was successfull
}

Btw. think of using Guzzle http client.

1 like
jagdishabhiandroid's avatar

i have tried

$json = json_decode($result);

if (isset($json->success) && $json->success) { //This call was successfull }

but its showing blank no data in it but success has data

jagdishabhiandroid's avatar

stdClass Object ( [multicast_id] => 5112366512282904862 [success] => 0 [failure] => 990 [canonical_ids] => 0 [results] => Array ( [0] => stdClass Object ( [error] => InvalidRegistration )

i want to get that success object but its not working even after decoding the json format

Please or to participate in this conversation.