简体   繁体   中英

Curl fails to send FCM message

I am trying to push notification to my android application using Firebase . I have tried it on my local system and it is working fine. this is my code.

 function sendPushNotiication($tokens,$message){
    $url='https://fcm.googleapis.com/fcm/send';
    $fields= array(
        'registration_ids'=>$tokens,
        'data'=>$message
    );
    $headers=array(
        'Authorization: key='.GOOGLE_API_KEY,
        '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);
    var_dump($result);
    if($result==FALSE){
        die('curl failed: '. curl_error($ch));
    }

    curl_close($ch);
    return $result;
}

I have called this sendPushNotification() method to send the data to firebase. Problem is when i put this online on my server. it is giving NULL in $result at var_dump. and after it die with

curl failed.

What is the problem with curl it should be working if it is working on local system.

i have also executed phpinfo() and found that

cURL support    enabled
cURL Information    libcurl/7.21.0 OpenSSL/0.9.8q zlib/1.2.3
function sendPushNotification($registration_ids, $message) {
            // Set POST variables
        $url = 'Url';

        $fields = array();
        $fields['data'] = $message;
        if(is_array($registration_ids)){
        $fields['registration_ids'] = $registration_ids;
        }else{
        $fields['to'] = $registration_ids;
        }

        $headers = array(
            'Authorization: key=' . GOOGLE_API_KEY,
            'Content-Type: application/json'
        );
        // Open connection
        $ch = curl_init();

        // Set the url, number of POST vars, POST data
        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);
        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }
        // Close connection
        curl_close($ch);
        return $result;
}

just verify ur code with this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM