Difference between revisions of "PHP Sending SMS"
From SMS Wiki
(New page: Please see the receiving example as it contains code for sending as well.) |
|||
| (14 intermediate revisions by one user not shown) | |||
| Line 1: | Line 1: | ||
| − | + | <source lang="php"> | |
| + | |||
| + | <?php | ||
| + | |||
| + | // NOTE - THE CODE BELOW IS PROVIDED FOR ILLUSTRATION ONLY | ||
| + | |||
| + | //SOME CHANGES MAY BE NEEDED TO GET IT TO WORK IN YOUR PROGRAM | ||
| + | |||
| + | // plug in the values returned from | ||
| + | |||
| + | // http://api.upsidewireless.com/soap/Authentication.asmx/GetParameters | ||
| + | |||
| + | // please note that in order to use this and other examples and EnterpriseSMS | ||
| + | |||
| + | //API your account must have permissions set to do so. | ||
| + | |||
| + | $token = '----------------------------------------'; | ||
| + | |||
| + | $signature = '--------------------------------------'; | ||
| + | |||
| + | $api_call_data = array( | ||
| + | 'token' => $token, | ||
| + | 'signature' => $signature, | ||
| + | 'recipient' => '+16043434343', | ||
| + | 'message' => 'Hello World', | ||
| + | 'encoding' => 'Seven' // Use 'Sixteen' if you are sending in Unicode | ||
| + | ); | ||
| + | |||
| + | $post_data = build_post_data($api_call_data ); | ||
| + | |||
| + | // Please refer to | ||
| + | |||
| + | // http://api.upsidewireless.com/ | ||
| + | |||
| + | // for other sending methods | ||
| + | |||
| + | $url = 'http://api.upsidewireless.com/soap/SMS.asmx/Send_Plain'; | ||
| + | |||
| + | $xml = do_post_request($url, $post_data); | ||
| + | |||
| + | // be sure you look at the XML from above to see if there are any errors | ||
| + | |||
| + | |||
| + | // Everything below is just function definitions | ||
| + | |||
| + | function do_post_request($url, $data, $optional_headers = null) | ||
| + | { | ||
| + | $params = array('http' => array('method' => 'POST', 'content' => $data)); | ||
| + | |||
| + | if ($optional_headers !== null) { | ||
| + | $params['http']['header'] = $optional_headers; | ||
| + | } | ||
| + | |||
| + | $ctx = stream_context_create($params); | ||
| + | |||
| + | $fp = @fopen($url, 'rb', false, $ctx); | ||
| + | if (!$fp) { | ||
| + | throw new Exception("Problem with $url, $php_errormsg"); | ||
| + | } | ||
| + | |||
| + | $response = @stream_get_contents($fp); | ||
| + | |||
| + | if ($response === false) { | ||
| + | throw new Exception("Problem reading data from $url, $php_errormsg"); | ||
| + | } | ||
| + | return $response; | ||
| + | } | ||
| + | |||
| + | |||
| + | function build_post_data($data, $prefix=null, $sep="", $key="") { | ||
| + | |||
| + | $ret = array(); | ||
| + | |||
| + | foreach((array)$data as $k => $v) { | ||
| + | |||
| + | $k = urlencode($k); | ||
| + | |||
| + | if(is_int($k) && $prefix != null) { | ||
| + | $k = $prefix.$k; | ||
| + | } | ||
| + | |||
| + | if(!empty($key)) { | ||
| + | $k = $key."[".$k."]"; | ||
| + | } | ||
| + | |||
| + | if(is_array($v) || is_object($v)) { | ||
| + | array_push($ret, build_post_data($v,"",$sep,$k)); | ||
| + | } else { | ||
| + | array_push($ret,$k."=".urlencode($v)); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | if(empty($sep)) { | ||
| + | $sep = ini_get("arg_separator.output"); | ||
| + | } | ||
| + | |||
| + | return implode($sep, $ret); | ||
| + | } | ||
| + | |||
| + | ?> | ||
| + | |||
| + | </source> | ||
| + | |||
| + | ---- | ||
| + | [[APIs | back]] | ||
Latest revision as of 11:47, 29 October 2013
<?php // NOTE - THE CODE BELOW IS PROVIDED FOR ILLUSTRATION ONLY //SOME CHANGES MAY BE NEEDED TO GET IT TO WORK IN YOUR PROGRAM // plug in the values returned from // http://api.upsidewireless.com/soap/Authentication.asmx/GetParameters // please note that in order to use this and other examples and EnterpriseSMS //API your account must have permissions set to do so. $token = '----------------------------------------'; $signature = '--------------------------------------'; $api_call_data = array( 'token' => $token, 'signature' => $signature, 'recipient' => '+16043434343', 'message' => 'Hello World', 'encoding' => 'Seven' // Use 'Sixteen' if you are sending in Unicode ); $post_data = build_post_data($api_call_data ); // Please refer to // http://api.upsidewireless.com/ // for other sending methods $url = 'http://api.upsidewireless.com/soap/SMS.asmx/Send_Plain'; $xml = do_post_request($url, $post_data); // be sure you look at the XML from above to see if there are any errors // Everything below is just function definitions function do_post_request($url, $data, $optional_headers = null) { $params = array('http' => array('method' => 'POST', 'content' => $data)); if ($optional_headers !== null) { $params['http']['header'] = $optional_headers; } $ctx = stream_context_create($params); $fp = @fopen($url, 'rb', false, $ctx); if (!$fp) { throw new Exception("Problem with $url, $php_errormsg"); } $response = @stream_get_contents($fp); if ($response === false) { throw new Exception("Problem reading data from $url, $php_errormsg"); } return $response; } function build_post_data($data, $prefix=null, $sep="", $key="") { $ret = array(); foreach((array)$data as $k => $v) { $k = urlencode($k); if(is_int($k) && $prefix != null) { $k = $prefix.$k; } if(!empty($key)) { $k = $key."[".$k."]"; } if(is_array($v) || is_object($v)) { array_push($ret, build_post_data($v,"",$sep,$k)); } else { array_push($ret,$k."=".urlencode($v)); } } if(empty($sep)) { $sep = ini_get("arg_separator.output"); } return implode($sep, $ret); } ?>