send SMS php coding

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

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: send SMS php coding

Post by Celauran »

Not sure what we're looking for exactly. I wouldn't define that function inside a conditional. Are you encountering any specific errors?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: send SMS php coding

Post by Celauran »

I'm assuming it works when you send to yourself?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: send SMS php coding

Post by Celauran »

Have you tested sending independently of the SQL query (ie. with hardcoded values)? Try to isolate individual points of failure and fix those independently.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: send SMS php coding

Post by Celauran »

What is sendSMS returning?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: send SMS php coding

Post by Celauran »

What is the value of $response?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: send SMS php coding

Post by Celauran »

Have you var_dump'ed the return value of sendSMS yet like I asked?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: send SMS php coding

Post by Celauran »

Variables don't return values. Functions do. You have a function called sendSMS.

Code: Select all

$response = sendSMS('47799', '692px', '447538503276', 'test message', 'test message 1');
var_dump($response);
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: send SMS php coding

Post by Celauran »

ianhaney wrote:that just displays SUCCESS from the MySQL connection
No output at all? That means it's not even executing.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: send SMS php coding

Post by Celauran »

Just went back and looked at your latest code posting. You're calling the function inside the function definition. That's not going to work.

Code: Select all

<?php

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);
}

$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');
}
// You don't need a success message; it will tell you if it fails.

$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));

//fetch the data from the database
while ($row = mysqli_fetch_array($query)) {
    $originator = 'Tax Elephants';
    $mobnumber = $row['mobnumber'];

    $name = $row['name'];

    $message = $name."Name:".$row['name'].$row['description'].$row['datedue'];
    $response = sendSMS('username', 'password', $row["mobnumber"], "{$row["description"]} expiry date: {$row["datedue"]}", $originator);  
    var_dump($response); exit;
}
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: send SMS php coding

Post by Celauran »

The query is fine. Don't worry about the query.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: send SMS php coding

Post by Celauran »

If the query is fine, and you have established that it is, what else is left? Don't need to lean on them, though, just start debugging it. What are you getting back when you call that function? We can go from there.

I would skip the query altogether for the time being. Define the function, call it with a set of known good data, and work toward getting the function itself working. Once that's done, you can use it with your query results. Identify the point of failure, isolate it, and get it working before moving on.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: send SMS php coding

Post by Celauran »

Yes, your function definition is broken. I corrected that above.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: send SMS php coding

Post by Celauran »

Code: Select all

<?php

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);
}

$response = sendSMS('put', 'real', 'values', 'here', 'please');
var_dump($response);
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: send SMS php coding

Post by Celauran »

Code: Select all

<?php

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);
}

$response = sendSMS('47799', '692px', '447538503276', 'expiry date:', '10th July 2015');
var_dump($response);
and nothing else. Forget the query, the DB connection, everything. We know those are fine.

[text]42invalid originator or too long[/text]
This is the kind of thing we're looking for. Check Text Marketer's API and/or documentation for what that error signifies. My guess is that $originator is meant to be the sender's number. Something like

Code: Select all

$response = sendSMS('47799', '692px', '447538503276', 'expiry date: 10th July 2015', '4420812345678');
var_dump($response);
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: send SMS php coding

Post by Celauran »

So it looks like bad values for $originator were causing it to fail. Now add back the rest of the code and get that working.
Post Reply