Twilio not working with curl php
First of all, since API's URL is secured, you have to disable SSL peer verification by setting CURLOPT_SSL_VERIFYPEER
option to false
.
Furthermore, according to API docs, URL of request is account-dependent, i.e. it should be built based on your account sandbox ID:
https://api.twilio.com/2010-04-01/Accounts/{AccountSid}/Messages
So the code should look like this:
$id = "SANDBOX_ACC_ID";$token = "SANDBOX_TOKEN";$url = "https://api.twilio.com/2010-04-01/Accounts/$id/SMS/Messages";$from = "+MAGICNUMBER";$to = "+XXXXXXXXXX"; // twilio trial verified number$body = "using twilio rest api from Fedrick";$data = array ( 'From' => $from, 'To' => $to, 'Body' => $body,);$post = http_build_query($data);$x = curl_init($url );curl_setopt($x, CURLOPT_POST, true);curl_setopt($x, CURLOPT_RETURNTRANSFER, true);curl_setopt($x, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($x, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);curl_setopt($x, CURLOPT_USERPWD, "$id:$token");curl_setopt($x, CURLOPT_POSTFIELDS, $post);$y = curl_exec($x);curl_close($x);var_dump($post);var_dump($y);
Here is a PHP - Curl implementation in a callable function
function send_sms($number,$body){ $ID = '1234567890abcdef1234567890abcdef12'; $token = '1234567890abcdef1234567890abcdef'; $service = 'AB1234567890abcdef1234567890abcdef'; $url = 'https://api.twilio.com/2010-04-01/Accounts/' . $ID . '/Messages.json'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true); curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD,$ID . ':' . $token); curl_setopt($ch, CURLOPT_POST,true); curl_setopt($ch, CURLOPT_POSTFIELDS, 'To=' . rawurlencode('+' . $number) . '&MessagingServiceSid=' . $service . //'&From=' . rawurlencode('+18885550000') . '&Body=' . rawurlencode($body)); $resp = curl_exec($ch); curl_close($ch); return json_decode($resp,true);}
$ID and $token can be found under SMS / Dashboard / 'Show API Credentials'https://www.twilio.com/console/sms/dashboard
(Optional) $service can be found under SMS / Messaging Services / 'SID' https://www.twilio.com/console/sms/services
Comment out 'MessagingServiceSid=' and uncomment 'From=' to use direct sending from a single phone number
Finally, key information can be found buried in the kb here https://www.twilio.com/docs/sms/send-messages#send-a-message-with-an-image-url
Curl Twilio PHP7+
$account_sid = 'account_sid';$auth_token = 'auth_token';$url = "https://api.twilio.com/2010-04-01/Accounts/$account_sid/SMS/Messages";$to = "+xxxxxx";$from = "+xxxxxx"; // twilio trial verified number$body = "using twilio rest api from Fedrick";$data = array ( 'From' => $from, 'To' => $to, 'Body' => $body,);$post = http_build_query($data);$x = curl_init($url );curl_setopt($x, CURLOPT_POST, true);curl_setopt($x, CURLOPT_RETURNTRANSFER, true);curl_setopt($x, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($x, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);curl_setopt($x, CURLOPT_USERPWD, "$account_sid:$auth_token");curl_setopt($x, CURLOPT_POSTFIELDS, $post);$y = curl_exec($x);curl_close($x);//var_dump($post);//var_dump($y);