Page 1 of 2

Re: send SMS php coding

Posted: Tue Jul 07, 2015 7:58 am
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?

Re: send SMS php coding

Posted: Tue Jul 07, 2015 8:51 am
by Celauran
I'm assuming it works when you send to yourself?

Re: send SMS php coding

Posted: Tue Jul 07, 2015 9:37 am
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.

Re: send SMS php coding

Posted: Tue Jul 07, 2015 1:15 pm
by Celauran
What is sendSMS returning?

Re: send SMS php coding

Posted: Tue Jul 07, 2015 1:39 pm
by Celauran
What is the value of $response?

Re: send SMS php coding

Posted: Thu Jul 09, 2015 5:25 am
by Celauran
Have you var_dump'ed the return value of sendSMS yet like I asked?

Re: send SMS php coding

Posted: Thu Jul 09, 2015 5:34 am
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);

Re: send SMS php coding

Posted: Thu Jul 09, 2015 5:43 am
by Celauran
ianhaney wrote:that just displays SUCCESS from the MySQL connection
No output at all? That means it's not even executing.

Re: send SMS php coding

Posted: Thu Jul 09, 2015 5:50 am
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;
}

Re: send SMS php coding

Posted: Thu Jul 09, 2015 5:51 am
by Celauran
The query is fine. Don't worry about the query.

Re: send SMS php coding

Posted: Thu Jul 09, 2015 5:54 am
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.

Re: send SMS php coding

Posted: Thu Jul 09, 2015 5:58 am
by Celauran
Yes, your function definition is broken. I corrected that above.

Re: send SMS php coding

Posted: Thu Jul 09, 2015 5:59 am
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);

Re: send SMS php coding

Posted: Thu Jul 09, 2015 7:59 am
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);

Re: send SMS php coding

Posted: Thu Jul 09, 2015 8:19 am
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.