简体   繁体   中英

How to send SMS using PHP CURL function

I have an account in a website by which I can send sms to mobile phones. In order to do that at first I need to log in using my id and password and then a page shows up where I put the recipient's mobile number and then my message and finally hit the button to send the message.

Now one of my friends told me that I can send sms from my own application through this website using PHP Curl function. I didn't have any prior idea about CURL function so I googled it but I couldn't figure out how to do this. I have checked the HTML code of the login page and the page from where I can send the sms of that website and I am posting it below.

Would you please kindly show me how to send sms using CURL function or any other way.. through this website..

Thanks in Advance :)

Form1

 <form name="form" action="/websms/index.php" method="POST">
 <input type="hidden" name="HTMLForm_formname" value="form">


  <table align="center" size="300" border="0" class="list">
  <tr class="r1">
    <th colspan="3" class="left">
        <label id="label_login_title" for="login_title" class="HTMLForm-label">User  Login</label>

       </th>
</tr>
  <tr>
    <td align="right" valign="top">
        <label id="label_mobile_no" for="mobile_no" class="HTMLForm-label">Mobile Number</label>
    </td>

     <td>
        <input type="text" id="mobile_no" name="mobile_no" size="20" maxlength="11" value="" onkeypress="return checkNumberOnly(event)" class="HTMLForm-text"> 
    </td>


   </tr>
   <tr> 
      <td align="right" valign="top">
        <label id="label_password" for="password" class="HTMLForm-label">Password</label>
    </td>
    <td>
        <input type="password" id="password" name="password" value="" class="HTMLForm-password" size="20">
    </td>

    </tr>

       <tr>
         <td colspan="3" align="center">
            <input type="submit" id="submit" name="submit" value="Login"  class="button_all_action">
            <input type="hidden" id="submit_login" name="submit_login" value="1">
      </td>
     </tr>
  </table>
  </form>

Second Form

<form name="form" action="javascript:get(document.getElementById('form'));" method="POST">
<input type="hidden" name="HTMLForm_formname" value="form">


<table align="center" size="450" border="0" class="list">


<tr class="r2">
    <th class="left">
        <label id="label_send_to_no" for="send_to_no" class="HTMLForm-label">To</label>
    </th>
    <td class="left">
        <textarea id="send_to_no" name="send_to_no" class="HTMLForm-textarea" onkeypress="checkValidGPNumner(document.form.send_to_no)" onchange="checkValidGPNumner(document.form.send_to_no)" onkeyup="checkValidGPNumner(document.form.send_to_no)" wrap="soft" style="width:250px;height:50px"></textarea>  

    </td>    
  </tr>

 <tr class="r1">

    <th class="left">
        <label id="label_message" for="message" class="HTMLForm-label">Message</label>
    </th>
    <td class="left">
        <textarea id="message" name="message" class="HTMLForm-textarea" onkeypress="textCounter(document.form.message,document.form.counter_message,160)" onchange="textCounter(document.form.message,document.form.counter_message,160)" onkeyup="textCounter(document.form.message,document.form.counter_message,160)" wrap="soft" style="width:250px;height:130px"></textarea>

       <input type="text" id="counter_message" name="counter_message" size="5" value="" readonly="" class="HTMLForm-text"> <label id="label_char_remain" for="char_remain" class="HTMLForm-label">Character remained</label> 
    </td>

 </tr>

     <tr class="r2">
     <td colspan="2" class="center">
        <input type="submit" id="submit" name="submit" value="Send" class="button_all_action">
        <input type="hidden" id="mid" name="mid" value="1">
        <input type="hidden" id="submit_sms" name="submit_sms" value="1">
    </td>
    </tr>
     </table>
   </form>

Simple CURL Function to send sms:

    function CURLsendsms($number, $message_body){   

        $api_params = $api_element.'?apikey='.$apikey.'&sender='.$sender.'&to='.$mobileno.'&message='.$textmessage;  
        $smsGatewayUrl = "http://springedge.com";  
        $smsgatewaydata = $smsGatewayUrl.$api_params;
        $url = $smsgatewaydata;

        $ch = curl_init();                       // initialize CURL
        curl_setopt($ch, CURLOPT_POST, false);    // Set CURL Post Data
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $output = curl_exec($ch);
        curl_close($ch);                         // Close CURL

        // Use file get contents when CURL is not installed on server.
        if(!$output){
           $output =  file_get_contents($smsgatewaydata);  
        }

    }

You can indeed send SMS messages with cURL, but cURL is just one part of it. Using cURL, you'd make API calls to a provider like Twilio .

This is a solution using Twilio.

First of all you have to download the twilio library for PHP: https://github.com/twilio/twilio-php/downloads

Then copy the Services folder in your Server and take in mind the location.

Now you have to create a simple program like the one below, (this is an example an easy php sms sender):

<?php //lets say that the name of this php is smsSender.php

$contactname = $_POST['name'];
$contactphone = $_POST['mobile_no'];

$message = $_POST['message'];


require 'Services/Twilio.php';//<<<<<<<<<HERE! make sure the path is ok.

$AccountSid = "AXXXXXX"; // this numbers you can find it in your twilio dashboard
$AuthToken = "TXXXXXXXXX";// also this number .

$client = new Services_Twilio($AccountSid, $AuthToken);

$people = array(
//"4566789903" => "Curious George",
$contactphone => $contactname,
);

foreach ($people as $number => $name) {

$sms = $client->account->sms_messages->create("7035960031",$number,
$message);

echo "Sent message to $name";

}
?>

So you will need to change in your forms the action like this:

<form name="form" action="smsSender.php" method="POST">

Note: if you are using a trial account, you will be only able to send to verified numbers in your account, but if you register a phone number (1USD/month) then you can send to any number and without the sandbox message).

IMPORTANT: Curl library must be installed in the php server, if you are using your own server, lets say in UBUNTU, this command will install those libraries: sudo apt-get install curl libcurl3 libcurl3-dev php5-curl

After changing to this, the code worked great for me.

foreach ($people as $number => $name) {
$client->account->messages->sendMessage("+12055xxxxxx",$number,
$message);

I am not sure that you can send SMS's with CURL, however you can with PHP's mail function. I have never done so however.

Curl basically used for scrapping contents from another web site, it doesnt have any function to send sms. You have to use other way.

there re few tools by which you can connect to your sms (text Server) from there you can send sms. you can use curl to get your sms replies or etc later part if you need and eager to use that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM