-1

In my PHP project I want to integrate SMS gateway.

I have integrated HSPSMS SMS gateway.

They have given one API for this,but unfortunately I am unable to call this in proper way. I have called API after user successfully registered to site for sending SMS to user regarding same to notify he had successfully registered. Currently I am sending SMS for same but API's can send response back for SMS delivery(Successfully SMS sent or not in a JSON format).

Here is a problem- I am unable to caught the return response of SMS gateway,so it causes the Response is showing on user/Web Page.

It is a problematic for user.

For calling SMS gateway I have used PHP Header function like:

header("Location:URL of SMS Gateway");

My Code as Bellow,

<?php
include("otherpages/connection.php");

if(isset($_POST['submit1'])) {
    $mname=$_POST['mname'];
    $cdate=$_POST['cdate'];
    $maddress1=$_POST['maddress'];
    $mschool=$_POST['mschool'];
    $mkendra=$_POST['centrename'];
    $mmobile=$_POST['mmobile'];
    $user=$_POST['user'];
    $pass=$_POST['pass'];
    $approved="0";

    $qr="insert into tempregistration values('','$cdate','$mname','$maddress1','$mschool','$mschool','$mkendra','$mmobile','$user','$pass','$approved')";

    // Code for Registration SMS

     $url = 'http://sms.hspsms.com/sendSMS?username=#USERNAME#&message=Dear User,You Have Succeffully Registered to ABC.com ,Thanks&sendername=HSPSMS&smstype=PROMO&numbers=9503808004&apikey=#APIKey#';

    header("Location:$url");
?>

Please help me on same or guide where I am doing right or wrong?

Any help will be appreciable.

random
  • 7,756
  • 3
  • 19
  • 25
  • 1
    It is unclear what you ask: if you forward the user to some service by means of a `Location` header, then you sent the user away. There is no way to somehow get the user back to you. This approach is not the use of an API, this is called "forwarding". What you _could_ try to do is to forward to that service inside some iframe. That would allow to use javascript to survey that iframe and react on that. But that sounds really ugly. Instead look for a _real_ API that allows you to hand over the requests from the server side, so from within your php script and react to the result. – arkascha Sep 06 '15 at 07:46
  • This is *all wrong*. Ignoring the non-functional and insecure SQL, what your code is doing is instructing *the user's browser* to go to `sms.hspsms.com`. As arkascha said, this means *the user will leave your site*. More than that, it means the user can see this happening, save the URL, change the message details and they can send additional SMS that you would be charged for. What you want instead is to send the request to the gateway *from your server*, which can be done by using [cURL](http://stackoverflow.com/q/3062324/1233508). – DCoder Sep 06 '15 at 07:59
  • And, to state the obvious on top of all that, if your URL literally contains `#USERNAME#` and `#APIKey#`, you have to replace them with your account credentials... Judging from your text about "or not JSON format", it also sounds like it's expecting JSON instead of a plain query string, you need to read the gateway's documentation for that. – DCoder Sep 06 '15 at 08:01

1 Answers1

0

You're sending the user's browser to the API, what you want to do is have PHP make the request and then tell the user if it was successful or not.

You may be able to use get_file_contents, but curl will give you more control.

You should have a function that will send the request to the API and return a success/failure and then display a message to the user.

Something like this (untested):

if(isset($_POST['submit1'])) {
    $mname=$_POST['mname'];
    $cdate=$_POST['cdate'];
    $maddress1=$_POST['maddress'];
    $mschool=$_POST['mschool'];
    $mkendra=$_POST['centrename'];
    $mmobile=$_POST['mmobile'];
    $user=$_POST['user'];
    $pass=$_POST['pass'];
    $approved="0";

    $qr="insert into tempregistration values('',
        '$cdate','$mname','$maddress1','$mschool',
        '$mschool','$mkendra','$mmobile','$user','$pass','$approved')";
    //update to encode parameters which contain spaces
    $message = urlencode('Dear User, You have successfully registered ...');
    // other parametes might also contain spaces ...
    $username = urlencode('#USERNAME');
    $sendername = urlencode('HSPSMS');
    //etc

    $url = 'http://sms.hspsms.com/sendSMS?username='.$username
           .'&message='.$message
           .'&sendername='.$sendername
           .'&smstype=PROMO&numbers=9503808004&apikey=#APIKey#';


    if ($result = do_api_call($url)){
        echo "SMS API returned:<br>";
        echo $result;//for debugging
    }else {
        echo "Failure"
    }
}

...

function do_api_call($url){
    //this is very simplistic code to get you going ...
    //use curl instead for more control
    return file_get_contents($url); 
}
Loopo
  • 2,204
  • 2
  • 28
  • 45
  • hello, Thanks for reply, I have tried but fails to find out solution I have used file_get_contents() method but it giving exception failed to open stream and exception details is as,Warning: file_get_contents(http%3A%2F%2Fsms.hspsms.com%2FsendSMS%3Fusername%3D#UserName#%26message%3DYe+Makada%26sendername%3DHSPSMS%26smstype%3DPROMO%26numbers%3D9970077302%26apikey%3D#APIKEY#) [function.file-get-contents]: failed to open stream: No such file or directory in C:\wamp\www\Sangola Taluka Society\SendSMS.php on line 9 Failure – user3350069 Sep 06 '15 at 09:53
  • I have tried also without using urlencode but it gives following exception.I am not sure how to use Curl() Warning: file_get_contents(http://sms.hspsms.com/sendSMS?username=#Username#&message=Hello&sendername=HSPSMS&smstype=PROMO&numbers=950380004&apikey=#APIKey#) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 505 HTTP Version Not Supported in C:\wamp\www\Sangola Taluka Society\SendSMS.php on line 9 Failure – user3350069 Sep 06 '15 at 10:03
  • if you're going to encode the url, you should encode each parameter separately, otherwise your 'http://...' path will get encoded as well and then it won't look like an address anymore ... see my updated answer – Loopo Sep 07 '15 at 10:22