Tuesday 26 March 2013

PHP Send SMS with SMS Gateway


PHP Send SMS with SMS Gateway

How to send SMS via SMS Gateway in PHP.I found the sms gateway to do this.But it is gives only 25 free sms per account.But you can create email account and register the following web site and send sms.Only you have to use simple function and call to it with passing parameters.
Here is a web site to register free.
After get API Key and Account name from your account dashboard.Go to,
1-My Services.
2-Click view button of the right side.
3-Click Setting tab.
4-You can view API Key and Account name under the API Setting.
Following diagram will show how this process is working.
SMS Process
This is the function.Copy and run it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
function post_request($url, $data, $optional_headers = null)
{
   // data needs to be in the form of foo=bar&amp;bar=foo
   $data = http_build_query($data);
   $params = array(
     'http'    => array(
     'method'  => 'POST',
     'content' => $data,
    // you need to specify the content length header
     'header'  => "Content-type: application/x-www-form-urlencoded\r\n"
    . 'Content-Length: ' . strlen($data) . "\r\n"
   ));
  if ($optional_headers !== null) {
      // append optional_headers if it is not null
      $params['http']['header'] .= $optional_headers;
  }
   $ctx = stream_context_create($params);
   $fp = fopen($url, 'r', 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");
   }
   fclose($fp);
   return $response;
}   // End of the function
   $post_data => array(
     'accountName' => 'MyAccount',
     'key'         => 'xxxxffxx-xxxx-xxxx-xxxx-xxxxd2d3xxxx',
     'phoneNumber' => '0715258378',
     'message'     => 'Hello World', //please perform URLEncode on this field
     'countryCode' => 'US'
  );
 try
 {
   $result = post_request('http://api.gateway160.com/client/sendmessage/', $post_data);
   if($result == "1")
   {
     echo "Sent Successfully";
   }
   else
   {
    //error (check the response code from the chart above)
    echo "Error";
   }
 }
 catch (Exception $e)
 {
    echo "Exception: " . $e->getMessage();
 }
?>

1 comment: