Udo
Udo

Reputation: 1157

PHP function to implement sending SMS

I am trying to implement sending SMS for this project I am working on using PHP. Note: I don't mean sending free SMS with the carrier and other things, I actually contacted an SMS company that provided a link as such www.smssender.com?username=myusername&pass=mypass&message=mymessage&recipient=phonenumber.

What function in PHP can be used to send such a request to the server API, and also get a response? Here is what I want (pseudocode):

function Sendsms(){
  add details to sting
  send url to sms server with the parameters
  get response and display
}

Upvotes: 0

Views: 311

Answers (3)

Ripa Saha
Ripa Saha

Reputation: 2540

function Sendsms()
{
   //add details to sting
     $url="www.smssender.comusername=myusername&pass=mypass&message=
      mymessage&recipient=phonenumber";

   //send url to sms server with the parameters
   $rsp = file_get_contents($url);

   //get response and display
   if( $res )
   {
        echo "sms successfully send";
   }
}

Upvotes: 1

Infinity
Infinity

Reputation: 3875

Look like you are doing a GET request. Have you looked into php http_get function?

<?php
$response = http_get("http://www.example.com/", array("timeout"=>1), $info);
print_r($info);
?>

source: https://www.php.net/manual/en/function.http-get.php

Upvotes: 2

Tucker
Tucker

Reputation: 7362

you want to do something like the following (this is an example for a POST request) i am using PHP's curl http://www.php.net/manual/en/function.curl-init.php :

$url = 'http://domain.com/get-post.php';
$fields = array(
            'lname' => urlencode($last_name),
            'fname' => urlencode($first_name),
            'title' => urlencode($title),
            'company' => urlencode($institution),
            'age' => urlencode($age),
            'email' => urlencode($email),
            'phone' => urlencode($phone)
        );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

The response to the request is in the variable $result

Upvotes: 2

Related Questions