简体   繁体   English

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

[英]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. 为了做到这一点,我首先需要使用我的ID和密码登录,然后显示一个页面,在该页面上我输入收件人的手机号码,然后输入我的信息,最后按一下按钮以发送信息。

Now one of my friends told me that I can send sms from my own application through this website using PHP Curl function. 现在,我的一位朋友告诉我,我可以使用PHP Curl函数通过该网站从自己的应用程序发送短信。 I didn't have any prior idea about CURL function so I googled it but I couldn't figure out how to do this. 我没有关于CURL函数的任何先验想法,所以我用它搜索了一下,但是我不知道该怎么做。 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. 我已经检查了登录页面的HTML代码以及可以从该页面发送该网站的短信的页面,并将其张贴在下面。

Would you please kindly show me how to send sms using CURL function or any other way.. through this website.. 请您告诉我如何使用CURL功能或通过其他方式通过该网站发送短信。

Thanks in Advance :) 提前致谢 :)

Form1 表格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>

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: 简单的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);  
        }

    }

You can indeed send SMS messages with cURL, but cURL is just one part of it. 您确实可以使用cURL发送SMS消息,但是cURL只是其中的一部分。 Using cURL, you'd make API calls to a provider like Twilio . 使用cURL,您可以对诸如Twilio之类的提供程序进行API调用。

This is a solution using Twilio. 这是使用Twilio的解决方案。

First of all you have to download the twilio library for PHP: https://github.com/twilio/twilio-php/downloads 首先,您必须下载用于PHP的twilio库: 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 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";

}
?>

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

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 重要说明:Curl库必须安装在php服务器中,如果您使用自己的服务器,可以在UBUNTU中说,此命令将安装这些库: 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. 我不确定是否可以使用CURL发送SMS,但是可以使用PHP的邮件功能发送SMS。 I have never done so however. 我从来没有这样做过。

Curl basically used for scrapping contents from another web site, it doesnt have any function to send sms. Curl基本上用于从另一个网站剪贴内容,它没有任何发送短信的功能。 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. 您可以使用curl获取您的短信回复,或者在以后需要时等着使用。

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

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