简体   繁体   English

PHP 致命错误:在 GCP 平台上调用未定义的 function curl_init()

[英]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.我想使用 PHP 从我的 GCP 服务器发送推送通知。 I used the following function to achieve this.我使用以下 function 来实现这一点。 When I run the same script in my local Xampp Server it works fine.当我在本地 Xampp 服务器上运行相同的脚本时,它工作正常。 However when I deploy on the GCP server its is giving the error.但是,当我在 GCP 服务器上部署时,它给出了错误。

PHP Fatal error: Call to undefined function curl_init() in PHP 致命错误:调用未定义的 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();添加下一个 php 代码以检查 curl 在 $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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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