skip to Main Content

I tried to send a message without a Twilio phone number because we has no intent to get any messages from users. I followed this tutorial written in Ruby https://www.twilio.com/blog/send-sms-without-phone-number-ruby. I search for any similar tutorial written in PHP but to no avail.

Code:

public function sendSms(string $message, string $phone)
    {
        $sid = env("TWILIO_ACCOUNT_SID");
        $token = env("TWILIO_AUTH_TOKEN");
        $messagingSid = env("MESSAGING_SERVICE_SID");
        $twilio = new Client($sid, $token);

        $message = $twilio->messages->create(
            $phone,
            [
                "body" => $message,
                "messaging_service_sid" => $messagingSid,
            ]
        );

        return $message->sid;
    }

When this code run, an error occurred saying [HTTP 400] Unable to create record: A ‘From’ phone number is required
I just send messages without expecting them to reply back. How can I do it? Or Do I need to buy one for it to work?

2

Answers


  1. I’m the main author of PHP content for the Twilio blog.

    From what I understand, a messaging service still requires a Twilio phone number to send SMS from, as covered in the Messaging Services documentation just before the table of contents.

    I’ll write up an equivalent version of the tutorial that you referenced but centred around PHP, during the week, making sure to highlight this point.

    Login or Signup to reply.
  2. It seems to me that you supply an unknown parameter to the function call. According to this docs page, it should be messagingServiceSid instead of messaging_service_sid.

    So you could should be:

    public function sendSms(string $message, string $phone)
        {
            $sid = env("TWILIO_ACCOUNT_SID");
            $token = env("TWILIO_AUTH_TOKEN");
            $messagingSid = env("MESSAGING_SERVICE_SID");
            $twilio = new Client($sid, $token);
    
            $message = $twilio->messages->create(
                $phone,
                [
                    "body" => $message,
                    "messagingServiceSid" => $messagingSid,
                ]
            )
    
    
            return $message->sid;
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search