基于数值的PHP卷曲和循环
问题描述:
即时通讯使用Twitter API收集我已经收藏的推文数量,以及准确计算出收藏推文的总页数。基于数值的PHP卷曲和循环
我用这个网址:http://api.twitter.com/1/users/show/username.xml
我抢了XML元素 'favorites_count'
对于这个例子让我们假设favorites_count = 5
Twitter的API使用这个网址来获得favorties :http://twitter.com/favorites.xml(必须经过身份验证)
您只能使用此URL获取最后20个优惠,但是您可以更改要包含的网址通过在收藏夹网址的末尾添加:?page=3
来创建“网页”选项。
http://twitter.com/favorites.xml?page=2
所以我需要做的是使用卷曲(我认为),收集喜欢的鸣叫,但使用的网址:
http://twitter.com/favorites.xml?page=1
http://twitter.com/favorites.xml?page=2
http://twitter.com/favorites.xml?page=3
http://twitter.com/favorites.xml?page=4
etc...
一些类型的循环来访问每个URL,并收集推文,然后输出cotents。
任何人都可以在这方面帮助: - 需要使用curl验证 - 收集的鸣叫(已脚本本)的页数 - 然后使用一个循环都要经过基于网页值的每个页面的网址?
答
是favorites_count收藏夹总数还是总页数?
$twitter = curl_init('http://api.twitter.com/1/users/show/username.xml');
curl_set_opt($twitter, CUROPT_RETURNTRANSFER, true);
$userInfo = curl_exec($twitter);
$userObj = new SimpleXmlElement($userInfo);
$nbFaves = $userObj->favorites_count; // (string) 5
$urlTpl = "http://http://twitter.com/favorites.xml?page=%s";
$favorites = array(); // this will be the data for output
for($i =0; $i < $nbFaves; $i++) {
$pageUrl = sprintf($urlTpl, $i+1); // notice the +1 to move from 0 indexed to 1 indexed
curl_set_opt($twitter, CURLOPT_URL, $pageUrl);
$faves = curl_exec($twitter);
$faves = new SimpleXmlElement($faves);
foreach($faves->favorite as $fave) {
$data = array();
/* here you assign the diferent child values/attributes
* from the favorite node to the $data array
*/
$favorites[] = $data; // push the data array into $favorites
}
unset($faves); // just some cleanup
}
// now you would loop through $favorites and output the data as html.
现在,如果favorites_count
是收藏夹的总数,而不是网页,那么你需要修改上面找出多少页有基于多少最喜欢的是一个页面上。但那很简单。
favorites_count返回收藏夹的数量,但我除以20并将数字向上舍去以获得总页数。 – CLiown 2010-03-30 19:33:53
试过它调用未定义的函数。 – CLiown 2010-03-30 19:41:27
在什么行/函数调用? – prodigitalson 2010-03-30 20:32:00