Sms script for nowsms

Sms script for nowsms SearchSearch

SMS & MMS Technical Forum » NowSMS Support - SMS Issues (Product Support Only) » Archive through June 24, 2009 » Sms script for nowsms « Previous || Next »
Author Message
Andrew
New member
Username: Drew1982

Post Number: 3
Registered: 10-2008
Posted on Sunday, May 03, 2009 - 12:41 pm:   

Hi there I had some support for this issue before now but could not figure out how to use nowsms to offer a sms service to my website users, I know that using a mobile connected to my pc may be costly for me but I would still like to offer a sms service on my website.

The problem is im a total novice with scripts and so the PHP code examples found on this site I am having trouble with.... is there a PHP script that I could copy and paste in order for me to do this (only editing the server and port in the script for my nowsms app?)

I tried to copy and paste the code examples given on this great site in a file I called sms.php but to no avail. I understand that this message makes me look stupid but we all have to learn somewhere.... many thanks
Des - NowSMS Support
Board Administrator
Username: Desosms

Post Number: 738
Registered: 08-2008
Posted on Monday, May 04, 2009 - 07:35 pm:   

Hi Andrew,

Don't worry about looking stupid ... your message is actually quite clear about what you are looking for.

Grab the example script here:

http://www.nowsms.com/download/sendsms-php.txt

Save it with a .php extension.

Edit the following line in that script:

$x = SendSMS("127.0.0.1", 8800, "username", "password", "+44999999999", "Test Message");

If you're running on the same PC as NowSMS, "127.0.0.1" is ok ... otherwise enter the IP address of the PC running NowSMS.

8800 is the "port number for the web interface" as configured on the "Web" page of the NowSMS configuration.

"username" and "password" correspond to an account defined on the "SMS Users" page of the NowSMS configuration.'

Replace "+44999999999" with the recipient phone number that you want to send to.

Replace "Test Message" with the message that you want to send.

Run the script from the command line (PHP scriptname.php) to see if it is working properly.

Next, run it as a web page, keeping the recipient address hard coded ... and verify that it is still working properly.

Your next step is to remove the following from the script, so that it is no longer hard coded to send your test message:

$x = SendSMS("127.0.0.1", 8800, "username", "password", "+44999999999", "Test Message");
echo $x;

Take a look at the commented out section below those lines in the example script. The first bit is a very simple HTML form that you can try using.

The next part is a PHP script to receive the example form and send an SMS message. Uncomment the PHP script part so that it replaces the hard coded call to SendSMS.

Does that help?

I could probably edit that example file into separate step examples if you're still confused, but those should be pretty simple edits.

The hardest part tends to be getting PHP configured properly on your web server. With some web servers it is easy, with some it is not.

That's why I suggest trying it from the command line first ... then trying the script with a hard coded message being sent ... then moving on to your own web form. That way if you encounter a problem, you have a better idea what needs to be working properly before moving on to the next step.

--
Des
NowSMS Support
Andrew
New member
Username: Drew1982

Post Number: 4
Registered: 10-2008
Posted on Monday, May 04, 2009 - 09:46 pm:   

hi there thank you so much for your reply, i have most definately progressed now, i got as far as the test message from my website which sent to me immediately and successfully. i achieved this by pasting the following code.......

<?php

function SendSMS ($host, $port, $username, $password, $phoneNoRecip, $msgText) {

/* Parameters:
$host - IP address or host name of the NowSMS server
$port - "Port number for the web interface" of the NowSMS Server
$username - "SMS Users" account on the NowSMS server
$password - Password defined for the "SMS Users" account on the NowSMS Server
$phoneNoRecip - One or more phone numbers (comma delimited) to receive the text message
$msgText - Text of the message
*/

$fp = fsockopen($host, $port, $errno, $errstr);
if (!$fp) {
echo "errno: $errno \n";
echo "errstr: $errstr\n";
return $result;
}

fwrite($fp, "GET /?Phone=" . rawurlencode($phoneNoRecip) . "&Text=" . rawurlencode($msgText) . " HTTP/1.0\n");
if ($username != "") {
$auth = $username . ":" . $password;
$auth = base64_encode($auth);
fwrite($fp, "Authorization: Basic " . $auth . "\n");
}
fwrite($fp, "\n");

$res = "";

while(!feof($fp)) {
$res .= fread($fp,1);
}
fclose($fp);


return $res;
}


/* This code provides an example of how you would call the SendSMS function from within
a PHP script to send a message. The response from the NowSMS server is echoed back from the script.

$x = SendSMS("127.0.0.1", 8800, "username", "password", "+44999999999", "Test Message");
echo $x;

in to a file which i called sendsms.php and the test message worked great. So I then placed the following code in to sendsms.html...

<HTML>
<HEAD><TITLE>Send SMS</TITLE></HEAD>
<BODY>
<form method="post" action="sendsmsscript.php">
<table border="1">
<tr>
<td>Mobile Number:</td>
<td><input type="text" name="phone" size="40"></td>
</tr>
<tr>
<td valign="top">Text Message:</td>
<td><textarea name="text" cols="80" rows="10"></textarea>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Send">
</td>
</tr>
</table>
</form>
</BODY>

the form is great but when i try to send sms i get an error, i assume thats because there is a third part to the code which is.....

if (isset($_REQUEST['phone'])) {
if (isset($_REQUEST['text'])) {
$x = SendSMS("127.0.0.1", 8800, "username", "password", $_REQUEST['phone'], $_REQUEST['text']);
echo $x;
}
else {
echo "ERROR : Message not sent -- Text parameter is missing!\r\n";
}
}
else {
echo "ERROR : Message not sent -- Phone parameter is missing!\r\n";
}

*/

?>

Thats the part in having a problem with, does this part go on to the end of the previous codes?


As a test I tried to just save the hole code at http://www.nowsms.com/download/sendsms-php.txt in to one single file which i called sms.php but the whole source in one php file seems to just only load a blank empty page.

I understand that you are very busy and thank you for your help. you have created a great gateway!

Im sure this is very simple to you but I hope that my posting this topic can help more people having teething problems, thanks again Des
Andrew
New member
Username: Drew1982

Post Number: 5
Registered: 10-2008
Posted on Monday, May 04, 2009 - 10:01 pm:   

Just tried to edit the file as a whole after removing the comment part of the script (I hadnt noticed that instruction before).... Now I get the error...

Parse error: syntax error, unexpected '<' in /var/www/smsclist.com/sms.php on line 34

Hope this helps and doesnt make me look dafter, thanks again

Line 34 is where the <HTML> starts
Des - NowSMS Support
Board Administrator
Username: Desosms

Post Number: 743
Registered: 08-2008
Posted on Wednesday, May 06, 2009 - 04:31 pm:   

Hi Andrew,

Let's take it one step at a time.

Start with the code download from http://www.nowsms.com/download/sendsms-php.txt.

Edit this file to uncomment the following section:


quote:

/* This code provides an example of how you would call the SendSMS function from within
a PHP script to send a message. The response from the NowSMS server is echoed back from the script.

$x = SendSMS("127.0.0.1", 8800, "username", "password", "+44999999999", "Test Message");
echo $x;

*/




After uncommenting, it should look like this:


quote:

/* This code provides an example of how you would call the SendSMS function from within
a PHP script to send a message. The response from the NowSMS server is echoed back from the script. */

$x = SendSMS("127.0.0.1", 8800, "username", "password", "+44999999999", "Test Message");
echo $x;




Basically, I moved the "*/" to close the comment up after the descriptive text. Everything between "/*" and "/*" is considered to be a comment and is ignored by the PHP script processor.

Now run the script ... you should either see an error about connecting to 127.0.0.1, or some HTML about the message being submitted.

This error:


quote:

Parse error: syntax error, unexpected '<' in /var/www/smsclist.com/sms.php on line 34




... is occurring because the "<HTML>" tag is not valid in a script unless you put it in an "echo" command or something similar in order to return it as output from the PHP script.

The idea of the HTML in the example is that this HTML would be saved as a separate file. It is a simple web form that submits to a PHP script.

In order to use that HTML form, comment out this part:


quote:

$x = SendSMS("127.0.0.1", 8800, "username", "password", "+44999999999", "Test Message");
echo $x;




And uncomment this part:


quote:

if (isset($_REQUEST['phone'])) {
if (isset($_REQUEST['text'])) {
$x = SendSMS("127.0.0.1", 8800, "username", "password", $_REQUEST['phone'], $_REQUEST['text']);
echo $x;
}
else {
echo "ERROR : Message not sent -- Text parameter is missing!\r\n";
}
}
else {
echo "ERROR : Message not sent -- Phone parameter is missing!\r\n";
}




Note that the HTML form example expects your PHP script to be named sendsmsscript.php, but you can easily change that by editing the HTML.

--
Des
NowSMS Support
byant
New member
Username: Byzantine

Post Number: 1
Registered: 06-2009
Posted on Friday, June 19, 2009 - 06:53 pm:   

Hi, I installed Nowsms and modem in my local pc. because i want send sms from my database phone in server. So I placed file sendsms.php and run from server. but I've got an error like this one "Warning: fsockopen() [function.fsockopen]: unable to connect to 127.0.0.1:8800 (Connection refused)..".
please help me how to solve this problem.
Thank's
Bryce Norwood - NowSMS Support
Board Administrator
Username: Bryce

Post Number: 7799
Registered: 10-2002
Posted on Monday, June 22, 2009 - 01:57 pm:   

Hi,

The PC running NowSMS and the server running your PHP script are not the same PC, correct?

You need to edit sendsms.php and change 127.0.0.1 to be the IP address of the PC running NowSMS.

-bn

Login Login / Register Logout Logout Search Last 30 Days Topics Topics