繁体   English   中英

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

[英]How to send SMS using PHP CURL function

我在一个网站上拥有一个帐户,可以通过该帐户向手机发送短信。 为了做到这一点,我首先需要使用我的ID和密码登录,然后显示一个页面,在该页面上我输入收件人的手机号码,然后输入我的信息,最后按一下按钮以发送信息。

现在,我的一位朋友告诉我,我可以使用PHP Curl函数通过该网站从自己的应用程序发送短信。 我没有关于CURL函数的任何先验想法,所以我用它搜索了一下,但是我不知道该怎么做。 我已经检查了登录页面的HTML代码以及可以从该页面发送该网站的短信的页面,并将其张贴在下面。

请您告诉我如何使用CURL功能或通过其他方式通过该网站发送短信。

提前致谢 :)

表格1

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

注意:如果您使用的是试用帐户,则只能发送到帐户中经过验证的号码,但是如果您注册了电话号码(每月1美元),则可以发送到任何号码,而不会出现沙盒消息)。

重要说明: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发送SMS,但是可以使用PHP的邮件功能发送SMS。 我从来没有这样做过。

Curl基本上用于从另一个网站剪贴内容,它没有任何发送短信的功能。 您必须使用其他方式。

没有什么工具可以用来从那里连接到您的短信(文本服务器),您可以发送短信。 您可以使用curl获取您的短信回复,或者在以后需要时等着使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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