微信平台采集回来的json字符通过json_decode转换为null.如果直接把结果放到json_decode中是可以转换的。微信JSON

解决方案 »

  1.   

    $url="https://mp.weixin.qq.com/cgi-bin/getmessage?t=wxm-message&".$token."&lang=zh_CN&count=50&timeline=1&day=0";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
    $contents = curl_exec($ch);
    preg_match("/<script type=\"json\" id=\"json-msgList\">([\s+].*)/s",$contents,$json);
    curl_close($ch);
    $str = '[ {"id":"295","type":"3","fileId":"0","hasReply":"0","fakeId":"2121212121","nickName":"哈哈哈","reName":"","dateTime":"1374114620","icon":"","content":"","playLength":"5505","length":"3696","source":"","starred":"0","status":"4","subtype":"0","showType":"0","desc":"","title":"","appName":"","contentUrl":"","bcardNickName":"","bcardUserName":"","bcardFakeId":"0"} ]';
    print_r(json_decode($str)); //如果直接转浏览器上的json数据可以显示
    print_r(json_decode($json[1]));//直接用匹配到的数据则显示为null
      

  2.   

    $url="https://mp.weixin.qq.com/cgi-bin/getmessage?t=wxm-message&".$token."&lang=zh_CN&count=50&timeline=1&day=0";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
    $contents = curl_exec($ch);
    preg_match("/<script type=\"json\" id=\"json-msgList\">([\s+].*)/s",$contents,$json);
    curl_close($ch);
    $str = '[ {"id":"295","type":"3","fileId":"0","hasReply":"0","fakeId":"2121212121","nickName":"哈哈哈","reName":"","dateTime":"1374114620","icon":"","content":"","playLength":"5505","length":"3696","source":"","starred":"0","status":"4","subtype":"0","showType":"0","desc":"","title":"","appName":"","contentUrl":"","bcardNickName":"","bcardUserName":"","bcardFakeId":"0"} ]';
    print_r(json_decode($str)); //如果直接转浏览器上的json数据可以显示
    print_r(json_decode($json[1]));//直接用匹配到的数据则显示为null
      

  3.   

    由于涉及到隐私,不好向你索取隐去的数据。自然就不能运行你的代码
    但你总可以贴出 $json 的值吧print_r(json_decode($str)); //如果直接转浏览器上的json数据可以显示
    你在浏览器“查看网页源代码”中得到的也可以转码?
    双引号被转义了吧?
      

  4.   

    浏览器打印出来的json的值我已经贴出来了就是保存在$str变量里。查看源码发现多了好多没用的东西?<script type="text/javascript">
    window.WXM && (function(WXM, win){
      var DATA = WXM.DATA;
      
      DATA.title = "实时消息";
      
      DATA.catalogList = [
        {
                title : "全部消息",
          link : '/cgi-bin/getmessage?t=wxm-message&token=466153210&lang=zh_CN&count=50',
          subCatalogList : 0
        }  
        ,{
                  _id   : 'msgNavCur',
                title : "今天",      
          link : '/cgi-bin/getmessage?t=wxm-message&token=466153210&lang=zh_CN&count=50&timeline=1&day=0',
          subCatalogList : 'subCatalogList'
        }
        ,{
                title : "昨天",      
          link : '/cgi-bin/getmessage?t=wxm-message&token=466153210&lang=zh_CN&count=50&timeline=1&day=1',
          subCatalogList : 'subCatalogList'
        }
        ,{
                title : "前天",      
          link : '/cgi-bin/getmessage?t=wxm-message&token=466153210&lang=zh_CN&count=50&timeline=1&day=2',
          subCatalogList : 'subCatalogList'
        }
        ,{
                title : "5天内消息",      
          link : '/cgi-bin/getmessage?t=wxm-message&token=466153210&lang=zh_CN&count=50&timeline=1&day=3',
          subCatalogList : 'subCatalogList'
        }
        
        ,
        {
                title : "星标消息",
          link : '/cgi-bin/getmessage?t=wxm-message&token=466153210&lang=zh_CN&count=50&star=1',
          subCatalogList : ''
        }
        
          ];
      
      DATA.List = {};
      DATA.List.keyword = '';
      DATA.List.keyword && (DATA.List.keyword = DATA.List.keyword.replace(/</g, '&lt;').replace(/>/g,'&gt;'));
      DATA.List.count = '50'*1;
      DATA.List.day = '0';
      DATA.List.timeline = '1';
      DATA.List.star = '';
      DATA.List.totalMsgNum = '6' * 1;
      DATA.List.pageNav = {
        PageIdx : '0'*1,
        PageCount : '1'*1,
        passPage:'',
        nextPage:'',
        pageJump:false,
        baseurl : ''
      };    DATA.List.msgList = jQuery.parseJSON(jQuery('#json-msgList').html());
      DATA.lastMsgId = '300';
    })(WXM, window);
    </script><script type="text/javascript" src="https://res.wx.qq.com/mpres/zh_CN/htmledition/js/wxm-msg-op16aa22.js"></script>
    <script type="text/javascript" src="https://res.wx.qq.com/mpres/zh_CN/htmledition/js/wxm-swfobject15def8.js"></script>
    <script type="text/javascript" src="https://res.wx.qq.com/mpres/zh_CN/htmledition/js/wxm-media15def8.js"></script>
    <script type="text/javascript" src="https://res.wx.qq.com/mpres/zh_CN/htmledition/js/wxm-message15def8.js"></script><link rel="stylesheet" type="text/css" href="https://res.wx.qq.com/mpres/htmledition/style/wxm_media15def8.css"/>
      

  5.   

    你贴出的不是 html 代码吗?
    你说的 json 数据在哪里?由 js 动态读取的?
      

  6.   

    源码里多了这些东西,我发现我正则匹配错了。如果我要匹配<script type="json" id="json-msgList"></script>中的内容应该怎么写?
      

  7.   

    preg_match("/<script type=\"json\" id=\"json-msgList\">(.+?)<\/script>/s",$contents,$json);