Krishnaraj Varma
Published

Spark Core and Twilio

Calling a Twilio number to control Spark Core

Full instructions provided4,100
Spark Core and Twilio

Things used in this project

Hardware components

Spark Core
Particle Spark Core
×1

Story

Read more

Code

file_7214.xml

XML
TwiML file
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Gather action="/twilioaction.php" method="GET">
        <Say>
          Thank you for trying Spark Core and Twilio Demo, please enter your PIN number, 
          followed by the pound sign.
        </Say>
    </Gather>
    <Say>We didn't receive any input. Goodbye!</Say>
</Response>

file_7215.php

PHP
PHP Script
<?php
if($_REQUEST['Digits'] == '55231' || $_REQUEST['Digits'] == '55230'){
	$url = 'https://api.spark.io/v1/devices/<<codeid>>/digitalWrite';

	if($_REQUEST['Digits'] == '55231'){
		$data = array(
			'access_token' => '<<accesstoken>>', 
			'params' => 'D7,HIGH'
		);
	}
	else{
		$data = array(
			'access_token' => '<<accesstoken>>', 
			'params' => 'D7,LOW'
		);
	}

	// use key 'http' even if you send the request to https://...
	$options = array(
		'http' => array(
			'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
			'method'  => 'POST',
			'content' => http_build_query($data),
		),
	);

	$context  = stream_context_create($options);
	$result = file_get_contents($url, false, $context);

	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	if($_REQUEST['Digits'] == '55231')
		echo "<Response><Say>Your light will be turned on, Goodbye.</Say></Response>";
	else
		echo "<Response><Say>Your light will be turned off, Goodbye.</Say></Response>";
}else{
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	echo "<Response><Say>You entered " . $_REQUEST['Digits'] . ", 
	      you are not authorized to use this demo, Goodbye.</Say></Response>";
}
?>

Credits

Krishnaraj Varma

Krishnaraj Varma

31 projects • 356 followers
Software Engineer from Cochin, Kerala, India

Comments

Add projectSign up / Login