Home > Backend Development > PHP Tutorial > 采集微信平台实时消息json问题

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

WBOY
Release: 2016-06-23 14:15:55
Original
883 people have browsed it

微信 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
Copy after login
Copy after login

人肉支持支持

微信平台采集回来的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
Copy after login
Copy after login

由于涉及到隐私,不好向你索取隐去的数据。自然就不能运行你的代码
但你总可以贴出 $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"/>
Copy after login

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

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

没有看到这个标记在哪里

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

支持,虽然看不懂!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template