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

rublopweb's avatar

Sending WhatsApp with Messabird

Hi, in our project we are trying to send whatsapps as reminders using the api of messagebird. This is the code we are using

 public static function SendSignatureReminderWhatsapp(array $info)
    {
        $phone  = PhoneHelper::formatPhoneNumber($info['phone']);

        $ch = curl_init();
    
        $url = env('SEND_WHATSAPP_URL');
        
        $data = array(
            "type" => "hsm",
            "to" => $phone,
            "from" => CHANNEL_ID,
            "content" => array(
                "hsm" => array(
                    "namespace" => "TEMPLATE_ID",
                    "templateName" => "reminder",
                    "language" => array(
                        "policy" => "deterministic",
                        "code" => "es_ES"
                    ),
                    "components" => array(
                        array(
                            "type" => "body",
                            "parameters" => array(
                                array(
                                    "type" => "text",
                                    "text" => $info['name']
                                )
                            )
                        ),
                        array(
                            "type" => "button",
                            "sub_type" => "url",
                            "parameters" => array(
                                array(
                                    "type" => "text",
                                    "text" => $info['link']
                                )
                            )
                        )
                    )
                )
            )
        );
        
        $data_string = json_encode($data);

        $headers = array(
            "Authorization: AccessKey ". API_KEY,
            "Content-Type: application/json"
        );

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        
        curl_close($ch);
        
        Log::info($response);
        Log::info($httpCode);

        
    }

In our local environment it works but in production it does not work. In the response we are not getting anything and the status we are getting is 0.

Can someone help me, please?

Thanks,

0 likes
1 reply
LaryAI's avatar
Level 58

It seems like you're encountering an issue where your code works in a local environment but not in production. This could be due to a variety of reasons, such as differences in environment configuration, network issues, or permissions. Here are some steps you can take to troubleshoot and resolve the issue:

  1. Check Environment Variables: Ensure that the environment variables like SEND_WHATSAPP_URL, CHANNEL_ID, and API_KEY are correctly set in your production environment.

  2. SSL Verification: If your production server is enforcing SSL verification and you're trying to hit an HTTPS endpoint, you might need to add an option to your cURL request to handle SSL.

  3. Error Handling: Improve error handling in your cURL request to get more insight into what might be going wrong.

  4. Server Logs: Check your server logs for any errors that might be occurring on the production server.

  5. Firewall or Network Issues: Ensure that your production server is not behind a firewall or network configuration that is preventing it from making outbound HTTP requests.

Here's an updated version of your function with improved error handling:

public static function SendSignatureReminderWhatsapp(array $info)
{
    $phone  = PhoneHelper::formatPhoneNumber($info['phone']);

    $ch = curl_init();

    $url = env('SEND_WHATSAPP_URL');
    $data = [
        // ... your data array
    ];
    
    $data_string = json_encode($data);

    $headers = [
        "Authorization: AccessKey " . env('API_KEY'),
        "Content-Type: application/json"
    ];

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Add this line if you have SSL verification issues
    // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if (curl_errno($ch)) {
        // If cURL error exists, log it
        Log::error('cURL Error: ' . curl_error($ch));
    }

    curl_close($ch);

    Log::info('Response: ' . $response);
    Log::info('HTTP Code: ' . $httpCode);
}

Make sure to replace env('API_KEY') with the actual API key for your production environment. If you're disabling SSL verification (CURLOPT_SSL_VERIFYPEER), be aware that this is not recommended for production environments as it can pose security risks.

If after these changes you're still facing issues, you might want to reach out to MessageBird support with the details of the error you're encountering, as it could be related to their service or your account configuration on their end.

Please or to participate in this conversation.