当前位置:Gxlcms > PHP教程 > php短信接口的示例代码(入门)

php短信接口的示例代码(入门)

时间:2021-07-01 10:21:17 帮助过:17人阅读

本文分享一段php短信接口代码,很简单,供初学的朋友参考,如果网站中有用到短信接口这方面的功能,这个例子可以借鉴下。

php 短信接口文件:

$uid,     //用户账号
  'pwd'=>strtolower(md5($pwd)), //MD5位32密码
  'mobile'=>$mobile,    //号码
  'content'=>$content,   //内容
  'time'=>$time,  //定时发送
  'mid'=>$mid      //子扩展号
  );
 $re= postSMS($http,$data);   //POST方式提交
 if( trim($re) == '100' )
 {
  return "发送成功!";
 }
 else 
 {
  return "发送失败! 状态:".$re;
 }
}

function postSMS($url,$data='')
{
 $row = parse_url($url);
 $host = $row['host'];
 $port = $row['port'] ? $row['port']:80;
 $file = $row['path'];
 while (list($k,$v) = each($data)) 
 {
  $post .= rawurlencode($k)."=".rawurlencode($v)."&"; //转URL标准码
 }
 $post = substr( $post , 0 , -1 );
 $len = strlen($post);
 $fp = @fsockopen( $host ,$port, $errno, $errstr, 10);
 if (!$fp) {
  return "$errstr ($errno)\n";
 } else {
  $receive = '';
  $out = "POST $file HTTP/1.1\r\n";
  $out .= "Host: $host\r\n";
  $out .= "Content-type: application/x-www-form-urlencoded\r\n";
  $out .= "Connection: Close\r\n";
  $out .= "Content-Length: $len\r\n\r\n";
  $out .= $post;  
  fwrite($fp, $out);
  while (!feof($fp)) {
   $receive .= fgets($fp, 128);
  }
  fclose($fp);
  $receive = explode("\r\n\r\n",$receive);
  unset($receive[0]);
  return implode("",$receive);
 }
}
?>
猜你喜欢:php短信发送函数 php短信接口函数的实例代码

人气教程排行