将多个社交媒体网站的关注者数量增加到一个XML

时间:2014-07-02 07:06:41

标签: php xml facebook twitter youtube

我正在寻找一个脚本,将来自多个社交媒体网站的粉丝抓取到一个XML。

示例:

我有多个社交媒体渠道,例如:

  • facebook.com/fbchannel1
  • facebook.com/fbchannel2
  • twitter.com/twchannel1
  • twitter.com/twchannel2
  • youtube.com/user/ytchannel1
  • youtube.com/user/ytchannel2
  • plus.google.com/11111 /
  • plus.google.com/22222 /

现在我想总结每个网络的所有追随者,比如说:

  • facebook.com/fbchannel1 // 200粉丝
  • facebook.com/fbchannel2 // 300粉丝

    = 500 FB追随者

  • twitter.com/twchannel1 // 200粉丝
  • twitter.com/twchannel2 // 300粉丝

    = 500 TW粉丝

  • youtube.com/user/ytchannel1 // 200粉丝
  • youtube.com/user/ytchannel2 // 300粉丝

    = 500 YT粉丝

  • plus.google.com/11111/ // 200粉丝
  • plus.google.com/22222/ // 300粉丝

    = 500 G +粉丝

然后,脚本应将这些数据放入以下XML中:

<?xml version="1.0" standalone="yes"?><socialMedia xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<social>
    <network>Facebook</network>
    <followers>500</followers>
</social>
<social>
    <network>Twitter</network>
    <followers>500</followers>
</social>
<social>
    <network>Google+</network>
    <followers>500</followers>
</social>
<social>
    <network>YouTube</network>
    <followers>500</followers>
</social>

到目前为止我做了什么,我的技能有限。
将各种代码合并为一个.php给了我这个: http://img.524d.de/i/xqki8x1mwqp4.jpg

我使用了以下代码:

<?php
//The following code returns the Number of likes for any facebook page.

//Page Id of TechRecite.com. Replace it with your page.
$page_id = "fbchannel1";
$likes = 0; //Initialize the count

//Construct a Facebook URL
$json_url ='https://graph.facebook.com/'.$page_id.'';
$json = file_get_contents($json_url);
$json_output = json_decode($json);

//Extract the likes count from the JSON object
if($json_output->likes){
    $likes = $json_output->likes;
}
//Printing the count
echo ''.$likes.'';
?>

微博

<a href="https://twitter.com/twchannel1" class="twitter-follow-button" data-show-count="true" data-lang="en">@twchannel1</a><br>

的YouTube

    <?php
$data = file_get_contents('http://gdata.youtube.com/feeds/api/users/zeissbettervision?alt=json');
$data = json_decode($data, true);
$stats_data = $data['entry']['yt$statistics'];
echo ''.$stats_data['subscriberCount'].'';
?>



如何让这些脚本在我之前提到的XML中解析其输出?

提前非常感谢你!

1 个答案:

答案 0 :(得分:0)

好的,这是我目前的代码。到目前为止它运作得很好,除了G +,直到现在我都无法运行。 Twitter甚至不需要令牌来工作。说实话,我不知道为什么。

<?php
    /* Gathering Number of Facebook Followers */

$facebookpage1 = file_get_contents('http://graph.facebook.com/facebookpage1');
$facebookpage2 = file_get_contents('http://graph.facebook.com/facebookpage2');

$facebookpage1 = json_decode($facebookpage1, true);
$facebookpage2 = json_decode($facebookpage2, true);

echo ''.$facebookpage1['likes'].'<br>';
echo ''.$facebookpage2['likes'].'<br>';

$var1 = $facebookpage1['likes'];
$var2 = $facebookpage2['likes'];


$FBtotal = $var1 + $var2;
?>



<?php
    /* Gathering Number of Twitter Followers */

$DATAtwittersite1 = file_get_contents('http://cdn.api.twitter.com/1/users/lookup.json?screen_name=twittersite1');
$DATAtwittersite2 = file_get_contents('http://cdn.api.twitter.com/1/users/lookup.json?screen_name=twittersite2');

$DATAtwittersite1 = json_decode($DATAtwittersite1, true);
$DATAtwittersite2 = json_decode($DATAtwittersite2, true);

$twittersite1 = $DATAtwittersite1['0'];
$twittersite2 = $DATAtwittersite2['0'];

echo ''.$twittersite1['followers_count'].'<br>';
echo ''.$twittersite2['followers_count'].'<br>';


$var1 = $twittersite1['followers_count'];
$var2 = $twittersite2['followers_count'];

$TWtotal = $var1 + $var2;
?>



<?php
    /* Gathering Number of YouTube Subscribers */

$DATAyoutubechannel1 = file_get_contents('http://gdata.youtube.com/feeds/api/users/youtubechannel1?alt=json');
$DATAyoutubechannel2 = file_get_contents('http://gdata.youtube.com/feeds/api/users/youtubechannel2?alt=json');

$DATAyoutubechannel1 = json_decode($DATAyoutubechannel1, true);
$DATAyoutubechannel2 = json_decode($DATAyoutubechannel2, true);

$youtubechannel1 = $DATAyoutubechannel1['entry']['yt$statistics'];
$ZeissMicroscopyyoutubechannel2 = $DATAyoutubechannel2['entry']['yt$statistics'];

echo ''.$youtubechannel1['subscriberCount'].'<br>';
echo ''.$youtubechannel2['subscriberCount'].'<br>';

$var1 = $youtubechannel1['subscriberCount'];
$var2 = $youtubechannel2['subscriberCount'];

$YTtotal = $var1 + $var2;
?>




<?php
    /* Saving Gathered Information to XML */

    /* Create a DOM Document with Encoding UTF8 */
    $domtree = new DOMDocument('1.0', 'UTF-8');

    /* Create the Root Element of the XML Tree */
    $xmlRoot = $domtree->createElement("socialMedia");
    /* append it to the document created */
    $xmlRoot = $domtree->appendChild($xmlRoot);


    /* Facebook */
    $social = $domtree->createElement("social");
    $social = $xmlRoot->appendChild($social);
    $social->appendChild($domtree->createElement('network','Facebook'));
    $social->appendChild($domtree->createElement('followers',''.$FBtotal.''));

    /* Twitter */
    $social = $domtree->createElement("social");
    $social = $xmlRoot->appendChild($social);
    $social->appendChild($domtree->createElement('network','Twitter'));
    $social->appendChild($domtree->createElement('followers',''.$TWtotal.''));

    /* Google+ */
    $social = $domtree->createElement("social");
    $social = $xmlRoot->appendChild($social);
    $social->appendChild($domtree->createElement('network','Google+'));
    $social->appendChild($domtree->createElement('followers','2250'));

    /* YouTube */
    $social = $domtree->createElement("social");
    $social = $xmlRoot->appendChild($social);
    $social->appendChild($domtree->createElement('network','YouTube'));
    $social->appendChild($domtree->createElement('followers',''.$YTtotal.''));


$domtree->save('./xml/follower.xml');

    /* get the xml printed */
    echo $domtree->saveXML();
?>