Skip to content Skip to sidebar Skip to footer

PHP Send SMS with SMS Gateway

Reading Time: < 1 minute

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.

     '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");
   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'
   $result = post_request('', $post_data);
   if($result == "1")
     echo "Sent Successfully";
    //error (check the response code from the chart above)
    echo "Error";
 catch (Exception $e)
    echo "Exception: " . $e->getMessage();

Other SMS Gateways.