请选择 进入手机版 | 继续访问电脑版

ylsunyuan技术论坛中心

 找回密码
 注册(请使用中文注册)
搜索
热搜: 活动 交友 discuz
查看: 981|回复: 0

自定义API接口不得不提的那些事——害死人不偿命的BOM头

[复制链接]

124

主题

127

帖子

619

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
619
发表于 2015-10-17 12:49:25 | 显示全部楼层 |阅读模式
前两天用自定义的网站API写了个“今日头条”的微信模块,上传微擎0.6程序,安装后发现能正常通过CURL获取到通过json_encode产生的JSON数据,但json_decode后返回的居然是NULL。
由于微擎程序的调试带来的各种不方便,调试了两天程序之后,才发现是返回的JSON字符串中包含了不可见的BOM头!所以直接导致了json_decode的解码失败。
解决方法:用$result = json_decode(trim($contents,chr(239).chr(187).chr(191)),true); 去除BOM头不可见字符


接口程序如下:
  1. <?php
  2. //*****网 微信top10头条信息接口
  3. //@author sun QQ 276572447
  4. //www.ylsunyuan.com
  5. //返回格式 JSON 包含BOM头不可见字符 json数组前必须用 $res = trim($res,chr(239).chr(187).chr(191));  //去除BOM头
  6. header("Content-type:text/html;charset=utf-8");
  7. require("../e/class/connect.php");
  8. require("../e/class/db_sql.php");
  9. require("../e/data/dbcache/class.php");
  10. $link=db_connect();
  11. $empire=new mysqlquery();
  12. $siteurl = "http://********.com";  


  13. $arr = array();
  14. $query = "select newstime,title,titlepic,titleurl,smalltext from vod_ecms_video where isurl='0' and firsttitle='1'  order by newstime desc limit 0,5";
  15. $sql = $empire->query($query);


  16. while($r = $empire->fetch($sql))
  17. {
  18.     $arr[] = $r;  //把结果集合放到数组里
  19. }
  20. mysql_free_result($sql);//释放字符集


  21. //格式化新数组
  22. $res = array();
  23. $i = 0;
  24. foreach ($arr as $k => $v) {
  25.     # code...

  26.     $res[$i]['title'] = $v['title'];
  27.     $res[$i]['description'] = $v['smalltext'];
  28.     $res[$i]['picurl'] = $siteurl.$v['titlepic'];
  29.     $res[$i]['newstime'] = $v['newstime'];
  30.     $res[$i]['url'] = $siteurl.$v['titleurl'];
  31.     $i++;

  32. }



  33. echo trim(json_encode($res));
复制代码


微信模块如下:
  1. <?php
  2. /**
  3. * 今日头条模块处理程序
  4. *
  5. * @author sun QQ 276572447
  6. * @url http://www.ylsunyuan.com
  7. */
  8. //header("Content-type:text/html;charset=utf-8");
  9. defined('IN_IA') or exit('Access Denied');

  10. class Sun_newsModuleProcessor extends WeModuleProcessor {
  11.         public function respond() {
  12.                 $content = $this->message['content'];
  13.                 //这里定义此模块进行消息处理时的具体过程, 请查看微擎文档来编写你的代码

  14.                 $url = "http://**********/top10.php";        //返回格式 JSON 包含BOM头不可见字符 json数组前必须用 $res = trim($res,chr(239).chr(187).chr(191));  //去除BOM头

  15.                 load()->func('communication');
  16.                 $res = ihttp_get($url);
  17.                 $res = $res['content'];

  18. //$res = $this->trimall($res);

  19.                 $res = trim($res,chr(239).chr(187).chr(191));  //去除BOM头
  20.                
  21.                 $news = json_decode($res,ture);
  22.                

  23.                 return $this->respNews($news);

  24.         }


  25.         public function trimall($str)//删除空格  
  26.         {  
  27.             $qian=array(" "," ","\t","\n","\r");  
  28.                  $hou=array("","","","","");  
  29.             return str_replace($qian,$hou,$str);      
  30.         }
  31. }
复制代码


内容原创,转载请标明出外!
灵感信息来源:http://www.phpddt.com/php/json_decode-bom.html
文章出外:bbs.ylsunyuan.com  官方网站:www.ylsunyuan.com
回复

使用道具 举报

本版积分规则

QQ|Archiver|手机版|小黑屋|ylsunyuan技术论坛 ( 桂ICP备14005218号-1

GMT+8, 2024-4-19 06:36 , Processed in 0.070511 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表