php短信接口程序

$loginname = ; // 接口帐号$pass = ; // 接口密码$code = ;$subid =
;$sender = ;$receivers = ; // 接收号码,多个号码间用 ; 分隔$settimer = ;
// 定时发送时间 格式:YYYY-MM-DD HH:MM$message = urlencode();$cmd =
send;$allowforward = ;$recordid_client = ; // 短信的客户端记录号$http =
_sms_url.ajax;$file =
$$loginname.pass=.$pass.code=.$code.subid=.$subid.sender=.$sender.receivers=.$receivers.settimer=.$settimer.message=.$message.cmd=.$cmd.allowforward=.$allowforward.recordid_client=.$recordid_client;$xml
= new XMLReader();$xml-open($file);$xml-setParserProperty(2,true);while
($xml-read()) {switch ($xml-name) {case Success: $xml-read();$Success =
$xml-value;$xml-read();break;case Fail: $xml-read();$Fail =
$xml-value;$xml-read();break;case RetCode: $xml-read();$RetCode =
$xml-value;$xml-read();break;case SmsRemain: $xml-read();$SmsRemain =
$xml-value;$xml-read();break;case ErrPhones: $xml-read();$ErrPhones =
$xml-value;$xml-read();break;}}$xml-close();// $Success 发送成功条数//
$Fail 发送失败条数// $RetCode 发送状态值// $SmsRemain 接口帐号余额//
$ErrPhones 发送失败号码

我们只需要在极光网站上申请,然后就appkey记录下来,写在代码里我这里是在thinkphp中测试的

说明:
uploadx
php批量上传组件遵循开源协议(GPL),任何个人、组织可自由对本程序进行使用、二次开发等权力。
由此也将声明本人不对您个人、组织使用本程序所带来的商业利益及损失有干涉及负责,但请保留版权信息。
也欢迎对uploadx提出保贵的建议及意见,不胜感激。
本程序使用PHP程序编写,能更高效的批量处理PHP开发中的文件上传,图片处理、批量添加图片水印等问题,在使用本程序前请详细阅读使用说明!

 代码如下

HTML表单页

<?php
class ApipostAction extends Action{
/**
* 模拟post进行url请求
* @param string $url
* @param string $param
*/
private $_appkeys = ‘**********************’;
private $_masterSecret =
‘**********************’;

 代码如下

function request_post($url =
”, $param = ”) {
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$ch = curl_init();//初始化curl
curl_setopt($ch,
CURLOPT_URL,$postUrl);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER,
1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//运行curl
curl_close($ch);
return $data;
}
/**
* 发送
* @param int $sendno 发送编号。由开发者自己维护,标识一次发送请求
* @param int $receiver_type 接收者类型。1、指定的 IMEI。此时必须指定
appKeys。2、指定的 tag。3、指定的 alias。4、 对指定 appkey
的所有用户推送消息。* @param string $receiver_value 发送范围值,与
receiver_type相对应。 1、IMEI只支持一个 2、tag 支持多个,使用 “,”
间隔。 3、alias 支持多个,使用 “,” 间隔。 4、不需要填
* @param int $msg_type 发送消息的类型:1、通知 2、自定义消息
* @param string $msg_content 发送消息的内容。 与 msg_type
相对应的值
* @param string $platform 目标用户终端手机的平台类型,如: android, ios
多个请使用逗号分隔
*/
function send($sendno = 0,$receiver_type = 1, $receiver_value = ”,
$msg_type = 1, $msg_content = ”, $platform = ‘android’) {
$url = ‘;
$param = ”;
$param .= ‘&sendno=’.$sendno;
$appkeys = $this->_appkeys;
$param .= ‘&app_key=’.$appkeys;
$param .= ‘&receiver_type=’.$receiver_type;
$param .= ‘&receiver_value=’.$receiver_value;
$masterSecret = $this->_masterSecret;
$verification_code =
md5($sendno.$receiver_type.$receiver_value.$masterSecret);
$param .= ‘&verification_code=’.$verification_code;
$param .= ‘&msg_type=’.$msg_type;
$param .= ‘&msg_content=’.$msg_content;
$param .= ‘&platform=’.$platform;
$res = $this->request_post($url, $param);

<form enctype=”multipart/form-data” action=”uploadx.php”
method=”post”>  
    <input name=”uploadx[]” type=”file”>  
    <input name=”uploadx[]” type=”file”>  
    <input type=”submit” value=”上传”>  
</form>  

$res_arr = json_decode($res, true);
if (intval($res_arr[‘errcode’])!=0){
return false;
}else{
return true;
}
/*if ($res === false) {
return false;
}
$res_arr = json_decode($res, true);
$res_arr[‘errmsg’]= “没有错误信息”;
switch (intval($res_arr[‘errcode’])) {
case 0:$res_arr[‘errmsg’] = ‘发送成功’;   break;
case 10:$res_arr[‘errmsg’] = ‘系统内部错误’;break;
case 1001:$res_arr[‘errmsg’] = ‘只支持 HTTP Post 方法,不支持 Get
方法’;break;
case 1002:$res_arr[‘errmsg’] = ‘缺少了必须的参数’;break;
case 1003:$res_arr[‘errmsg’] = ‘参数值不合法’;break;
case 1004:$res_arr[‘errmsg’] = ‘验证失败’;break;
case 1005:$res_arr[‘errmsg’] = ‘消息体太大’;break;
case 1007:$res_arr[‘errmsg’] = ‘receiver_value 参数 非法’;break;
case 1008:$res_arr[‘errmsg’] = ‘appkey参数非法’;break;
case 1010:$res_arr[‘errmsg’] = ‘msg_content 不合法’;break;
case 1011:$res_arr[‘errmsg’] = ‘没有满足条件的推送目标’;break;
case 1012:$res_arr[‘errmsg’] = ‘iOS 不支持推送自定义消息。只有
Android 支持推送自定义消息’;break;
default:break;
}
$msg_content = json_decode($msg_content,true);
if (intval($res_arr[‘errcode’])==0){
$str=
“<li>第”.$res_arr[‘sendno’].”条发送”.$res_arr[‘errmsg’].”!</li>”;
}else{
$str=
“<li>第”.$res_arr[‘sendno’].”条发送失败:”.$res_arr[‘errmsg’].”</li>”;
}
print_r($str);die();*/
}
}
?>

uploadx.php处理页

调用
<?php
$receiver_value = 1;//接收者id和n_builder_id相同
$platform = ‘android,ios’;//在上面手机上接收
$msg_content = json_encode(array(‘n_builder_id’=>’1′,
‘n_title’=>’标题’,
‘n_content’=>’内容’,’n_extras’=>array(‘fromer’=>’发送者’,’fromer_name’=>’发送者名字’,’fromer_icon’=>’发送者头像’,’image’=>’发送图片链接’,’sound’=>’发送音乐链接’)));
$this->send($sendno,3,$receiver_value,1,$msg_content,$platform);
?>

require (‘./classes/uploadx.class.php’); 
 
$uploadx = new uploadx; 
 
$uploadx->save = ‘./temp’; 
$uploadx->name = ‘auto’; 
$uploadx->mini = ‘200,200,mini’; 
$uploadx->mark = ‘./images/logo.png,0,60’; 
print_r($uploadx->is()); 

参数说明:

$uploadx->save
上传文件的保存目录
$uploadx->name;
上传文件的命名方式
参数说明
auto=自动随机文件名,
null=原文件名(覆盖),
其他自定义如按时间:date(‘YmdHis’); 
$uploadx->mini;
生成缩略图参数
width = 生成缩略图宽度。
height = 生成缩略图高度。
mini = 生成缩略图文件后缀附加名
默认留空不生成缩略图; 
$uploadx->mark;
上传图片文件添加水印参数:
水印文件,水印位置,水印透明度

位置参数说明:
0 = 随机;
1 = 左上角;
2 = 顶部居中;
3 = 右上角;
4 = 左居中;
5 = 中部居中;
6 = 右居中;
7 = 左下角;
8 = 底部居中;
9 = 右下角;

$uploadx->is(true);
返回已上传文件的数组
is参数说明:
true = 返回所有上传文件
false = 过滤失败文件 
$uploadx->is(); 
返回数据数组格式说明:
name = 上传后已保存的文件名
mini = 生成缩略图的文件名
mark = 添加水印状态,1表示成功,否则失败
code = 错误代码:0表示上传成功
error = 错误信息:上传错误时提示的错误信息
其中生成缩略图和添加水印功能也可单独使用!

以上是对uploadx2.0 进行简单的介绍, 
也欢迎各位能在使用过程中进行改进补充

例子

 代码如下

<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″
/>
<title>uploadx demo</title>
</head>

<body>
<form enctype=”multipart/form-data” action=”uploadx.php”
method=”post”>
 <input name=”uploadx[]” type=”file”> <br />
 <input name=”uploadx[]” type=”file”> <br />
 <input name=”uploadx[]” type=”file”> <br />
 <input name=”uploadx[]” type=”file”> <br />
 <br />
 <input type=”submit” value=”上传所选文件”>
</form>
</body>
</html>

<?php

require (‘./classes/uploadx.class.php’);

$uploadx = new uploadx;

$uploadx->save = ‘./temp’;
$uploadx->name = ‘auto’;
$uploadx->mini = ‘200,200,mini’;
$uploadx->mark = ‘./images/logo.png,0,60’;
print_r($uploadx->mini(‘./temp/2.jpg’));

uploadx.class.php

<?php

class uploadx {
  var $form = ‘uploadx’;
  var $save = ‘./’;  
  var $size = ‘1024’;
  var $type =
‘gif,bmp,png,jpg,jpeg,swf,flv,mp3,wma,rar,zip,7z,doc,docx,ppt,pptx,xls,xlsx,txt,pdf’;
  var $name = null;
  var $mini = null;
  var $mark = null;
  var $version = ‘2.0’;

  public function is($type = true){
   foreach ($this->files()
as $key => $val) {
    $file = $mini = null;
    $file = $this->saves($val[‘name’], $val[‘type’],
$val[‘tmp_name’], $val[‘size’], $val[‘error’]);
    $file[‘code’] || $file[‘path’] =
rtrim($this->save,’/’).’/’.$file[‘name’];
    $file[‘code’] || $file[‘mini’] =
$this->mini($file[‘path’]);  
    $file[‘code’] || $file[‘mark’] =
$this->mark($file[‘path’]);    
    $file[‘code’] && $file[‘error’] =
$this->error($file[‘code’]);
    $type ? $files[] = $file :  ($file[‘code’] || $files[] =
$file);
   }
    return isset($files) ? $files : array();
  }
  private function files(){
   if(count($_FILES[$this->form])<1) return array();
   if(is_array($_FILES[$this->form][‘name’])){
    for($i=0; $i<count($_FILES[$this->form][‘name’]); $i++)
{
     if($_FILES[$this->form][‘error’][$i]==4) continue;
     $files[] = array(
      ‘name’=>$_FILES[$this->form][‘name’][$i],
      ‘type’=>$_FILES[$this->form][‘type’][$i],
      ‘tmp_name’=>$_FILES[$this->form][‘tmp_name’][$i],
      ‘error’=>$_FILES[$this->form][‘error’][$i],
      ‘size’=>$_FILES[$this->form][‘size’][$i]);
    } 
   }else{
     $files[] = $_FILES[$this->form];
   }
     return $files;
  }
  private function saves($name, $type, $temp, $size, $error){  
    if($error) return array(‘name’=>$name, ‘code’=>$error);
    $prefix = strtolower(pathinfo($name, PATHINFO_EXTENSION));
    if(!in_array($prefix,
explode(‘,’,
strtolower($this->type)))){
     return array(‘name’=>$name, ‘code’=>’-1′);
    }
    if($size/1024>$this->size){
     return array(‘name’=>$name, ‘code’=>’-2′);
    }
    if(!is_dir($this->save)){
     if(!mkdir($this->save, 0777, TRUE)){
     return array(‘name’=>$name, ‘code’=>’-3′);
     }     
    }
    $filename = $this->name ? ($this->name==’auto’ ? uniqid() :
$this->name) : trim(basename($name,$prefix),’.’);
    $savefile = trim($this->save,’/’).’/’. $filename.’.’.$prefix;
    if(!@move_uploaded_file($temp,$savefile)){
     return array(‘name’=>$name, ‘code’=>’-4′);
    }
     @chmod($savefile,0777); 
    return array(‘name’=>$filename.’.’.$prefix, ‘code’=>0);
  }
  public function mini($file = null){
   if(!$file || !$this->mini) return false;
   if(!is_file($file)) return $this->error(-5,$file);
   list($width, $height, $extends) = explode(‘,’, $this->mini);
   $types = array(‘gif’,’png’,’jpg’,’jpeg’);
   $type = pathinfo($file, PATHINFO_EXTENSION);
   if(!in_array($type, $types)) return $this->error(-6);
   if(!is_file($file)) return $this->error(-5,$file);
   $mini = $extends ? basename($file, $type).$extends.’.’.$type :
trim(basename($file),’.’);
   $image = imagecreatefromstring(file_get_contents($file));
   $imagex = imagesx($image);
   $imagey = imagesy($image);
   $scale = $width / $imagex;
   if($width>$imagex){
    $mini_width = $imagex;
    $mini_height = $imagey;
   }else{
    $mini_width = $width;
    $mini_height = round($scale * $imagey);
   }
   if(function_exists(‘imagecreatetruecolor’) &&
function_exists(‘imagecopyresampled’)){
    $temp = imagecreatetruecolor($mini_width, $mini_height);
    imagecopyresampled($temp,$image,0,0,0,0,$mini_width, $mini_height,
$imagex, $imagey);
   }else{
    $temp = imagecreate($mini_width, $mini_height);
    imagecopyresized($temp,$image,0,0,0,0,$mini_width, $mini_height,
$imagex, $imagey);    
   }
    imagejpeg($temp, rtrim($this->save,’/’).’/’.$mini, 100);
    imagedestroy($temp);
    imagedestroy($image);

   return is_file(rtrim($this->save,’/’).’/’.$mini) ? $mini:
false;
  }
  public function mark($file = null){
     if(!$file || !$this->mark) return false;     
     list($watermark, $position, $opacity) = explode(‘,’,
$this->mark);
     if(!is_file($file) || !is_file($watermark)) return
$this->error(-5,’FILE=’.$file.’||’.’Watermark=’.$watermark);
   $type = pathinfo($file, PATHINFO_EXTENSION);
   $types = array(‘gif’,’png’,’jpg’,’jpeg’);
   if(!in_array($type, $types)) return $this->error(-6,$file);
         $opacity = min($opacity,100);
   $file_data = imagecreatefromstring(file_get_contents($file));
         $file_width = imagesx($file_data);
         $file_height = imagesy($file_data);
         if (in_array(pathinfo($watermark, PATHINFO_EXTENSION),
array(‘gif’,’png’))) {
          $mark_data =
imagecreatefromstring(file_get_contents($watermark));
          $mark_width = imagesx($mark_data);
          $mark_height =  imagesy($mark_data);
          switch($position){
              case 1: $x = 5; $y = 5; break;
              case 2: $x = ($file_width – $mark_width)/2; $y =
$mark_height; break;
              case 3: $x = ($file_width – $mark_width)-5; $y =
$mark_height; break;
              case 4: $x = 5; $y = ($file_height – $mark_height) / 2;
break;
              case 5: $x = ($file_width – $mark_width)/2; $y =
($file_height – $mark_height)/2; break;
              case 6: $x = ($file_width – $mark_width)-5; $y =
($file_height – $mark_height)/2; break;
              case 7: $x = 5; $y = ($file_height – $mark_height) – 5;
break;
              case 8: $x = ($file_width – $mark_width)/2; $y =
($file_height – $mark_height)-5; break;
              case 9: $x = ($file_width – $mark_width)-5; $y =
($file_height – $mark_height)-5; break;
              default: $x = rand(0,($file_width – $mark_width)); $y =
rand(0,($file_height – $mark_height));
          } 
     $temp = imagecreatetruecolor($mark_width, $mark_height); 
        imagecopy($temp, $file_data, 0, 0, $x, $y, $mark_width,
$mark_height); 
        imagecopy($temp, $mark_data, 0, 0, 0, 0, $mark_width,
$mark_height); 
        imagecopymerge($file_data, $temp, $x, $y, 0, 0, $mark_width,
$mark_height, $opacity);          
           imagejpeg($file_data, $file, 100);
           imagedestroy($temp);
           imagedestroy($file_data);
           imagedestroy($mark_data);
           return true;
         }else{
           return $this->error(-6,$watermark);
         } www.111cn.net
  }
  private function error($code = 0, $extends = ”){
   if($code){
   switch ($code) {    
    case 6:  $error = ‘写入临时文件夹失败’; break;
    case 5:  $error = ‘写入系统临时文件夹错误’; break;
    case 4:  $error = ‘没有文件被上传请检查表单’; break;
    case 3:  $error = ‘文件上传出错上传不完整’; break;
    case 2:  $error = ‘文件大小超出表单限制’; break;
    case 1:  $error = ‘文件大小超出系统限制’; break;
    case -1: $error = ‘上传文件类型不合法’; break;
    case -2: $error = ‘上传文件大小超出后台限制’; break;  
    case -3: $error = ‘创建文件保存路径失败’; break; 
    case -4: $error = ‘保存文件失败请检查路径’; break; 
    case -5: $error = ‘读取文件错误’; break; 
    case -6: $error = ‘不支持该操作’; break; 
    default: $error = ‘未知错误’;
   }
    return  ‘[‘.$code.’]:’.$error.$extends;
   }else{
    return false; 
   } 
  }

}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注