在PHP中,如何使用JSON数据搜索和匹配特定字符串?

时间:2011-02-14 04:05:36

标签: php json pattern-matching

我有一堆字符串,我想知道它们是否存在于JSON响应中。

我使用了以下代码,但它无效。我不想遍历JSON数据。

 $url  = "https://graph.facebook.com/me/feed?access_token=".$session['access_token'];
 $page=file_get_contents($url);
 $json_a = json_decode($page,true);

 $pos = strpos($page, $mystring);
 if($pos == true)
 {
     do something
 }

$ page不包含带有JSON提要内容的字符串。如何将JSON转换为字符串,以便我可以检查$ mystring?

编辑:

这很奇怪。 当我使用网址https://graph.facebook.com/me/feed?access_token=“。$ session ['access_token']

我得到空数据,但是当我使用“https://graph.facebook.com/me/friends?access_token=”。$ session ['access_token'];

一切都很好,我得到了所有朋友的名单。我无法理解我哪里出错了?

这是链接http://developers.facebook.com/docs/reference/api/ 我使用了Profile feed(Wall)的确切url格式:https://graph.facebook.com/me/feed?access_token= ...

1 个答案:

答案 0 :(得分:7)

JSON IS 一个字符串,直到你通过解码函数运行它并将其转换为php结构。你应该可以做到

if (strpos($page, "your search string") !== FALSE) {
    echo "hey, it's in there"; 
}

如果你的$页面空白,那么你的file_get_contents调用就会失败。