FCM could not be sent on GAE due to PHP error - ca

2019-08-22 02:45发布

I am attempting to send Firebase Cloud Message via PHP setup at Google App Engine (GAE). In my local testing environment (WAMP), I could successfully have FCM delivered with below code -

<?php

// Establishing FCM connection here to send a token received over to another device.
function send_fcm_notification($tokens,$message) {

    $url = 'https://fcm.googleapis.com/fcm/send';
    $fields = array('registration_ids' => $tokens,
                    'data' => $message);

    $headers = array('Authorization:key =<server key allocated to my app>',
                     'Content-Type: application/json');// Use the server key you have it for your Google Account

    $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('Curl failed: '. curl_error($ch));
    }
    curl_close($ch);

    return $result;
}

But if I have above deployed at Google App engine, as I say in subject, I am getting - PHP Fatal error : Call to undefined function curl_init() As I have investigated on the Web, below is what I found should help. http://terrenceryan.com/blog/index.php/php-on-app-engine-does-curl/ I have chosen to enable the cURL_Lite in php.ini. For this I have created new php.ini having entry for -

google_app_engine.enable_curl_lite = "1"

But, after uploading to GAE still it didnt help. The above change is done by analyzing / understanding multiple threads on the same topic at stackoverflow. Since I did not have php.ini; I have created the one which is placed at same location of app.yaml. No changes have been made at app.yaml. Below is my app.yaml -

runtime: php55
api_version: 1

handlers:

# Serve PHP scripts.
- url: /(.+\.php)$
  script: \1

- url: /.*
  script: index.php

Is there anything which I am overlooking ? What I understand is, by enabling curl lite, we dont have to change the code mentioned for FCM transfer. Or, Has anybody implemented FCM via PHP sitting on Google App Engine ? I could understand curl_init() is not option, but has anybody implemented with PHP calls like - stream_context_create & file_get_contents ? Thanks in advance.

0条回答
登录 后发表回答