I am trying to develop a PHP based application to send sms across India to contacts picked from the database. After searching for hours, I found the following working (I tested it) script which makes use of fullonsms.com
as sms gateway but it sends a single message at a time and then loads the homepage.
But I don’t know a lot about curl.
So
- I need help to understand what is actually being done.
- Secondly,how can I modify it to optimally send sms to multiple people(like will it be optimal to put it in a loop) without wasting data or unnecessary page loads. 3.After sending each SMS ,it loads homepage.So won’t it try to load the homepage before sending SMS.If yes,can I remove the starred(** **) line to prevent this.
Here is my code:
<?php $cookie_file_path = "/cookie.txt"; $username="username"; $password="password"; $tomobno="1234567890"; $message=urlencode("Hi buddy"); $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,"http://sms.fullonsms.com/login.php"); curl_setopt($ch, CURLOPT_USERAGENT, $agent); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_fie_path); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_POSTFIELDS, "MobileNoLogin=$username&LoginPassword=$password&x=16&y=14"); $html=curl_exec($ch); **curl_setopt($ch, CURLOPT_URL,"http://sms.fullonsms.com/home.php");** curl_setopt($ch, CURLOPT_USERAGENT, $agent); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_fie_path); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_POSTFIELDS, "ActionScript=%2Fhome.php&CancelScript=%2Fhome.php&HtmlTemplate=%2Fvar%2Fwww%2Fhtml%2Ffullonsms%2FStaticSpamWarning.html&MessageLength=140&MobileNos=$tomobno&Message=$message&Gender=0&FriendName=Your+Friend+Name&ETemplatesId=&TabValue=contacts"); $html = curl_exec($ch); echo $html; ?>
There’s is a similar question asked but answer accepted is off the topic and it uses a different gateway SMS sending through free gateway
Advertisement
Answer
1) I need help to understand what is actually being done.
The code is sending two HTTP requests to the service. The first request signs in with your username and password, and it stores the session cookies for the next request. The second request is the one that actually triggers the send, it takes the mobile number and other details as POST data. The second request is able to use the session created by the first request because of the cookiejar.
2) Secondly,how can I modify it to optimally send sms to multiple people
If the service allows you to input multiple mobile numbers (for example comma separated) then that would be most optimal because it would only require the two requests to send to all mobile numbers.
If not, you will have to loop the second request so that there is one request per mobile number. Remember to change the hardcoded POST data so that is uses the next mobile number and name, instead of sending to the same number each time.
I would also suggest pausing for a period of time after each iteration of the loop to prevent sending the requests too fast. You can use sleep(1)
for example to wait for 1 second.