This article is about a Php code which is used to send SMS to your Indian mobile number through way2sms service provider. You will require an account with way2sms in order to use this code. Account registration at way2sms.com is free.

The code in the article is a php function which uses the cURL library. Before you run the code you need to ascertain that the cURL library is also installed along with Php.

The function takes four arguments (parameters). Those are $userID, $userPWD, $recerverNO & $message. Note that the arguments $userID and $recerverNO are redundant as both are the same in our case that is for Ways2SMS and represents your mobile number. $userPWD is the password for your Ways2SMS account and $message is the message that you want to send in the SMS.

The code checks for the existence of the cURL library. If not found, it stops the execution of the function and comes out. Else it proceeds by checking the length of the message. Ways2SMS has a limit of 140 characters for the maximum message size. So, if the message is longer than 140 characters, it is truncated, that is, only the first 140 characters of the message are used.

The location to store the cookies, the temporary file, and the user agent are defined. The URL to the login page is set along with login credentials and login button. The curl options are set for this URL and executed. The result is html page stored in $result variable. This html code is saved to the temporary file which was defined earlier.

The URL for signing out from Ways2SMS is fetched from temporary file previously created. The signing out URL can be located in the temporary file on a line which begins with “ window.location=”. Similarly the session id is also fetched from this temporary file which can located on a line which begins with “JSESSIONID”.

The URL to sent SMS is set along with the message credentials. The curl options are set for this URL and executed. The result is a html page giving the message of confirmation of the sending of the SMS which we do not save.

The URL to sign out is set, than the curl options are set for this URL and executed. The temporary files created are deleted and the function is closed.



{
if (!
function_exists('curl_init')) {
echo 
"Error : Curl library not installed";
return 
FALSE;
}
$message_urlencode rawurlencode($message);
if (
strlen($message) > 140) {
$message substr($message0139);
}

$cookie_file_path “./cookie.txt”;
$temp_file        “./temporary.txt”;
$user_agent       “Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36”;

// LOGIN TO WAY2SMS

$url        “http://site24.way2sms.com/content/Login1.action”;
$parameters = array(
“username” => $userID,
“password” => $userPWD,
“button” => “Login”
);

$ch curl_init();
curl_setopt($chCURLOPT_URL$url);
curl_setopt($chCURLOPT_POSTcount($parameters));
curl_setopt($chCURLOPT_POSTFIELDS$parameters);
curl_setopt($chCURLOPT_HEADERTRUE);
curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
curl_setopt($chCURLOPT_COOKIEJAR$cookie_file_path);
curl_setopt($chCURLOPT_COOKIEFILE$cookie_file_path);
curl_setopt($chCURLOPT_FOLLOWLOCATIONTRUE);
curl_setopt($chCURLOPT_USERAGENT$user_agent);
curl_setopt($chCURLOPT_NOBODYFALSE);
$result curl_exec($ch);
curl_close($ch);

// SAVE LOGOUT URL

file_put_contents($temp_file$result);
$result     “”;
$logout_url “”;
$file       fopen($temp_file“r”);
$line       “”;
$cond       TRUE;
while (
$cond == TRUE) {
$line fgets($file);
if (
$line === FALSE) { // EOF
$cond FALSE;
} else {
$pos strpos($line‘ window.location=”‘);
if (
$pos === FALSE) {
$line “”;
} else { 
// URL FOUND
$cond       FALSE;
$logout_url substr($line, –25);
$logout_url substr($logout_url021);
}
}
}
fclose($file);

// SAVE SESSION ID

$file fopen($cookie_file_path“r”);
$line “”;
$cond TRUE;
while (
$cond == TRUE) {
$line fgets($file);
if (
$line === FALSE) { // EOF
$cond FALSE;
} else {
$pos strpos($line“JSESSIONID”);
if (
$pos === FALSE) {
$line “”;
} else { 
// SESSION ID FOUND
$cond FALSE;
$id   substr($line$pos 15);
}
}
}
fclose($file);

// SEND SMS

$url        “http://site24.way2sms.com/smstoss.action?Token=” $id;
$parameters = array(
“button” => “Send SMS”,
“mobile” => $recerverNO,
“message” => $message
);

$ch curl_init();
curl_setopt($chCURLOPT_URL$url);
curl_setopt($chCURLOPT_POSTcount($parameters));
curl_setopt($chCURLOPT_POSTFIELDS$parameters);
curl_setopt($chCURLOPT_HEADERTRUE);
curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
curl_setopt($chCURLOPT_COOKIEJAR$cookie_file_path);
curl_setopt($chCURLOPT_COOKIEFILE$cookie_file_path);
curl_setopt($chCURLOPT_FOLLOWLOCATIONTRUE);
curl_setopt($chCURLOPT_USERAGENT$user_agent);
curl_setopt($chCURLOPT_NOBODYFALSE);
$result curl_exec($ch);
curl_close($ch);

// LOGOUT WAY2SMS

$url “site24.way2sms.com/” $logout_url;

$ch curl_init();
curl_setopt($chCURLOPT_URL$url);
curl_setopt($chCURLOPT_HEADERTRUE);
curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
curl_setopt($chCURLOPT_COOKIEJAR$cookie_file_path);
curl_setopt($chCURLOPT_COOKIEFILE$cookie_file_path);
curl_setopt($chCURLOPT_FOLLOWLOCATIONTRUE);
curl_setopt($chCURLOPT_USERAGENT$user_agent);
curl_setopt($chCURLOPT_NOBODYFALSE);
$result curl_exec($ch);
curl_close($ch);

// DELETE TEMP FILES

unlink($cookie_file_path);
unlink($temp_file);

return TRUE;

}
?>
Please share the code so that everyone benefits.

Click on this link to download the code.