Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rifky49's avatar

Cannot Send SMS

"I have been trying to send an SMS using a PHP function but it's not working. I have tried everything that I know of, but I'm not getting any results. I am using an API SMS method but I'm getting an error message that says 'Undefined variable $ch'. I am not sure if something is missing or not in my code. Below is the code that I am using."

<?php
include_once "./config.php";

$fname = $_POST["fname"];
$lname = $_POST["lname"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$date = $_POST["date"];
$gender = $_POST["gender"];
$address = $_POST["address"];
$hobby = $_POST["hobby"];

$hobbyInString = serialize($hobby);

// Use prepared statement to prevent SQL injection
$sql = "INSERT INTO details (first_name, last_name, email, mobile_num, dob, gender, address, hobbies) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, "ssssssss", $fname, $lname, $email, $phone, $date, $gender, $address, $hobbyInString);
$checkResult = mysqli_stmt_execute($stmt);

if ($checkResult) {
    // Successfully entered data into the database

    // Continue with SMS sending
    $access_token = login();

    $post_data_sms = array(
        "campaignName" => "XXXXXXX",
        "mask" => "XXXXXXX",
        "numbers" => $phone, // Assuming $phone contains the recipient's number
        "deliveryReportRequest" => true,
        "content" => "Test message"
    );

    $ch_sms = curl_init('https://bsms.hutch.lk/api/sendsms');
    curl_setopt_array($ch_sms, array(
        CURLOPT_POST => TRUE,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_HTTPHEADER => array(
            'Content-Type: application/json',
            'Accept: */*',
            'X-API-VERSION: v1',
            'Authorization: Bearer ' . $access_token
        ),
        CURLOPT_POSTFIELDS => json_encode($post_data_sms)
    ));
    $response_sms = curl_exec($ch_sms);

    // Handle the SMS sending response
    if ($response_sms === FALSE) {
        die(curl_error($ch_sms));
    }

    // Close the cURL handle
    curl_close($ch_sms);

    // Handle the response as needed
    $sms_response_data = json_decode($response_sms, TRUE);
    if ($sms_response_data['status'] == 'success') {
        echo "SMS sent successfully!";
    } else {
        echo "SMS sending failed!";
    }

    // Redirect to another page or display a success message
    header("Location: ./biodata.php");
} else {
    // Database entry unsuccessful
    echo "Unsuccessful!!";
}

function login()
{
    global $config_file;
    $post_data = array("username" => "[email protected]", "password" => "XXXXXXXX");
    $ch = curl_init('https://bsms.hutch.lk/api/login');
    curl_setopt_array($ch, array(
        CURLOPT_POST => TRUE,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_HTTPHEADER => array(
            'Content-Type: application/json',
            'Accept: */*',
            'X-API-VERSION: v1'
        ),
        CURLOPT_POSTFIELDS => json_encode($post_data)
    ));
    $response = curl_exec($ch);

    // Handle the login response
    if ($response === FALSE) {
        die(curl_error($ch));
    }

    // Close the cURL handle
    curl_close($ch);

    // Decode the JSON response
    $responseData = json_decode($response, TRUE);

    // Check and update the $config_file variable
    if (empty($config_file)) {
        // Update $config_file with a valid file path
        $config_file = 'path/to/config/file.json';
    }

    // Save the login response to the specified file
    file_put_contents($config_file, json_encode($responseData, JSON_PRETTY_PRINT));

    return $responseData['access_token']; // Return the access token
}
?>
0 likes
5 replies
Snapey's avatar

Thanks for reminding me, I’m so pleased I have not had to write code like this in the last 10 years.

Thank you Laravel

rifky49's avatar

"Hey @Snapey, do you know what the error message is that wasn't sent to Client?"

martinbean's avatar

I'm getting an error message that says 'Undefined variable $ch'. I am not sure if something is missing or not

@rifky49 Yes. A variable called $ch given that error message. So read the error message, and go to the line that it’s complaining about.

rifky49's avatar

"I am not receiving an error message. The data is being saved to the table, but it is not being sent."

Snapey's avatar

@rifky49

I am not receiving an error message

Well, "Undefined variable $ch" sounds just like an error message

Please or to participate in this conversation.