我只想获取用户最后一篇推特帖子的文本。我发现了将用户提要抽象为 json 格式的代码,但我如何将 json var 抽象为 php vars?在"partucalar"中,"TEXT"变量如下所示。
这是我用来将推特提要转换为 json 的脚本: 链接到代码但是,这与这个问题无关。我只需要输出变量成为 php 变量。我已经投了反对票,所以我提供了更多细节。
下面是 json 输出:
{
"tweets": [
{
"url": "http://twitter.com/cosmocatalano/status/343768531101417474",
"text": "This is a test tweet. @ Sufferloft http://instagram.com/p/aWFnSJInU-/ ",
"html": "This is a test tweet. @ Sufferloft <a href='"http://t.co/XRaizXhwYz'" rel='"nofollow'" dir='"ltr'" data-expanded-url='"http://instagram.com/p/aWFnSJInU-/'" class='"twitter-timeline-link'" target='"_blank'" title='"http://instagram.com/p/aWFnSJInU-/'" ><span class='"invisible'">http://</span><span class='"js-display-url'">instagram.com/p/aWFnSJInU-/</span><span class='"invisible'"></span><span class='"tco-ellipsis'"><span class='"invisible'"> </span></span></a>",
"date": "1370795779",
"user": "/cosmocatalano",
"id": "14503633",
"img": "https://si0.twimg.com/profile_images/2225916199/image_normal.jpg",
"name": "Cosmo Catalano",
"rt": false,
"card": {
"href": "http://instagram.com/p/aWFnSJInU-/",
"data-url": "http://distilleryimage2.ak.instagram.com/9d54f23ed12211e29fe522000a1f97ce_5.jpg",
"data-resolved-url-large": "http://distilleryimage2.ak.instagram.com/9d54f23ed12211e29fe522000a1f97ce_7.jpg"
}
}
]
}
我想要推文文本:例变量
$Tweet = jsondecode(text);
echo $Tweet;
假设 JSON 字符串存储在 $jsonString
中。
$obj = json_decode($jsonString);
echo $obj->tweets[0]->text; // get the first tweet
或者,如果有多个推文,则循环它们:
$obj = json_decode($jsonString);
foreach($obj->tweets as $tweet)
{
echo $tweet->text;
}