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

manojvarma's avatar

can i write the plain php code in laravel ?

Hi, i want to know that can i use plain php code in laravel. If yes how to write it ? i want to use an api for sneding sms from laravel to phone numbers . i dont know where to place the code. this is my api code, can anyone suggest how can i achieve this?

<?php
    // Textlocal account details
    $username = '[email protected]';
    $hash = 'Your API hash';
    
    // Message details
    $numbers = array(447123456789, 447987654321);
    $sender = urlencode('Jims Autos');
    $message = rawurlencode('This is your message');
 
    $numbers = implode(',', $numbers);
 
    // Prepare data for POST request
    $data = array('username' => $username, 'hash' => $hash, 'numbers' => $numbers, "sender" => $sender, "message" => $message);
 
    // Send the POST request with cURL
    $ch = curl_init('http://api.txtlocal.com/send/');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    
    // Process your response here
    echo $response;
?>
0 likes
2 replies
rsands's avatar

@manojvarma Sure you can write raw code. Could be placed inside a Handler for example or anywhere you think is good

Best to move the $username, $hash to .env variables and load them via the config(), etc - means should it ever change its not a hardcoded value

willvincent's avatar

For what it's worth, all of laravel, except the frontend parts, is plain PHP. ;)

It would behoove you to learn the framework properly, rather than just trying to hack things in in a messy way.

That said. This would probably be a good candidate for implementing as an event, probably even a queued event if it's going to happen a lot. As @rsands mentioned, api credentials should be pulled out to the .env file, ideally.

If you're using laravel 5.3, best place to do this, probably, would be as a custom notifications channel. SMS is already available with notifications via nexmo, but if you want to use txtlocal.com, you'll probably need a custom channel.

Please or to participate in this conversation.