r/PHPhelp • u/danlindley • Jun 11 '25
cURL Not posting (it did, and no changes)
I've done a lot of reading on this and cant find an answer. I have a site, and use an SMS api. Was working fine for some time. I've come to it today to tinker and for some reason it isn't working.
I have a form one the page that sends data to "send_sms.php" for processing then to execute the cURL. The data is going to the server via POST perfectly fine as far as I can tell. It just seems to be the cURL. I did read that either php version (using 8.3) may or may not support it (i rolled back and this didn't change it) or that the server is preventing it going out.
I managed to come up with a workaround however it leaves my api key exposed in the code. It picks up what in the variables or the text box and sends it to the api url. This works without issue.
<button class="delete btn btn-outline-secondary" onclick="window.location.href='https://www.firetext.co.uk/api/sendsms?apiKey=MYAPIKEY&message='+document.getElementById('sms_message').value + '&from=RescueCtr&to=' + document.getElementById('sms_send_to').value">Submit</button>
My code for the send_sms
<?php
header('Location: ' . $_SERVER["HTTP_REFERER"] );
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$finder_name = ($_POST['finder_name']);
$sms_message = ($_POST['sms_message']);
$rescue_name = ($_POST['rescue_name']);
$sms_array = ("Dear $finder_name. \n $sms_message \n $rescue_name");
// this is the section that actually send the SMS
$smsdata = array(
'apiKey' => 'MYAPIKEY',
'message' => $sms_array,
'from' => 'MY_APP',
'to' => urlencode($_POST['sms_send_to']));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.firetext.co.uk/api/sendsms");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $smsdata);
$result = curl_exec($ch); curl_close ($ch);
// Do something with $result
$SMSalert = '<div class="alert alert-success" role="alert">
Message Sent
</div>';
} else {
echo "error";
exit();
}?>
If anyone knows about this sort of thing and can guide me in the right direction, i'd be made up.
Thank you
4
u/Tiancris Jun 11 '25
Your php code expects to receive data in a POST request, but the button is sending a GET one
1
u/flyingron Jun 11 '25 edited Jun 11 '25
You do know that Firetext has a PHP SDK that will let you send texts without resorting to curl?
Anyhow, the problem seems to be that you urlencode the phone number (which probably won't be required unless you've stuffed spaces in it), but you don't urlencode the message which absolutely won't work because it does have spaces in it.
Try:
1
u/isoAntti Jun 11 '25
Supporting or not curl means that often you need to install the curl support. For example to Ubuntu it's installing php-curl package. I think similar for most distros.
You really should turn on all error reporting to see what's going on.
https://stackoverflow.com/a/48715857/468921
if curl library doesn't exist or can't contact the server you get appropriate error messages on display. That's where to start from.
1
u/boborider Jun 12 '25
Use POSTMAN to emulate your requests.
0
u/[deleted] Jun 12 '25
[deleted] — view deleted comment