0

I'm setting up a simple script to send SMS through Twillio, and record the messages in the database. All the code runs fine until I add an IF statement.

A form is used to enter the phone number and message, which is sent (POST) to the process.php file, which send the message through the Twillio API and logs it in the database.

I'm pretty good with PHP, it's a very basic IF statement I have used thousands of times before, but this 500 error has me stumped.

if(isset($_POST['phone_number'])) {

    $sms_to = $_POST['phone_number'];
    $sms_body = $_POST['sms_message'];

    require_once '../vendor/autoload.php';

... Lots more code

} else {
    echo "Sorry, message not sent. No phone number was entered.";
}

I have triple checked all the code to make sure there was no errors, which it does run fine, until I add the if statement.

Eoin H
  • 301
  • 5
  • 12
  • 4
    What is the actual error? A `500 error` is a general server error, the logs should have details about what is causing error. – user3783243 Jul 05 '22 at 14:35
  • There's nothing wrong with this code. If adding this if/else causes an error, then you've probably wrapped the open/close parens at different levels, and you're now unbalanced. – Alex Howansky Jul 05 '22 at 14:39
  • 1
    Try to print error. And paste the error so we can give best possible solution. – Monish Khatri Jul 05 '22 at 14:40
  • @AlexHowansky , That was my first thought, but they are in the correct places. I checked them manually and both Sublime and Dreamweaver is showing them as correct too. – Eoin H Jul 05 '22 at 14:42
  • @MonishKhatri no errors showing other than HTTP 500 ERROR - currently unable to handle this request. – Eoin H Jul 05 '22 at 14:46
  • 1
    You need to check the server log file, not the browser screen. – Alex Howansky Jul 05 '22 at 14:47
  • @AlexHowansky The server log is not showing anything at all, either is the error_log file in the root directory. This is why I am so stumped on this one. – Eoin H Jul 05 '22 at 14:56
  • Found the culprit.. Twilio REST API won't work within an IF statement.... use Twilio\Rest\Client; – Eoin H Jul 05 '22 at 15:00
  • 1
    `ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL)` put this in your script and you'll get exact error. Than post the error here or solve it. – Monish Khatri Jul 05 '22 at 15:03
  • @MonishKhatri No errors showing for anything, but I did find it with a bit more manual debugging.. it was "use Twilio\Rest\Client;", part of the Twillio REST API. – Eoin H Jul 05 '22 at 15:06
  • Who closed this question? The duplicate post does not answer the question. As I said multiple time, no errors are showing. – Eoin H Jul 05 '22 at 15:09
  • _"Twilio REST API won't work within an IF statement"_ This makes no sense. Whether or not you're inside an if statement has nothing to do with the error due to lack of a `use` statement. Your problem isn't the if statement, it's the erroneous code you added inside it. – Alex Howansky Jul 05 '22 at 15:19
  • @AlexHowansky The code inside it works fine, until the if statement is added. – Eoin H Jul 05 '22 at 15:31
  • @AlexHowansky.. This works... `if(isset($sms_body)) { require_once '../vendor/autoload.php'; } use Twilio\Rest\Client;` ... This doesn't... `if(isset($sms_body)) { require_once '../vendor/autoload.php'; use Twilio\Rest\Client; }` – Eoin H Jul 05 '22 at 15:33
  • Your autoloader config shouldn't be inside a conditional, just move the require to the top of the file and always run it. – Alex Howansky Jul 05 '22 at 15:59

0 Answers0