Posted by & filed under PHP, Twilio.

Using Twilio and PHP, you can create a temporary phone number for your own purposes. You can put this on Craigslist for some ads, and delete the number when you’re done.

The less tech-advanced can use Burner Phone, which also has more features than this particular script. I built my script for a most basic forwarding, and it does just that.

<?php
// Constants
$TWILIO_ACCOUNT_SID = ""; // Your Twilio Account SID
$TWILIO_AUTH_TOKEN = ""; // Your Twilio Auth Token
$FORWARD_NUMBER = ""; // The number to receive the calls/texts in format +1XXXYYYZZZZ

$to = $_REQUEST['To'];
$from = $_REQUEST['From'];
$body = $_REQUEST['Body'];

if($_REQUEST['CallSid']){ // Phone Call
	print <<<END
<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Dial callerId='$from'>$FORWARD_NUMBER</Dial>
</Response>
END;
}
else if($from == "$FORWARD_NUMBER"){ // SMS Reply
	// Send the message to the last person who sent a text
	$json_string = file_get_contents("https://$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN@api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/SMS/Messages.json?To=$to");
	$parsed_json = json_decode($json_string, TRUE);
	foreach($parsed_json['sms_messages'] as $message){
		if($message['from']=="$FORWARD_NUMBER")
			continue;
		$recipient = $message['from'];
		break;
	}
	print <<<END
<Response>
<Sms to='$recipient'>$body</Sms>
</Response>
END;
}
else{ // SMS
	print <<<END
<Response>
<Sms to='$FORWARD_NUMBER'>$from:$body</Sms>
</Response>
END;
}

?>

The way this is set up, you don’t even need your own database. This just uses the Twilio SMS log. There are a few drawbacks: phone calls are incoming-only; you can only send messages to the most recent person; and long messages may be curtailed. Luckily, it’s still useful enough for most purposes.

To use this script:

  1. Save this script to your server (updating the first three lines)
  2. Buy a number from Twilio
  3. Point the SMS and Voice URL to your script
  4. Use the number as your own

Simple as that!

2 Responses to “Create Temporary Phone Numbers Using Twilio”

  1. Elaine

    Unfortunately, Craigslist does not accept VOIP numbers for its verification calls, and this includes Twilio numbers. So Twilio phone numbers cannot be used to verify Craigslist accounts.

    • alex

      That’s fine, I didn’t use this for creating usernames or person verification. This is just something you can put in a public forum with the ability to drop in the future.