php代码发送短信而无需在代码

时间:2017-04-07 10:13:48

标签: php html api

<?php
//Please Enter Your Details
$user="user_name"; //your username
$password="your_password"; //your password
$mobilenumbers="0123456789"; //enter Mobile numbers comma seperated
$message = "Demo sms"; //enter Your Message 
$senderid="DEMO"; //Your senderid
$messagetype="N"; //Type Of Your Message 
$url="http://sms.mavyah.com/WebserviceSMS.aspx";
//domain name: Domain name Replace With Your Domain  
$message = urlencode($message);
$ch = curl_init(); 
if (!$ch){die("Couldn't initialize a cURL handle");}
$ret = curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);          
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 

"User=$user&passwd=$password&mobilenumber=$mobilenumbers&message=$
message&sid=$s
enderid&mtype=$messagetype");
$ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);


//If you are behind proxy then please uncomment below line and provide your 
proxy ip with port.
// $ret = curl_setopt($ch, CURLOPT_PROXY, "PROXY IP ADDRESS:PORT");

$curlresponse = curl_exec($ch); // execute
if(curl_errno($ch))
echo 'curl error : '. curl_error($ch);

if (empty($ret)) {
// some kind of an error happened
die(curl_error($ch));
curl_close($ch); // close cURL handler
} else {
$info = curl_getinfo($ch);
curl_close($ch); // close cURL handler
//echo "<br>";
echo $curlresponse;    //echo "Message Sent Succesfully" ;

}
?>

我编写了用于发送短信的PHP代码。此代码使用SMs国家API。在该代码中,如上所示,我们必须包含$mobilenumbers,它告诉应该向谁发送此消息。但我需要更改如何将消息发送给收件人而不提及代码中的数字。即,当HTMl表单填充了接收者和消息时,这些字段应该作为代码中$mobilenumber$message的输入。我们不应在代码中手动输入数字和消息。

1 个答案:

答案 0 :(得分:0)

我假设您从某种形式获取值,如下所示:

<form name="form" action="" method="post">
  <input type="text" name="mobilenumbers" id="mobilenumbers" value="Mobile number">
  <input type="text" name="message" id="message" value="Message">
</form>

然后你只需要从表单中获取值到PHP代码中:

function sendSms($mobilenumbers, $message) {

  // Validation
  if ( ! is_numeric($mobilenumbers) ) {
    return;
  }
  $message = urlencode($message);

  $user="your_username"; //your username
  $password="your_password"; //your password
  $senderid="DEMO"; //Your senderid
  $messagetype="N"; //Type Of Your Message
  $url="http://sms.mavyah.com/WebserviceSMS.aspx";
  //domain name: Domain name Replace With Your Domain
  $ch = curl_init();
  if (!$ch){die("Couldn't initialize a cURL handle");}
  $ret = curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt ($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  curl_setopt ($ch, CURLOPT_POSTFIELDS,

  "User=$user&passwd=$password&mobilenumber=$mobilenumbers&message=$
  message&sid=$s
  enderid&mtype=$messagetype");
  $ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  // If you are behind proxy then please uncomment below line and provide your
  // proxy ip with port.
  // $ret = curl_setopt($ch, CURLOPT_PROXY, "PROXY IP ADDRESS:PORT");

  $curlresponse = curl_exec($ch); // execute
  if(curl_errno($ch))
  echo 'curl error : '. curl_error($ch);

  if (empty($ret)) {
  // some kind of an error happened
  die(curl_error($ch));
  curl_close($ch); // close cURL handler
  } else {
  $info = curl_getinfo($ch);
  curl_close($ch); // close cURL handler
  //echo "<br>";
  echo $curlresponse;    //echo "Message Sent Succesfully" ;

  }
}

// Call the function with the values provided by the form
sendSms($_POST('mobilenumbers'), $_POST('message'));

包装永远不应该在函数中更改的代码,并使用表单中的值调用该函数。您应该验证所有用户数据,我已经添加了一个简单的电话号码检查,如果值不是数字,那么该功能将无声地失败。