如何使用PHP CURL函数发送短信


How to send SMS using PHP CURL function

我在一个网站上有一个帐户,可以通过该帐户向手机发送短信。为了做到这一点,一开始我需要使用我的id和密码登录,然后会出现一个页面,我把收件人的手机号码放在那里,然后是我的消息,最后点击按钮发送消息。

现在我的一个朋友告诉我,我可以通过这个网站使用PHP Curl函数从自己的应用程序发送短信。我之前对CURL函数没有任何想法,所以我在谷歌上搜索了它,但我不知道如何做到这一点。我已经检查了登录页面的HTML代码和我可以发送该网站短信的页面,我将其发布在下面。

你能告诉我如何使用CURL功能或任何其他方式发送短信吗。。通过这个网站。。

提前感谢:)

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>

第二种形式

<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>

发送短信的简单CURL函数:

    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);  
        }
    }

您确实可以使用cURL发送SMS消息,但cURL只是其中的一部分。使用cURL,您可以对Twilio这样的提供商进行API调用。

这是一个使用Twilio的解决方案。

首先,您必须下载适用于PHP的twilio库:https://github.com/twilio/twilio-php/downloads

然后复制服务器中的"服务"文件夹,并记下位置。

现在您必须创建一个简单的程序,如下所示(这是一个简单php sms发送器的示例):

<?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";
}
?>

所以你需要在你的表格中改变这样的动作:

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

注意:如果您使用的是试用帐户,您将只能发送到帐户中已验证的号码,但如果您注册了电话号码(1USD/月),则可以发送到任何号码,并且不需要沙盒消息)。

重要提示:Curl库必须安装在php服务器中,如果您使用自己的服务器,比如在UBUNTU中,此命令将安装这些库:sudo apt-get-install curl libcurl3 libcurl3-dev php5 curl

更改后,代码对我来说效果很好。

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

我不确定你是否可以用CURL发送短信,但你可以用PHP的邮件功能。然而,我从来没有这样做过。

Curl基本上是用来从另一个网站上删除内容的,它没有任何发送短信的功能。你必须使用其他方式。

很少有工具可以让你连接到你的短信(文本服务器),从那里你可以发送短信。如果你需要并渴望使用,你可以使用curl来获得你的短信回复等。