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