unable to grab the values of twitter and feedburner
$rssfeed = "https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=tutorialsbucket";
$twitter = "http://twitter.com/users/show.xml?screen_name=tutorialsbucket";
function followers($arg1, $arg2) {
$url = array($arg1, $arg2);
foreach ($url as $value)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $value);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
$fb = $xml->feed->entry['circulation'];
$tw = $xml->followers开发者_高级运维_count;
if (!empty($fb) || !empty($tw))
{
return $fb;
return $tw;
}
}
}
hi friends,
I am writing code to grab values of my twitter and Feedburner.but its not going good. i want to extract both values alone but i am unable to do this.
You can make followers() process one of the feeds and call the function twice, or you can return an array with both values.
$rssfeed = "https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=tutorialsbucket";
$twitter = "http://twitter.com/users/show.xml?screen_name=tutorialsbucket";
function followers($url, $twitter) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
if($twitter) {
return $xml->followers_count;
}else{
return $xml->feed->entry['circulation'];
}
}
$tw = followers($twitter, true);
$fb = followers($rssfeed, false);
or:
$rssfeed = "https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=tutorialsbucket";
$twitter = "http://twitter.com/users/show.xml?screen_name=tutorialsbucket";
function followers($arg1, $arg2) {
$url = array($arg1, $arg2);
$result = array();
foreach ($url as $value)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $value);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
$fb = $xml->feed->entry['circulation'];
$tw = $xml->followers_count;
if (!empty($fb))
{
$result[] = $fb;
}elseif(!empty($tw)){
$result[] = $tw;
}
}
return $result;
}
list($fb, $tw) = followers($rssfeed, $twitter);
I prefer the first one since it's simple and obvious what happens. You can make it even more general by using XPath to fetch the entries and letting the second argument be an XPath description.
精彩评论