Jump to content

send sms PHP coding


ianhaney50

Recommended Posts

Hi

 

I am building a SMS coding script that does the same for my auto email script that pings a email out to signed up users if their expiry date is within 14 and 7 days and I set up a cron job to check it every day and now am building a SMS script to do the same so it sends a SMS text out to the signed up users, I am using text marketer and all they said was it is possible with their system and said to build a script and put the system into it so have put together the following coding and on the php page it is just saying SUCCESS so looks like it has connected to the database successfully but just seeing if anyone would mind just checking over the coding to see if I have missed anything please

<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
?>

<?php

$db = mysqli_connect("" , "", "") or die("Check connection parameters!"); 
// Optionally skip select_db and use: mysqli_connect(host,user,pass,dbname)  
mysqli_select_db($db,"") or die(mysqli_error($db));

if (mysqli_connect_error()) {
    die ('Failed to connect to MySQL');
} else {
	/*SUCCESS MSG*/
	echo 'SUCCESS';
}

$sqlCommand = "SELECT 
        u.id
		, name
		, mobnumber
        , item.description
        , renewal_id
        , DATE_FORMAT(renewal_date, '%e %M %Y') as datedue
        , renewal_date
        FROM users u
            INNER JOIN renewal USING (id)
            INNER JOIN item USING (item_id)
        WHERE renewal_date BETWEEN CURDATE() AND CURDATE()+INTERVAL 14 DAY
            AND IFNULL(date_notified, '1901-01-01') < CURDATE()-INTERVAL 14 DAY

        UNION

        SELECT 
        u.id
		, name
		, mobnumber
        , item.description
        , renewal_id
        , DATE_FORMAT(renewal_date, '%e %M %Y') as datedue
        , renewal_date
        FROM users u
            INNER JOIN renewal USING (id)
            INNER JOIN item USING (item_id)
        WHERE renewal_date BETWEEN CURDATE() AND CURDATE()+INTERVAL 7 DAY
            AND IFNULL(date_notified, '1901-01-01') < CURDATE()-INTERVAL 7 DAY
        ORDER BY id, renewal_date";

$query = mysqli_query($db, $sqlCommand) or die (mysqli_error($db));

$current_visitor=0;
$mobnumber = "";
if(isset($_GET['mobnumber'])){ $mobnumber = $_GET['mobnumber']; }

//fetch the data from the database 
while ($row = mysqli_fetch_array($query)) {

$to = $mobnumber;
$name = '';
if(isset($_POST['name'])){ $name = $_POST['name']; }
$message = $name."Name:".$row['name'].$row['description'].$row['datedue'];

if ($row['id'] != $current_visitor) {
	
	if ($current_visitor != 0) {
			$to = $mobnumber;
			
	$current_visitor = $row['id'];

    // Simple SMS send function
    function sendSMS($username, $password, $to, $message, $originator) {
        $URL = 'http://api.textmarketer.co.uk/gateway/'."?username=$username&password=$password&option=xml";
        $URL .= "&to=$to&message=".urlencode($message).'&orig='.urlencode($originator);
        $fp = fopen($URL, 'r');
        return fread($fp, 1024);
    }
    // Example of use
    $response = sendSMS('textmarketerusername', 'textmarketerpassword', '{$mobnumber}', '{$row["description"]} expiry date: {$row["datedue"]}', '{$row["description"]} expiry date: {$row["datedue"]}');	
    echo $response;
}

}

}
	
?>

Thank you in advance

Link to comment
Share on other sites

If you only want to send a single SMS, why are fetching records for everyone that has due due dates then checking each one for the $current_visitor? Make it a condition in the WHERE clauses so you only get the data you need.

 

BTW, $current_visitor doesn't appear to be defined anywhere.

Link to comment
Share on other sites

The variables on this line won't fire cause you have them inside 'single quotes'.

 $response = sendSMS('textmarketerusername', 'textmarketerpassword', '{$mobnumber}', '{$row["description"]} expiry date: {$row["datedue"]}', '{$row["description"]} expiry date: {$row["datedue"]}');
Link to comment
Share on other sites

I changed that line to the following

$response = sendSMS("username", "password", "{$mobnumber}", "{$row['description']} expiry date: {$row['datedue']}", "{$row['description']} expiry date: {$row['datedue']}");

I get the following error with that

 

invalid number or not an integer

Link to comment
Share on other sites

Ahh just moved a } to below the last of the PHP and the error has gone and just got a success message from where it is connected to the database

<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
?>

<html>
<title>Automatic SMS Text</title>
<body>

<?php

$db = mysqli_connect("" , "", "") or die("Check connection parameters!"); 
// Optionally skip select_db and use: mysqli_connect(host,user,pass,dbname)  
mysqli_select_db($db,"") or die(mysqli_error($db));

if (mysqli_connect_error()) {
    die ('Failed to connect to MySQL');
} else {
	/*SUCCESS MSG*/
	echo 'SUCCESS';
}

$sqlCommand = "SELECT 
        u.id
		, name
		, mobnumber
        , item.description
        , renewal_id
        , DATE_FORMAT(renewal_date, '%e %M %Y') as datedue
        , renewal_date
        FROM users u
            INNER JOIN renewal USING (id)
            INNER JOIN item USING (item_id)
        WHERE renewal_date BETWEEN CURDATE() AND CURDATE()+INTERVAL 14 DAY
            AND IFNULL(date_notified, '1901-01-01') < CURDATE()-INTERVAL 14 DAY

        UNION

        SELECT 
        u.id
		, name
		, mobnumber
        , item.description
        , renewal_id
        , DATE_FORMAT(renewal_date, '%e %M %Y') as datedue
        , renewal_date
        FROM users u
            INNER JOIN renewal USING (id)
            INNER JOIN item USING (item_id)
        WHERE renewal_date BETWEEN CURDATE() AND CURDATE()+INTERVAL 7 DAY
            AND IFNULL(date_notified, '1901-01-01') < CURDATE()-INTERVAL 7 DAY
        ORDER BY id, renewal_date";

$query = mysqli_query($db, $sqlCommand) or die (mysqli_error($db));

$mobnumber = "";
if(isset($_GET['mobnumber'])){ $mobnumber = $_GET['mobnumber']; }

//fetch the data from the database 
while ($row = mysqli_fetch_array($query)) {

$to = $mobnumber;
$name = '';
if(isset($_POST['name'])){ $name = $_POST['name']; }
$message = $name."Name:".$row['name'].$row['description'].$row['datedue'];
}
    // Simple SMS send function
    function sendSMS($username, $password, $to, $message, $originator) {
        $URL = 'http://api.textmarketer.co.uk/gateway/'."?username=$username&password=$password&option=xml";
        $URL .= "&to=$to&message=".urlencode($message).'&orig='.urlencode($originator);
        $fp = fopen($URL, 'r');
        return fread($fp, 1024);
    
    // Example of use
	$response = sendSMS('', '', '447538503276', 'test message', 'test message 1');
    /*$response = sendSMS("47799", "692px", "{$mobnumber}", "{$row['description']} expiry date: {$row['datedue']}", "{$row['description']} expiry date: {$row['datedue']}");*/
    echo $response;
	}
	
?>

</body>
</html>

I was hoping I would receive a text on my mobile number but nothing has came through

Link to comment
Share on other sites

Just a update, I have done a var_dump($message) and outputs the following data so is connected to the database and getting the correct data, is just the SMS is not sending

 

string(29) "Name:Ian HaneyTax10 July 2015"

// Simple SMS send function
    function sendSMS($username, $password, $to, $message, $message) {
        $URL = 'http://api.textmarketer.co.uk/gateway/'."?username=$username&password=$password&option=xml";
        $URL .= "&to=$to&message=".urlencode($message).'&orig='.urlencode($message);
        $fp = fopen($URL, 'r');
        return fread($fp, 1024);
    
    // Example of use
    $response = sendSMS('username', 'password', $mobnumber, "{$row["description"]} expiry date: {$row["datedue"]}", "{$row["description"]} expiry date: {$row["datedue"]}");  
	
	echo $response;
	
	}
Link to comment
Share on other sites

I think it is struggling to retrieve the mobnumber from the database as done a var_dump($mobnumber) and just outputs the following

 

 

The code is below

$mobnumber = "";
if(isset($_GET['mobnumber'])){ $mobnumber = $_GET['mobnumber']; }

//fetch the data from the database 
while ($row = mysqli_fetch_array($query)) {

$originator = 'Tax Elephants';
$to = 'mobnumber';
$name = '';
if(isset($_POST['name'])){ $name = $_POST['name']; }
$message = $name."Name:".$row['name'].$row['description'].$row['datedue'];
}
    // Simple SMS send function
    function sendSMS($username, $password, $to, $message, $originator) {
        $URL = 'http://api.textmarketer.co.uk/gateway/'."?username=$username&password=$password&option=xml";
        $URL .= "&to=$to&message=".urlencode($message).'&orig='.urlencode($originator);
        $fp = fopen($URL, 'r');
        return fread($fp, 1024);
    
    // Example of use
    $response = sendSMS('username', 'password', "{$row["mobnumber"]}", "{$row["description"]} expiry date: {$row["datedue"]}", "{$row["description"]} expiry date: {$row["datedue"]}");  
	
	echo $response;
	
	}
	
	var_dump($mobnumber)
Link to comment
Share on other sites

Sorry think I got the mobnumber being retrieved from the database by the following

 



$mobnumber = $row['mobnumber'];


 

so now when I do var_dump($mobnumber)

 

it displays the mobnumber on the php page

 

so just wondering why the SMS is not sending?

 

the updated code is below

 



$db = mysqli_connect("" , "", "") or die("Check connection parameters!");
// Optionally skip select_db and use: mysqli_connect(host,user,pass,dbname)
mysqli_select_db($db,"") or die(mysqli_error($db));

if (mysqli_connect_error()) {
die ('Failed to connect to MySQL');
} else {
/*SUCCESS MSG*/
echo 'SUCCESS';
}

$sqlCommand = "SELECT
u.id
, name
, mobnumber
, item.description
, renewal_id
, DATE_FORMAT(renewal_date, '%e %M %Y') as datedue
, renewal_date
FROM users u
INNER JOIN renewal USING (id)
INNER JOIN item USING (item_id)
WHERE renewal_date BETWEEN CURDATE() AND CURDATE()+INTERVAL 14 DAY
AND IFNULL(date_notified, '1901-01-01') < CURDATE()-INTERVAL 14 DAY

UNION

SELECT
u.id
, name
, mobnumber
, item.description
, renewal_id
, DATE_FORMAT(renewal_date, '%e %M %Y') as datedue
, renewal_date
FROM users u
INNER JOIN renewal USING (id)
INNER JOIN item USING (item_id)
WHERE renewal_date BETWEEN CURDATE() AND CURDATE()+INTERVAL 7 DAY
AND IFNULL(date_notified, '1901-01-01') < CURDATE()-INTERVAL 7 DAY
ORDER BY id, renewal_date";

$query = mysqli_query($db, $sqlCommand) or die (mysqli_error($db));

/*$mobnumber = "";
if(isset($_GET['mobnumber'])){ $mobnumber = $_GET['mobnumber']; }*/

//fetch the data from the database
while ($row = mysqli_fetch_array($query)) {

$originator = 'Tax Elephants';
$mobnumber = $row['mobnumber'];
/*$name = '';*/
$name = $row['name'];
/*if(isset($_POST['name'])){ $name = $_POST['name']; }*/
$message = $name."Name:".$row['name'].$row['description'].$row['datedue'];
}
// Simple SMS send function
function sendSMS($username, $password, $mobnumber, $message, $originator) {
$URL = 'http://api.textmarketer.co.uk/gateway/'."?username=$username&password=$password&option=xml";
$URL .= "&to=$to&message=".urlencode($message).'&orig='.urlencode($originator);
$fp = fopen($URL, 'r');
return fread($fp, 1024);

// Example of use
$response = sendSMS('username', 'password', "{$row["mobnumber"]}", "{$row["description"]} expiry date: {$row["datedue"]}", "{$row["description"]} expiry date: {$row["datedue"]}");

echo $response;

}

var_dump($message)

Edited by ianhaney50
Link to comment
Share on other sites

You don't have a closing bracket on your function.

 

Please always develop with errors turned on to the strictest setting, so that you can avoid simple problems like this. At the top of your script put:

ini_set('display_errors', 1);
error_reporting(-1);
Or find these directives in the php.ini and set them there, so that they are set globally.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.