Curl无法发送FCM消息

时间:2016-08-04 16:05:44

标签: php curl firebase-cloud-messaging

我正在尝试使用 Firebase 将通知推送到我的Android应用程序。我在我的本地系统上尝试过,它工作正常。这是我的代码。

 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;
}

我调用了这个sendPushNotification()方法将数据发送到firebase。 问题是当我把它放在我的服务器上。它在var_dump中的$ result中给出NULL。在它死后

卷曲失败。

如果curl在本地系统上工作,它应该有什么问题。

我还执行了 phpinfo()并找到了

cURL support    enabled
cURL Information    libcurl/7.21.0 OpenSSL/0.9.8q zlib/1.2.3

1 个答案:

答案 0 :(得分:0)

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;
}

只需用此验证你的代码。