diff --git a/functions.php b/functions.php index d3858ab..92f0984 100755 --- a/functions.php +++ b/functions.php @@ -327,9 +327,15 @@ function yimian__gugu($body){ function gugu__send($ak, $userID, $memobirdID, $body){ - $body = iconv("UTF-8","gbk//TRANSLIT",$body); - $url = "http://open.memobird.cn/home/printpaper"; - return curl__post($url, array("ak" => $ak, "userID" => $userID, "memobirdID" => $memobirdID, "printcontent" => "T:".base64_encode($body)."", "timestamp" => "".time()."")); + require 'memobird.php'; + $m = new memobird($ak); + return $m->printPaper( "T:".base64_encode(iconv("UTF-8","gbk//TRANSLIT",$body))."", $memobirdID, $userID); + + + + //$body = iconv("UTF-8","gbk//TRANSLIT",$body); + //$url = "http://open.memobird.cn/home/printpaper"; + //return curl__post($url, array("ak" => $ak, "userID" => $userID, "memobirdID" => $memobirdID, "printcontent" => "T:".base64_encode($body)."", "timestamp" => "".time()."")); } diff --git a/memobird.php b/memobird.php new file mode 100644 index 0000000..fcc2953 --- /dev/null +++ b/memobird.php @@ -0,0 +1,122 @@ + 'http://open.memobird.cn/home/setuserbind/', + 'printPaper' => 'http://open.memobird.cn/home/printpaper/', + 'getPrintStatus' => 'http://open.memobird.cn/home/getprintstatus/' + ); + + + function __construct($ak){ + $this->ak = $ak; + date_default_timezone_set('PRC'); + } + + public function getUserId($memobirdID,$useridentifying){ + $params=array( + 'ak'=> $this->ak, + 'timestamp'=>date('Y-m-d h:m:s',time()), + 'memobirdID'=>$memobirdID, + 'useridentifying'=>$useridentifying + ); + $paramsString = http_build_query($params); + return $this->curl($this->url['getUserId'],$paramsString); + } + + public function printPaper($printcontent,$memobirdID,$userID){ + $params=array( + 'ak'=> $this->ak, + 'timestamp'=>date('Y-m-d h:m:s',time()), + 'printcontent'=>$printcontent, + 'memobirdID'=>$memobirdID, + 'userID'=>$userID + ); + $paramsString = http_build_query($params); + return $this->curl($this->url['printPaper'],$paramsString); + } + + + //构造printPaper方法中$printcontent格式,多个可以循环并用|拼接 + public function contentSet($type,$content){ + switch($type){ + case 'T': + $ret = $type.':'.base64_encode($this->charsetToGBK($content)."\n");break; + case 'P': + $ret = 'P:'.base64_encode($content); + default: + } + return $ret; + } + + public function getPaperStatus($printcontentID){ + $params=array( + 'ak'=> $this->ak, + 'timestamp'=>date('Y-m-d h:m:s',time()), + 'printcontentID'=>$printcontentID + ); + $paramsString = http_build_query($params); + return $this->curl($this->url['getPrintStatus'],$paramsString); + } + + + /** + * 创建http header参数 + * @param array $data + * @return bool + */ + private function createHttpHeader() { + // + } + /** + * 发起 server 请求 + * @param $action + * @param $params + * @param $httpHeader + * @return mixed + */ + public function curl($action,$params) { + //$action = self::SERVERAPIURL.$action.'.'.$this->format; + //$httpHeader = $this->createHttpHeader(); + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $action); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $params); + //curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); //处理http证书问题 + curl_setopt($ch, CURLOPT_HEADER, false); + curl_setopt($ch, CURLOPT_TIMEOUT, 5); + curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $ret = curl_exec($ch); + if (false === $ret) { + $ret = curl_errno($ch); + } + curl_close($ch); + return $ret; + } + + public function charsetToGBK($mixed){ + if (is_array($mixed)) { + foreach ($mixed as $k => $v) { + if (is_array($v)) { + $mixed[$k] = charsetToGBK($v); + } else { + $encode = mb_detect_encoding($v, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5')); + if ($encode == 'UTF-8') { + $mixed[$k] = iconv('UTF-8', 'GBK', $v); + } + } + } + } else { + $encode = mb_detect_encoding($mixed, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5')); + //var_dump($encode); + if ($encode == 'UTF-8') { + $mixed = iconv('UTF-8', 'GBK', $mixed); + } + } + return $mixed; + } + + +}