PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
bob_the _builder
Forum Contributor
Posts: 131 Joined: Sat Aug 28, 2004 12:25 am
Post
by bob_the _builder » Mon May 14, 2007 3:40 am
Hi,
Is the following a reasonable way to loop through database and send out sms, or are there better ways to go about it?
Code: Select all
$sql = mysql_query("SELECT * FROM calendar WHERE sendtxt = 1");
while ($row = mysql_fetch_array($sql)){
$user = '###';
$password = '###';
$api_id = '###';
$baseurl ="http://api.clickatell.com";
$text = urlencode("This is an example message");
$to = "".$row['cellphone']."";
$url = "$baseurl/http/auth?user=$user&password=$password&api_id=$api_id";
$ret = file($url);
$sess = split(":",$ret[0]);
if ($sess[0] == "OK") {
$sess_id = trim($sess[1]);
$url = "$baseurl/http/sendmsg?session_id=$sess_id&to=$to&text=$text";
$ret = file($url);
$send = split(":",$ret[0]);
if ($send[0] == "ID")
echo "success<br>message ID: ". $send[1];
else
echo "send message failed";
}else{
echo "Authentication failure: ". $ret[0];
}
}
Thanks
Sparky
Forum Newbie
Posts: 11 Joined: Sat May 12, 2007 10:54 am
Post
by Sparky » Mon May 14, 2007 3:51 am
Firstly - you've got variables that don't change included within the loop. They should be taken outside of the loop for 'speed' (though it won't make a difference - it's better practise IMO).
Otherwise, though, this appears to be a perfectly good way of sending messages.... Oh, don't forget to update the MySQL database so you don't keep sending the same messages
bob_the _builder
Forum Contributor
Posts: 131 Joined: Sat Aug 28, 2004 12:25 am
Post
by bob_the _builder » Mon May 14, 2007 5:05 am
Thanks,
I have got:
Code: Select all
$user = '###';
$password = '###';
$api_id = '###';
$baseurl ='http://api.clickatell.com';
$url = "$baseurl/http/auth?user=$user&password=$password&api_id=$api_id";
$ret = file($url);
$sess = split(":",$ret[0]);
$sess_id = trim($sess[1]);
if ($sess[0] == "OK") {
$sql = mysql_query("SELECT * FROM calendar WHERE sendtxt = 1");
while ($row = mysql_fetch_array($sql)){
$text = urlencode('Hi '.$row['firstname'].' message here');
$url = "$baseurl/http/sendmsg?session_id=$sess_id&to=".$row['cellphone']."&text=$text";
$ret = file($url);
}
if (!$ret)
echo 'send message failed';
else
echo 'success';
}else{
echo 'Authentication failure';
}
Cleans it up, is that ok for the job ..
Thanks
Sparky
Forum Newbie
Posts: 11 Joined: Sat May 12, 2007 10:54 am
Post
by Sparky » Mon May 14, 2007 5:24 am
Looks good to me!