Shop OBEX P1 Docs P2 Docs Learn Events
need help with php code for sms based gate opener — Parallax Forums

need help with php code for sms based gate opener

michaelmonsomichaelmonso Posts: 19
edited 2011-10-05 12:19 in Accessories
im trying to make this project i saw online work for me. this guy used a spinneret and a cloud server called twilio to be able to text message his spinneret to have it trigger a servo to lock or unlock his door. i would like to do the same thing but to open or close my gate. i have a handle around his .spin project and i finally got twilio to work. between the two though he has a python django enviroment that parses the text message and sends the information to the spinneret. i cant get the django part to work but he said it could be written in php to work. im not familiar with php enough to do that code though. im looking for help from anyone that can take the following code written in python django and make a php document that does the same thing.
import urllib2

def door(request):
    '''
      This is plugged into a django env, but can really be used with anything.
      Twilio hits this URL, which pings the door and returns the response
    '''

    doorurl = 'http://162.228.0.150:5001/'

    whitelist = [ "+18183798995",
                  "+14179592671",
                  ]

    fromnum = request.POST.get("From")
    body = request.POST.get("Body")

    if fromnum and body and fromnum in whitelist:
        if body == "open":
            doorurl = doorurl + '?1=o'
        elif body == "close":
            doorurl = doorurl + '?1=c'
        elif body == "status":
            pass

        try:
            doorres = urllib2.urlopen(doorurl, timeout = 10).read().strip()
        except:
            doorres = "door unresponsive"

        return HttpResponse('<Response><Sms>' + doorres + '</Sms></Response>', content_type="text/xml")

    raise Http404

so he would text twilio "open" >twilio would foward this to his django script> django would parse and send his spinneret "o"> the spinneret would trigger the servo... but he is using the above script written in python i would like written in php because my host for the python file doesnt support the django enviroment he wrote the code in.

please help anyone.thanks.

Comments

  • Mike GMike G Posts: 2,702
    edited 2011-10-05 11:42
    michaelmonso, it depends on your installation of PHP. I'm using cURL. See the online php manual for command syntax -> http://php.net/
    <?php 
    
    	// Members
    	$whilelist = array("+18183798995", "+14179592671");
    	$authenticated = false;
    	
    	//HTTP POST check
    	if($_POST)
    	{
    		// Get the POST data
    		$from = $_POST["from"];
    		$body = $_POST["body"];
    		
    		//Enumerate the phone list and auth
    		if(in_array($from, $whilelist)) {
    			$authenticated = true;
    		} 
    
    		// Setup querystring variable
    		switch($body) {
    			case "open":
    				$qdata = "?l=o";
    				break;
    			case "close":
    				$qdata = "?l=c";
    				break;
    			case "status":
    				$qdata = "?l=";
    				break;
    			default:
    				$qdata = "?l=";
    				break;	
    		}
    		
    		// Execute HTTP GET if authenticated	
    		if($authenticated)
    		{
    			$url = "http://162.228.0.150:5001:5000" . $qdata;
    			$ch = curl_init();
    			curl_setopt($ch, CURLOPT_URL, $url);
    			curl_setopt($ch, CURLOPT_HEADER, 0);
    			curl_exec($ch);
    			curl_close($ch);
    		}
    		
    	}//$_POST
    	
    ?>
    

    IMO, It would be easier to send the HTTP request directly to your Spinneret from twilio. Why relay through a secondary server?
  • michaelmonsomichaelmonso Posts: 19
    edited 2011-10-05 12:17
    im using wamp server which is running apache 2.2.21 and php 5.3.8
  • michaelmonsomichaelmonso Posts: 19
    edited 2011-10-05 12:19
    i dont know how to manually send it to wpinneret from twilio.
Sign In or Register to comment.