4 Replies - 1518 Views - Last Post: 11 October 2012 - 12:20 AM Rate Topic: -----

#1 general07z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 18-February 12

This code is echo error message even if its correct!

Posted 10 October 2012 - 11:49 PM

This code in PHP for sending SMS

the problem with this code is either the sending SMS has been sent successfully or NOT successfully will echo for me the same message.




<?php
$baseurl = "http://sendnow.websms.my";
$username = urlencode("<snipped>");
$password = urlencode("<snipped>");
$mobile = urlencode("6".$_POST['mobile']);
$message = urlencode($_POST['message']);
$sender = urlencode($_POST['sender']);
$type = urlencode("1");

$url = $baseurl."/websmsapi/ISendSMS.aspx?username=".$username."&password=".$password.
"&mobile=".$mobile."&message=".$message.
"&sender=".$sender."&type=".$type;



// do sendmsg call
$ret = fetchURL($url);
$send = split(":",$ret);

if ($send[0] == "ID") {
// Message was submitted to gateway successfully
echo ($ret);
echo "Your Message has been sent successfully man!";

}
else {
// There was an ERROR
echo ($ret);
echo "OMG. You made an Error man!. Go back and check your staff!!";
}



function fetchURL( $url ) {
$url_parsed = parse_url($url);
$host = $url_parsed["host"];
$port = $url_parsed["port"];
if ($port==0)
$port = 80;
$path = $url_parsed["path"];
if ($url_parsed["query"] != "")
$path .= "?".$url_parsed["query"];
$out = "GET $path HTTP/1.0\r\n";
$out .= "User-Agent: gateway script\r\n";
$out .= "Host: " . $host . "\r\n";
$out .= "\r\n";

//Open the socket
$fp = fsockopen($host, $port, $errno, $errstr, 30);
if (!$fp)
echo "$errstr ($errno)<br>\r\n";
else {

fwrite($fp, $out);
$body = false;
while (!feof($fp)) {
$s = fgets($fp, 1024);
if ( $body )
$in .= $s;
if ( $s == "\r\n" )
$body = true;
}
fclose($fp);
return $in;

}

}
?>


I am sure the problem in IF ELSE but I could not solve it.

Any help please?

This post has been edited by Atli: 11 October 2012 - 12:09 AM
Reason for edit:: Removed sensitive info.


Is This A Good Question/Topic? 0
  • +

Replies To: This code is echo error message even if its correct!

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,324
  • Joined: 08-June 10

Re: This code is echo error message even if its correct!

Posted 11 October 2012 - 12:03 AM

line #59 should cause a notice or warning (string concatenation on an unitialised variable). and no matter whether the sending worked, you return the same variable (which may be unset in either case).
Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: This code is echo error message even if its correct!

Posted 11 October 2012 - 12:07 AM

Well, your fetchURL() call appears to be returning the body of the request, which you then proceed to split on : chars. You then check to see if the first of those parts is "ID", and if it is print a success message. Otherwise you print an error message.

So the question is: what is the SMS API supposed to be returning when the message is sent successfully? And what is it returning when it's unsuccessful? Without knowing that, we have no hope of knowing why the code isn't working or what you need to do to fix it.

By the way, for a long time now you've been able to use fopen(), and other such function like file_get_contents to do HTTP requests. Unless your server is extremely oddly configured, you should be able to remove the fetchURL() code and just replace it with a standard file_get_contents call:
$ret = file_get_contents($url);


Was This Post Helpful? 0
  • +
  • -

#4 general07z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 18-February 12

Re: This code is echo error message even if its correct!

Posted 11 October 2012 - 12:14 AM

Sorry I didn't note that The error between lines 20-30.
these messages echo...
"Your Message has been sent successfully man!";

"Your Message has been sent successfully man!";

r

The Return code for successful SMS is: 1701

For unsuccessful there are too much. the below:
1702 Invalid Username/Password
1703 Internal Server Error
1704 Insufficient Credits
1705 Invalid Mobile Number
1706 Invalid Message / Invalid SenderID
1707 Transfer Credits Successful
1708 Account not existing for Credits Transfer
1709 Invalid Credits Value for Credits Transfer
1718 Duplicate record received

where should I put the Successful code ID in my Code?
Was This Post Helpful? 0
  • +
  • -

#5 general07z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 18-February 12

Re: This code is echo error message even if its correct!

Posted 11 October 2012 - 12:20 AM

I have solved the problem THANKS Atli!

I have changed the ID to 1701 before but no hope.

Seems the problem solved when I change "$ret = fetchURL($url);"
to
"$ret = file_get_contents($url);"

Thanks again man!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1