PHP Фатальная ошибка: вызов неопределенной функции curl_init () на платформе GCP - PullRequest
0 голосов
/ 20 апреля 2020

Я хочу отправить уведомление pu sh с моего сервера GCP, используя PHP. Я использовал следующую функцию для достижения этой цели. Когда я запускаю тот же скрипт на локальном сервере Xampp, он работает нормально. Однако при развертывании на сервере GCP выдается ошибка.

PHP Неустранимая ошибка: вызов неопределенной функции curl_init () в

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

Может кто-нибудь помочь мне решить эту проблему? ошибка.

1 Ответ

0 голосов
/ 21 апреля 2020

Добавить следующий 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";
}
...