错误401:Firebase推送通知获取401错误

时间:2017-03-30 12:04:27

标签: php curl firebase firebase-authentication firebase-cloud-messaging

当尝试通过https:// URI访问时,我遇到了firebase的以下问题,但相同的代码可以正常使用http:// URI:

错误401:无效(传统)已发送的服务器密钥或发件人无权执行请求。

// firebase服务器url发送curl请求。

    $url = 'https://fcm.googleapis.com/fcm/send';

    //building headers for the request
    $headers = array(
        'Authorization: key=' . FIREBASE_API_KEY,
        'Content-Type: application/json'
    );

    //Initializing curl to open a connection
    $ch = curl_init();
    //Setting the curl url
    curl_setopt($ch, CURLOPT_URL, $url);
    //setting the method as post
    curl_setopt($ch, CURLOPT_PORT, 443);
    curl_setopt($ch, CURLOPT_POST, true);        
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //disabling ssl support
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    //adding headers 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    //adding the fields in json format 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    //finally executing the curl request 
    $result = curl_exec($ch);
    /*if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }*/        
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($status != 201) {
        echo "Error123: call to URL $url failed with status $status, response $result, curl_error " . curl_error($ch) . ", curl_errno " . curl_errno($ch) . '<br>';
    }

    //Now close the connection
    curl_close($ch);

1 个答案:

答案 0 :(得分:0)

从firebase获得解决方案(域SSL问题)......并且工作正常!