function.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /*
  3. File: functions.php
  4. */
  5. function media($content) //多媒体转换
  6. {
  7. if(strstr($content,'murl')){//音乐
  8. $a=array();
  9. foreach (explode('#',$content) as $content)
  10. {
  11. list($k,$v)=explode('|',$content);
  12. $a[$k]=$v;
  13. }
  14. $content = $a;
  15. }
  16. elseif(strstr($content,'pic'))//多图文回复
  17. {
  18. $a=array();
  19. $b=array();
  20. $c=array();
  21. $n=0;
  22. $contents = $content;
  23. foreach (explode('@t',$content) as $b[$n])
  24. {
  25. if(strstr($contents,'@t'))
  26. {
  27. $b[$n] = str_replace("itle","title",$b[$n]);
  28. $b[$n] = str_replace("ttitle","title",$b[$n]);
  29. }
  30. foreach (explode('#',$b[$n]) as $content)
  31. {
  32. list($k,$v)=explode('|',$content);
  33. $a[$k]=$v;
  34. $d.= $k;
  35. }
  36. $c[$n] = $a;
  37. $n++;
  38. }
  39. $content = $c ;
  40. }
  41. return $content;
  42. }
  43. function get_utf8_string($content) // 将一些字符转化成utf8格式
  44. {
  45. // 将一些字符转化成utf8格式
  46. $encoding = mb_detect_encoding($content, array('ASCII','UTF-8','GB2312','GBK','BIG5'));
  47. return mb_convert_encoding($content, 'utf-8', $encoding);
  48. }
  49. function xiaojo($key,$from,$to) //小九接口函数,该函数可通用于其他程序
  50. {
  51. global $yourdb,$yourpw;
  52. //取全局变量
  53. $yourdb = XIAOJO_DB ;
  54. $yourpw = XIAOJO_PW ;
  55. //转换编码格式
  56. $key=urlencode($key);
  57. $yourdb=urlencode($yourdb);
  58. $from=urlencode($from);
  59. $to=urlencode($to);
  60. //调用小九API接口得到回复结果
  61. $post= "chat=".$key."&db=".$yourdb."&pw=".$yourpw."&from=".$from."&to=".$to;
  62. $api = "http://www.xiaojo.com/api5.php";
  63. $replys = http_post($api,$post);
  64. $reply = media(urldecode( $replys));//多媒体转换
  65. return $reply;
  66. }
  67. //有道翻译
  68. function youdaoDic($word){
  69. $keyfrom = "zhuojin"; //申请APIKEY时所填表的网站名称的内容
  70. $apikey = "304804921"; //从有道申请的APIKEY
  71. /*/有道翻译-xml格式
  72. $url_youdao = 'http://fanyi.youdao.com/fanyiapi.do?keyfrom='.$keyfrom.'&key='.$apikey.'&type=data&doctype=xml&version=1.1&q='.$word;
  73. $xmlStyle = simplexml_load_file($url_youdao);
  74. $errorCode = $xmlStyle->errorCode;
  75. $paras = $xmlStyle->translation->paragraph;
  76. if($errorCode == 0){
  77. return $paras;
  78. }else{
  79. return "无法进行有效的翻译";
  80. }
  81. */
  82. //有道翻译-json格式
  83. $url_youdao = 'http://fanyi.youdao.com/fanyiapi.do?keyfrom='.$keyfrom.'&key='.$apikey.'&type=data&doctype=json&version=1.1&q='.$word;
  84. $jsonStyle = http_get($url_youdao);
  85. $result = json_decode($jsonStyle,true);
  86. $errorCode = $result['errorCode'];
  87. $trans = '';
  88. if(isset($errorCode)){
  89. switch ($errorCode){
  90. case 0:
  91. $trans = $result['translation']['0'];
  92. break;
  93. case 20:
  94. $trans = '要翻译的文本过长';
  95. break;
  96. case 30:
  97. $trans = '无法进行有效的翻译';
  98. break;
  99. case 40:
  100. $trans = '不支持的语言类型';
  101. break;
  102. case 50:
  103. $trans = '无效的key';
  104. break;
  105. default:
  106. $trans = '出现异常';
  107. break;
  108. }
  109. }
  110. return $trans;
  111. }
  112. //百度翻译
  113. function baiduDic($word,$from="auto",$to="auto"){
  114. //首先对要翻译的文字进行 urlencode 处理
  115. $word_code=urlencode($word);
  116. //注册的API Key
  117. $appid="O1IyaDAfnLPAIemNuG9kSdwq";
  118. //生成翻译API的URL GET地址
  119. $baidu_url = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=".$appid."&q=".$word_code."&from=".$from."&to=".$to;
  120. $text=json_decode(http_get($baidu_url));
  121. $text = $text->trans_result;
  122. return $text[0]->dst;
  123. }
  124. /**
  125. * GET 请求
  126. * @param string $url
  127. */
  128. function http_get($url){
  129. //初始化一个cURL对象
  130. $oCurl = curl_init();
  131. if(stripos($url,"https://")!==FALSE){
  132. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  133. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
  134. }
  135. //设置需要抓取的URL
  136. curl_setopt($oCurl, CURLOPT_URL, $url);
  137. //设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上
  138. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
  139. //在发起连接前等待的时间,如果设置为0,则无限等待。微信的一次连接最长为5秒
  140. $timeout = 5;
  141. curl_setopt ($oCurl, CURLOPT_CONNECTTIMEOUT, $timeout);
  142. //运行cURL,请求网页
  143. $sContent = curl_exec($oCurl);
  144. $aStatus = curl_getinfo($oCurl);
  145. //关闭URL请求
  146. curl_close($oCurl);
  147. if(intval($aStatus["http_code"])==200){
  148. return $sContent;
  149. }else{
  150. return false;
  151. }
  152. }
  153. /**
  154. * POST 请求
  155. * @param string $url
  156. * @param array $param
  157. * @return string content
  158. */
  159. function http_post($url,$param){
  160. //初始化curl
  161. $oCurl = curl_init();
  162. if(stripos($url,"https://")!==FALSE){
  163. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  164. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  165. }
  166. if (is_string($param)) {
  167. $strPOST = $param;
  168. } else {
  169. $aPOST = array();
  170. foreach($param as $key=>$val){
  171. $aPOST[] = $key."=".urlencode($val);
  172. }
  173. $strPOST = join("&", $aPOST);
  174. }
  175. //在发起连接前等待的时间,如果设置为0,则无限等待。微信的一次连接最长为5秒
  176. $timeout = 5;
  177. curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $timeout);
  178. curl_setopt($oCurl, CURLOPT_URL, $url); //抓取指定网页
  179. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 ); //要求结果为字符串且输出到屏幕上
  180. curl_setopt($oCurl, CURLOPT_POST,true); //post提交方式
  181. curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
  182. $sContent = curl_exec($oCurl); //运行curl
  183. $aStatus = curl_getinfo($oCurl);
  184. curl_close($oCurl);
  185. if(intval($aStatus["http_code"])==200){
  186. return $sContent;
  187. }else{
  188. return false;
  189. }
  190. }
  191. /**
  192. * 系统加密方法
  193. * @param string $data 要加密的字符串
  194. * @param string $key 加密密钥
  195. * @param int $expire 过期时间 单位 秒
  196. * @return string
  197. */
  198. function think_encrypt($data, $key = '', $expire = 0) {
  199. $key = md5(empty($key) ? C('DATA_AUTH_KEY') : $key);
  200. $data = base64_encode($data);
  201. $x = 0;
  202. $len = strlen($data);
  203. $l = strlen($key);
  204. $char = '';
  205. for ($i = 0; $i < $len; $i++) {
  206. if ($x == $l) $x = 0;
  207. $char .= substr($key, $x, 1);
  208. $x++;
  209. }
  210. $str = sprintf('%010d', $expire ? $expire + time():0);
  211. for ($i = 0; $i < $len; $i++) {
  212. $str .= chr(ord(substr($data, $i, 1)) + (ord(substr($char, $i, 1)))%256);
  213. }
  214. return str_replace(array('+','/','='),array('-','_',''),base64_encode($str));
  215. }
  216. ?>