PHP Send SMS code sharing _php Tips

Source: Internet
Author: User
Tags curl explode http post php code trim

Method One (better, recommended)

PHP Send SMS Monxin Special (PHP code function)//This code is based on Monxin run//Source: Monxin./config/functions.php function SMS ($config, $language, $pdo , $sender, $phone _number, $content) {//demo var_dump (SMS (self:: $config, Self:: $language, $pdo, "system", "
  18074507509,15507455992 "," Test content, Time ". Date (" H:i:s ", Times ()));
  $sender =safe_str ($sender);
  $content =safe_str ($content);
  $arr =explode (', ', $config [' SMS '] [' disable_phrase ']);
  $disable =false;
  foreach ($arr as $v) {if (Strpos ($content, $v)!==false) {$phrase = $v; $disable =true;continue;} } if ($disable) {return $language [' Exist_disable_phrase '].
   
  ". $phrase;}
  $phone _number=explode (', ', $phone _number);
  $phone _number=array_unique ($phone _number);
  $addressee = ';
  $count = 0;
  foreach ($phone _number as $v) {if Preg_match ($config [' Other '] [' reg_phone '], $v)) {$addressee. = $v. ', ';}
  $addressee =trim ($addressee, ', ');
  $addressee =explode (",", $addressee);
  Var_dump ($addressee);
   
  $section =ceil (COUNT ($addressee)/$config [' SMS '] [' Max ']]; for ($i =0; $i < $sectIon $i + +) {$phone [$i]= '];
      for ($j = $i * $config [' SMS '] [' Max ']; $j < ($i + 1) * $config [' SMS '] [' Max ']; $j + +) {//echo $j. ', ';
    if (Isset ($addressee [$j])) {$phone [$i].= $addressee [$j]. $config [' SMS '] [' delimiter '];}
    $phone [$i]=trim ($phone [$i], $config [' SMS '] [' delimiter ']);
    $temp =explode ($config [' SMS '] [' delimiter '], $phone [$i]);
    $count =count ($temp);
    $length =ceil (strlen (preg_replace ('/[\x80-\xff]{3}/', ' x ', $content))/($config [' SMS '] [' length ']/2)];
    $count = $length * $COUNT;
    if (!isset ($timing)) {$timing = 0;}
      if ($phone [$i]!= ') {$time =time (); $sql = "INSERT into". $pdo->index_pre. " Phone_msg (' sender ', ' addressee ', ' content ', ' state ', ' time ', ' count ', ' timing ') VALUES (' $sender ', ' ". $phone [$i]."  
      $content. "', ' 1 ', ' $time ', ' $count ', ' 0 ')";
      if ($pdo->exec ($sql)) {return send_sms ($config, $pdo, $pdo->lastinsertid ());
      }else{return false;

 }
    }
  }
 
}

Example 2: Send SMS via file_get_contents function in PHP5 (HTTP get method)

PHP code

<?php   
$url = "http://sms.api.bz/fetion.php?username=13812345678&password=123456&sendto= 13512345678&message= SMS Content ";   
$result = file_get_contents ($url);   
echo $result; Return information default to UTF-8-encoded Chinese characters, if your page is encoded as gb2312, use the downlink statement to output the return information.   
//echo iconv ("UTF-8", "GBK", $result);   
? > 

Example 3: Send SMS via Curl in PHP (HTTP POST method)

PHP code

<?php $data ["username"] = 13812345678;   
$data ["password"] = "password123";   
$data ["sendto"] = 13512345678; $data ["message"] = "This is a test SMS!"   
   
";   
$curl = new Curl_class ();   
$result = @ $curl->post ("http://sms.api.bz/fetion.php", $data); echo $result;   
Return information default to UTF-8-encoded Chinese characters, if your page is encoded as gb2312, use the downlink statement to output the return information.   
   
Echo iconv ("UTF-8", "GBK", $result);   
Curl class Curl_class {function Curl_class () {return true;    
function Execute ($method, $url, $fields = ', $userAgent = ', $httpHeaders = ', $username = ', $password = ')   
{$ch = Curl_class::create ();   
if (false = = $ch) {return false;   
} if (Is_string ($url) && strlen ($url)) {$ret = curl_setopt ($ch, Curlopt_url, $url);   
else {return false;   
//whether to display header information curl_setopt ($ch, Curlopt_header, false);   
   
curl_setopt ($ch, Curlopt_returntransfer, true); if ($username!= ') {curl_setopt ($ch, Curlopt_userpwd, $username . ':' .   
$password);   
} $method = Strtolower ($method);   
if (' post ' = = $method) {curl_setopt ($ch, Curlopt_post, true);   
if (Is_array ($fields)) {$sets = array (); foreach ($fields as $key => $val) {$sets [] = $key. '=' .   
UrlEncode ($val);   
} $fields = Implode (' & ', $sets);   
curl_setopt ($ch, Curlopt_postfields, $fields);   
else if (' put ' = $method) {curl_setopt ($ch, Curlopt_put, true);   
}//curl_setopt ($ch, curlopt_progress, true);   
curl_setopt ($ch, Curlopt_verbose, true);   
curl_setopt ($ch, Curlopt_mute, false); curl_setopt ($ch, Curlopt_timeout, 10)//Set Curl timeout number of seconds if (strlen ($userAgent)) {curl_setopt ($ch, Curlopt_userage   
NT, $userAgent);   
} if (Is_array ($httpHeaders)) {curl_setopt ($ch, Curlopt_httpheader, $httpHeaders);   
   
} $ret = Curl_exec ($ch);   
if (Curl_errno ($ch)) {curl_close ($ch);   
Return Array (Curl_error ($ch), Curl_errno ($ch));   
} else {Curl_close ($ch);   
if (!is_string ($ret) | | |!strlen ($ret)) {return false;   
return $ret; } function post ($url, $fields, $userAgent = ', $httpHeaders = ', $username = ', $password = ') {$re   
t = Curl_class::execute (' POST ', $url, $fields, $userAgent, $httpHeaders, $username, $password);   
if (false = = $ret) {return false;   
} if (Is_array ($ret)) {return false;   
return $ret; function Get ($url, $userAgent = ', $httpHeaders = ', $username = ', $password = ') {$ret = Curl_class:   
: Execute (' Get ', $url, ', $userAgent, $httpHeaders, $username, $password);   
if (false = = $ret) {return false;   
} if (Is_array ($ret)) {return false;   
return $ret;   
function Create () {$ch = null;   
if (!function_exists (' Curl_init ')) {return false;   
} $ch = Curl_init ();   
if (!is_resource ($ch)) {return false;   
return $ch;    }   
   
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.