简体   繁体   中英

PHP Fatal error: Call to undefined function curl_init() on GCP platform

I want to send a push notification from my GCP server using PHP. I used the following function to achieve this. When I run the same script in my local Xampp Server it works fine. However when I deploy on the GCP server its is giving the error.

PHP Fatal error: Call to undefined function curl_init() in

Here is my code:

function sendMessage($appId,$userId){
    $content = array(
                     "en" => "Welcome ",
                     );

    $fields = array(
                    'app_id' => $appId,

                    'include_player_ids' => [$userId],

                    'contents' => $content,

                    ); 


    $fields = json_encode($fields);
    print("\nJSON sent:\n");
    print($fields);

    $ch = curl_init(); // Showing error here
    curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
                                               'Authorization: Basic xxxxxxx'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
}

How can I solve this error?

Add next php code to check is curl enabled before $ch = curl_init();

// Script to test if the CURL extension is installed on this server

// Define function to test
function _is_curl_installed() {
    if  (in_array  ('curl', get_loaded_extensions())) {
        return true;
    }
    else {
        return false;
    }
}

// Ouput text to user based on test
if (_is_curl_installed()) {
  echo "cURL is <span style=\"color:blue\">installed</span> on this server";
} else {
  echo "cURL is NOT <span style=\"color:red\">installed</span> on this server";
}

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