Send Receive Free SMS PHP

In this short tutorial we are going to learn about SMS functionality in our Send Receive Free SMS PHP project. SMS functionality is very common in every project these days but most cellular companies give a service very costly, due to high cost most student can not afford for learning projects. We have found a cost zero method for it. We know that everyone has smart device, we can use it for sending or receiving free sms in our php project. in this post we are going to learn all.

Before start to write code, let’s talk about prerequisites

  • Android mobile device (on this device you need android app installed)
  • GSM modem (SMS) app (we can find this app on playstore check further details here)
  • Laptop with php and mysql installed

There two sections first for sending and second or receiving so let’s start

Sending FREE SMS

For sending SMS from your project you need to create URL with required parameters.

LIKE

 http://192.168.1.101:8090/SendSMS?username=Sadiq&password=1234&phone=123456789&message=hello+word 

Before starting to write code, you must change IP address and port in above URL and then put in any browser address bar and hit network call. You will receive sms on given number.

For sending sms from your project make you sure for some points.

  • Open GSM Android (SMS) app and start it.
  • Check IP with port number in your app.
  • Update IP address in your program

EXAMPLE


<pre>
<html>
	<head>
		<title>Sending SMS</title>
	</head>

	<body>
			
<h3>Sending SMS</h3>

			
<form method='GET' action='sendSMS.php'>
				Phone <input type='phone' name = 'phone' autocomplete='off'> 
				Message <input type='message' name='message'>
				<input type='submit' value='Send'/>
			 </form>

	</body>
</html>

<?php try{ $message = isset($_GET['message']) ? $_GET['message'] : null; $phoneNumber = isset($_GET['phone']) ? $_GET['phone'] : null; if($message !=null && $phoneNumber !=null){ $url = "http://192.168.1.101:8090/SendSMS?username=Sadiq&password=1234&phone=".$phoneNumber."&message=".urlencode($message); $curl = curl_init($url); curl_setopt($curl,CURLOPT_RETURNTRANSFER, true); $curl_response = curl_exec($curl); if($curl_response === false){ $info = curl_getinfo($curl); curl_close($curl); die('Error occurred'.var_export($info)); } curl_close($curl); $response = json_decode($curl_response); if($response->status == 200){
			echo 'Message has been sent';
		}else{
			'Technical Problem';
		}

	}
}catch(Exception $ex){
	echo "Exception: ".$ex;
}
?>
 
</pre>

Receiving FREE SMS

For receiving free sms you must install GSM modem application, In that app you must add your url where you want to receive sms with given parameters. You must expose your own URL where you want to get sms text with phone number. In this tutorial we will save sms text and phone number in database (MySQL). GSM modem (SMS) hits on URL with following information GET method with message and phoneNumber parameters. For example http://…./receiveSMS.php?message=message&phoneNumber=123456789

Before testing GSM modem app for receiving SMS you must check these points.

 

  • Is your URL working fine with following request information, Method type GET with given parameters message and phoneNumber?
  • Have you entered your URL in app GET SMS at URL and check box is switched on?

EXAMPLE


<pre>
<?php $servername = 'localhost'; $username = 'root'; $password = '1234'; $dbname = 'test'; $message = isset($_GET['message']) ? $_GET['message'] : null; $phoneNumber = isset($_GET['phoneNumber']) ? $_GET['phoneNumber'] : null ; $conn = new mysqli($servername, $username, $password, $dbname); if($conn->connect_error){
			die('Connection failed'.$conn->connect_error);
		}

		if($message){
			$sql = "INSERT INTO inbox (phone_number, message) 
			Values ('".$phoneNumber."', '".$message."')";

			if($conn->query($sql) === TRUE){
				echo "New record created successfully";
			}else{
				echo "Error: ".$sql. "
 ".$conn->error;
			}
		}else{
			$sql = "SELECT * FROM inbox";
			$result = $conn->query($sql);

			if($result){
				echo "
<table border=1>
<th>ID</th>
<th>PHONE</th>
<th>MESSAGE</th>

";
				foreach($result as $row){
					echo "
<tr>
<td>".$row['id']."</td>
<td>".$row['phone_number']."</td>
<td>".$row['message']."</td>
</tr>

";
				}
				echo "</table>

";
			}else{
				echo "Error: ".$sql. "
".$conn->error;
			}

		}

		$conn->close();
?>
</pre>

11 Comments

  1. I got on error in sending sms:
    array (
    ‘url’ => ‘http://192.168.8.101:8090/SendSMS?username=edel&password=123&phone=093603611010&message=erw’,
    ‘content_type’ => NULL,
    ‘http_code’ => 0,
    ‘header_size’ => 0,
    ‘request_size’ => 0,
    ‘filetime’ => -1,
    ‘ssl_verify_result’ => 0,
    ‘redirect_count’ => 0,
    ‘total_time’ => 2.207983,
    ‘namelookup_time’ => 0.001995,
    ‘connect_time’ => 0.0,
    ‘pretransfer_time’ => 0.0,
    ‘size_upload’ => 0.0,
    ‘size_download’ => 0.0,
    ‘speed_download’ => 0.0,
    ‘speed_upload’ => 0.0,
    ‘download_content_length’ => -1.0,
    ‘upload_content_length’ => -1.0,
    ‘starttransfer_time’ => 0.0,
    ‘redirect_time’ => 0.0,
    ‘redirect_url’ => ”,
    ‘primary_ip’ => ”,
    ‘certinfo’ =>
    array (
    ),
    ‘primary_port’ => 0,
    ‘local_ip’ => ”,
    ‘local_port’ => 0,
    ‘http_version’ => 0,
    ‘protocol’ => 0,
    ‘ssl_verifyresult’ => 0,
    ‘scheme’ => ”,
    ‘appconnect_time_us’ => 0,
    ‘connect_time_us’ => 0,
    ‘namelookup_time_us’ => 1995,
    ‘pretransfer_time_us’ => 0,
    ‘redirect_time_us’ => 0,
    ‘starttransfer_time_us’ => 0,
    ‘total_time_us’ => 2207983,
    )Error occurred

    1. When I get incoming SMS, the application show Erro occurred, and I see any message in my database.

Leave a Reply

Your email address will not be published. Required fields are marked *