采集微信平台实时消息json问题

WBOY
發布: 2016-06-23 14:15:55
原創
869 人瀏覽過

微信 JSON

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

回复讨论(解决方案)

贴出代码看看

$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":"哈哈哈","remarkName":"","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
登入後複製
登入後複製

人肉支持支持

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

$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":"哈哈哈","remarkName":"","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
登入後複製
登入後複製

由于涉及到隐私,不好向你索取隐去的数据。自然就不能运行你的代码
但你总可以贴出 $json 的值吧

print_r(json_decode($str)); //如果直接转浏览器上的json数据可以显示
你在浏览器“查看网页源代码”中得到的也可以转码?
双引号被转义了吧?

由于涉及到隐私,不好向你索取隐去的数据。自然就不能运行你的代码
但你总可以贴出 $json 的值吧

print_r(json_decode($str)); //如果直接转浏览器上的json数据可以显示
你在浏览器“查看网页源代码”中得到的也可以转码?
双引号被转义了吧?
浏览器打印出来的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, '<').replace(/>/g,'>'));  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"/>
登入後複製

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

你贴出的不是 html 代码吗?
你说的 json 数据在哪里?由 js 动态读取的?
源码里多了这些东西,我发现我正则匹配错了。如果我要匹配<script></script>中的内容应该怎么写?

没有看到这个标记在哪里

preg_match("/<script>(.+?)<\/script>/s",$contents,$json); </script>

支持,虽然看不懂!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板