This is an opensource project by me and final release is version 1.0.1 till now.
First we have to create index.php for the options that will give the senders number and text box what to send to the sender and then at last the resgistration id from your mobile phone(It will be needed to use your mobile phone as messaging server.It will come from GCM app).
<!-- author:Syed Ahmed Zaki blog:www.zakilive.com facebook.com/zakilivebuzz app_idea:Mohammad Amir Hamza --> <html> <head> <title>SMS Gateway</title> </head> <body> <form action="gcmtest.php" method="GET"> <label>PHP-GCM<br> <br> Mobile Nummer</label> <br> <input type="text" name="numb"> <br> <label>Message</label> <br> <textarea name="mess"></textarea> <br> <br> <textarea name="id">registration id from your device</textarea> <br> <input type="submit" name="btn" value="SUBMIT"> </form> </body> </html>
Now we have to create gcmtest.php it is the core part of our project.Here all the settings available that you can customize easily according to your needs.
<?php /* author:Syed Ahmed Zaki blog:www.zakilive.com facebook.com/zakilivebuzz app_idea:Mohammad Amir Hamza */ // API access key from Google API's Console define( 'API_ACCESS_KEY', 'Your API server key is here' ); $registrationIds = array( $_GET['id'] ); //it is getting registration id from the index page. // prep the bundle.You can define the desired values from gcm library.I needed message and number only so it has defined here. $msg = array ( 'message' => $_GET['mess'], 'number' => $_GET['numb'], ); //It is json passing $fields = array ( 'registration_ids' => $registrationIds, 'data' => $msg, ); $headers = array ( 'Authorization: key=' . API_ACCESS_KEY, 'Content-Type: application/json' ); $ch = curl_init(); curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' ); curl_setopt( $ch,CURLOPT_POST, true ); curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers ); curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false ); curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) ); $result = curl_exec($ch ); curl_close( $ch ); echo $result;
Now when we run the script from our php backend server then it will show you the page where you will input sender number and message and click submit it will pass through your phone to google cloud messaging server and receiver will recieve a sms from your number.
You can download this whole project from my mirror server: php-gcm.zip
I took help from the opensource project to learn how they did the gcm passing:
https://gist.github.com/prime31/5675017#file-gistfile1-php-L16 (In PHP)
https://github.com/anjlab/android-sms-gateway (Android SMS gateway call in Ruby)
If you liked the tutorial or face any problem don’t forget to comment in the comment section.Thanx for reading 🙂