send bulk sms from database - php

I've been trying to use the code below to send sms but it does not send when I loop. It only works if I just pick one number from database. I have over 5,000 numbers in the database and wish to send an sms to all of them at the same time, Please help.
mysql_select_db($database_xxx, $xxx);
$query_rs = "SELECT phone FROM `notify` order by id asc LIMIT $l1 , $l2";
$rs= mysql_query($query_rs, $xxx) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs= mysql_num_rows($rs);
$phone = $row_rs['phone'];
// Do while loop to send sms.
while($row=mysql_fetch_assoc($rs)){
// Let's do some formatting and keep smiling.
$giringirin = ereg_replace("[^0-9]", "", $phone );
if (strlen($giringirin) == 11) {
$phone1=substr($giringirin, 1);
$phone= "234$phone1";
} elseif (strlen($giringirin) == 13){
$phone = $giringirin;
}
extract($_POST);
//set POST variables
$url = "http://sms.xxx.com/bulksms/bulksms.php?username=$username&password=$password&message=$smsmessage&mobile=$phone&sender=$sender";
$fields = array(
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
if ($result == '1801') { echo "SMS has also been sent to the Customer ($phone) \n";} else { echo "Oooops, No sms was sent";}
//close connection
curl_close($ch);
}

Your code is confusing...
curl_setopt($ch,CURLOPT_POST,count($fields));
CURLOPT_POST is a boolean flag. Either you're doing a post, or you're not. The number of fields you're posting is irrelevant.
You're building up a series of variables/values to be posted, but doing it via string operations. CURL is perfectly capable of taking an array and doing all that for you, reducing your entire foreach loop to just
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
You're using extract() on $_POST, which pollutes your script's variable namespace with any garbage a malicious user cares to send over - you're essentially replicating PHP's utterly moronically brain-dead register_globals all over again.
You're using ereg, which has been deprecated for approximiately 5 million internet years. You should be using the preg functions insteadl

What happens when you run this script in the browser? Blank page? Something? Error?
Start by changing
$url = "http://sms.xxx.com/bulksms/bulksms.php?username=$username&password=$password&message=$smsmessage&mobile=$phone&sender=$sender";
to
$url = "http://sms.xxx.com/bulksms/bulksms.php?username=".$username."&password=".$password."&message=".$smsmessage."&mobile=".$phone."&sender=".$sender."";

Related

salesforce api add leads from form

I am trying to add leads from my contact form to salesforce. Currently I am using this solution that passes them through what I assume is the Web-to-Lead solution they have:
if (isset($_POST['submit'])) {
$ch_register_first_name = $_POST['ch_register_first_name'];
$ch_register_last_name = $_POST['ch_register_last_name'];
$ch_register_dob = $_POST['ch_register_dob'];
$ch_register_phone = $_POST['ch_register_phone'];
$ch_register_email = $_POST['ch_register_email'];
$ch_register_street = $_POST['ch_register_street'];
$ch_register_street2 = $_POST['ch_register_street2'];
$ch_register_city = $_POST['ch_register_city'];
$ch_register_state = $_POST['ch_register_state'];
$ch_register_zip = $_POST['ch_register_zip'];
//set POST variables
$url = 'https://test.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
$fields = array(
'last_name'=>urlencode($ch_register_first_name),
'first_name'=>urlencode($ch_register_last_name),
'street'=>urlencode($ch_register_street),
'city'=>urlencode($ch_register_city),
'state'=>urlencode($ch_register_state),
'zip'=>urlencode($ch_register_zip),
'company'=>urlencode($company),
'description'=>urlencode($ch_register_dob),
'email'=>urlencode($ch_register_phone),
'phone'=>urlencode($ch_register_email),
'mycustomefieldid' => urlencode($ch_register_dob), // custom field
'oid' => $ch_register_salesforce_id, // insert with your id
'retURL' => urlencode('http://thank-you/'), // sending this just in case
'debug' => '1',
'debugEmail' => urlencode("email#email.com"), // your debugging email
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, TRUE);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
}
The issue by doing this (if I am correct that its using the Web-to-Lead solution) the max limit is 500 in 24hrs. Some events this form will be used at day of will have more than 500 for sure. How can I use just their API I have read about? Or should I save the locally to the database then run a batch using the Force api?

Looping for certain count for curl post

I am new here to get answers for my issues, hoping for your kind advice. Thanks in advance.
I have written a HTTP API to send SMS using curl. Everything is working fine, except I am failing to loop and post curl for certain phone numbers. For example: User uploads 50000 phone numbers using excel sheet on my site, I fetch all the mobile numbers from the database, and then post it through CURL.
Now the sms gateway which I send the request accepts only maximum 10000 numbers at once via http api.
So from the 50000 fetched numbers I want to split the numbers to 10000 each and loop that and send curl post.
Here is my code
//have taken care of sql injection on live site
$resultRestore = mysql_query("SELECT * FROM temptable WHERE userid = '".$this->user_id."' AND uploadid='".$uploadid."' ");
$rowRestoreCount = mysql_num_rows($resultRestore);
#mysql_data_seek($resultRestore, 0);
$phone_list = "";
while($rowRestore = mysql_fetch_array($resultRestore))
{
$phone_list .= $rowRestore['recphone'].",";
}
$url = "http://www.smsgatewaycenter.com/library/send_sms_2.php?UserName=".urlencode($this->param[userid])."&Password=".urlencode($this->param[password])."&Type=Bulk&To=".urlencode(substr($phone_list, 0, -1))."&Mask=".urlencode($this->sendname)."&Message=Hello%20World";
//echo $url;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
Now, from the $phone_list, I need to loop for every 10000 numbers, How can I achieve this?
Its been 2 days, I have tried several things and not getting the result.
Kindly help...
NOTE: I'm going to start off with the obligatory warning about using mysql functions. Please consider switching to mysqli or PDO.
There are a number of different ways you could do this. Personally, I would reconfigure your script to only fetch 10,000 numbers at a time from the database and put that inside a loop. It might look something like this (note that for simplicity I am not updating your mysql* calls to mysqli*). Keep in mind I didn't run this through a compiler since most of your code I can't actually test
// defines where the query starts from
$offset= 0;
// defines how many to get with the query
$limit = 10000;
// set up base SQL to use over and over updating offset
$baseSql = "SELECT * FROM temptable WHERE userid = '".$this->user_id."' AND uploadid='".$uploadid."' LIMIT ";
// get first set of results
$resultRestore = mysql_query($baseSql . $offset . ', '. $limit);
// now loop
while (mysql_num_rows($resultRestore) > 0)
{
$rowRestoreCount = mysql_num_rows($resultRestore);
$phone_list = "";
while($rowRestore = mysql_fetch_array($resultRestore))
{
$phone_list .= $rowRestore['recphone'].",";
}
$url = "http://www.smsgatewaycenter.com/library/send_sms_2.php?UserName=".urlencode($this->param[userid])."&Password=".urlencode($this->param[password])."&Type=Bulk&To=".urlencode(substr($phone_list, 0, -1))."&Mask=".urlencode($this->sendname)."&Message=Hello%20World";
//echo $url;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
// now update for the while loop
// increment by value of limit
$offset += $limit;
// now re-query for the next 10000
// this will continue until there are no records left to retrieve
// this should work even if there are 50,123 records (the last loop will process 123 records)
$resultRestore = mysql_query($baseSql . $offset . ', '. $limit);
}
You could also achieve this without using offset and limit in your sql query. This might be a simpler approach for you:
// define our maximum chunk here
$max = 10000;
$resultRestore = mysql_query("SELECT * FROM temptable WHERE userid = '".$this->user_id."' AND uploadid='".$uploadid."' ");
$rowRestoreCount = mysql_num_rows($resultRestore);
#mysql_data_seek($resultRestore, 0);
$phone_list = "";
// hold the current number of processed phone numbers
$count = 0;
while($rowRestore = mysql_fetch_array($resultRestore))
{
$phone_list .= $rowRestore['recphone'].",";
$count++;
// when count hits our max, do the send
if ($count >= $max)
{
$url = "http://www.smsgatewaycenter.com/library/send_sms_2.php?UserName=".urlencode($this->param[userid])."&Password=".urlencode($this->param[password])."&Type=Bulk&To=".urlencode(substr($phone_list, 0, -1))."&Mask=".urlencode($this->sendname)."&Message=Hello%20World";
//echo $url;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
// now reset count back to zero
$count = 0;
// and reset phone_list
$phone_list = '';
}
}
// if we don't have # of phones evenly divisible by $max then handle any leftovers
if ($count > 0)
{
$url = "http://www.smsgatewaycenter.com/library/send_sms_2.php?UserName=".urlencode($this->param[userid])."&Password=".urlencode($this->param[password])."&Type=Bulk&To=".urlencode(substr($phone_list, 0, -1))."&Mask=".urlencode($this->sendname)."&Message=Hello%20World";
//echo $url;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
}
I notice that you are retrieving the information in $curl_scraped_page. In either of these scenarios above, you will need to account for the new loop if you're doing any processing on $curl_scraped_page.
Again, please consider switching to mysqli or PDO, and keep in mind that there are likely more efficient and flexible ways to achieve this than what you are doing here. For example, you might want to log successful sends in case your script breaks and incorporate that into your script (for example, by selecting from the database only those numbers that have not yet received this text). This would allow you to re-run your script but only send to those who did NOT yet receive the text, rather than hitting everyone again (or maybe your SMS gateway handles that for you?)
EDIT
Another approach would be to load all the retrieved numbers into a single array, then chunk the array into pieces and process each chunk.
$numbers = array();
while ($rowRestore = mysql_fetch_array($resultRestore))
{
$numbers[] = $rowRestore['recphone'];
}
// split into chunks of 10,000
$chunks = array_chunk($numbers, 10000);
// loop and process the chunks
foreach ($chunks AS $chunk)
{
// $chunk will be an array, so implode it with comma to get the phone list
$phone_list = implode(',', $chunk);
// note that there is no longer a need to substr -1 the $phone_list because it won't have a trailing comma using implode()
$url = "http://www.smsgatewaycenter.com/library/send_sms_2.php?UserName=".urlencode($this->param[userid])."&Password=".urlencode($this->param[password])."&Type=Bulk&To=".urlencode($phone_list)."&Mask=".urlencode($this->sendname)."&Message=Hello%20World";
//echo $url;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
}

PHP Cron job process with marker in URL

I want to process a script every minute using a cron on my server but I need to pass a variable in the URL or some other way. I have researched this and I've seen solutions using arguments in the cron but I don't think that works with what I'm doing.
Here is what I am trying to do:
script.php (runs every minute)
<?php
$marker = $_GET['marker'];
$accountObj = new etAccounts($consumer);
$request_params = new TransactionHistoryRequest();
$request_params->__set('count', 50); //how many will be shown
if($marker_get != ''){
$request_params->__set('marker', $marker_get); //starting point ex. 14293200140265
}
$json = $accountObj->GetTransactionHistory($account, $request_obj, $request_params );
echo $json; //shows most recent 50 transactions starting from marker value
//process json data here...
//included in json is a marker variable that will be used to return the next 50 json results
//after data is processed reload the page with marker in URL
header('Location: script.php?marker=14293200140265');
?>
I understand that cron is CLI on the server side and that it can't process redirections or header locations but how is this possible. I saw someone mention using CURL, how might this work? Example?
Simple example to send post variables to an url:
$fields = array(
'id' => $id,
'mail' => $mail,
);
$url = "yourdomain.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
Based on the comments, you don't need to redirect or use query variables.
You could use a loop that runs while your marker variable is not empty:
$your_marker_variable = '';
do {
$accountObj = new etAccounts($consumer);
$request_params = new TransactionHistoryRequest();
$request_params->__set('count', 50); //how many will be shown
if($marker_get != '') {
$request_params->__set('marker', $marker_get); //starting point ex. 14293200140265
}
$json = $accountObj->GetTransactionHistory($account, $request_obj, $request_params );
echo $json; //shows most recent 50 transactions starting from marker value
//process json data here...
//included in json is a marker variable that will be used to return the next 50 json results
// set the new value of $your_marker_variable
$your_marker_variable = ...
} while (!empty($your_marker_variable));
Note that the undefined and unused variables in the script you posted, make it hard to see what variable is used for what, so you would need to adapt this a bit.

json_decode in PHP to set a cookie

I want to decode Json returned from the WebService, And it should set a cookie, that i want to use to call next WebService API. I am not sure how to set a cookie, and decode this json.
I tried decoding it, but get the error. I need to extract sessionId. You can use the WebService.. I have put this on Internet.
Here is my code sample
<?php //extract data from the post
extract($_POST); //set POST variables
$url = 'http://202.83.243.119/ems/loginByEID.json';
$fields = array(
'eid'=>urlencode("7ea888b6-36e9-49db-84f3-856043841bef")
);
//url-ify the data for the POST
foreach($fields as $key=>$value) {
$fields_string .=
$key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection $ch = curl_init();
//set the url, number of POST vars,
POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post $result =
curl_exec($ch);
//close connection curl_close($ch);
//decoding Json $obj =
json_decode($result);
print $obj->{'stat'};
?>
Use setcookie() to set a cookie.
Avoid extract() like the plague; it can be used to introduce any client-specified variables into your code.
Use $fields_string = http_build_query($_GET) to build a query string instead of your hodge-podge above.
Format your code properly. For example, you put the $obj = inside the previous comment line.

Make a PHP GET request from a PHP script and exit

Is there something simpler than the following.
I am trying to make a GET request to a PHP script and then exit the current script.
I think this is a job for CURL but is there something simpler as I don't want to really worry about enabling the CURL php extension?
In addition, will the below start the PHP script and then just come back and not wait for it to finish?
//set GET variables
$url = 'http://domain.com/get-post.php';
$fields = array(
'lname'=>urlencode($last_name),
'fname'=>urlencode($first_name)
);
//url-ify the data for the GET
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_GET,count($fields));
curl_setopt($ch,CURLOPT_GETFIELDS,$fields_string);
//execute GET
$result = curl_exec($ch);
//close connection
curl_close($ch);
I want to run the other script which contains functions when a condition is met so a simple include won't work as the if condition wraps around the functions, right?
Please note, I am on windows machine and the code I am writing will only be used on a Windows OS.
Thanks all for any help and advice
$url = 'http://domain.com/get-post.php?lname=' . urlencode($last_name) . '&fname=' . urlencode($first_name);
$html = file_get_contents($url);
If you want to use the query string assembly method (from the code you posted):
//set GET variables
$url = 'http://domain.com/get-post.php';
$fields = array(
'lname'=>urlencode($last_name),
'fname'=>urlencode($first_name)
);
//url-ify the data for the GET
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
$html = file_get_contents($url . '?' . $fields_string);
See:
http://php.net/manual/en/function.file-get-contents.php

Categories