php - 将多个社交媒体网站上的关注者数量提升到一种XML

标签 php xml facebook twitter youtube

我正在寻找一个脚本,该脚本可以将关注者从多个社交媒体网站捕获到一个XML。

例:

我确实有多个社交媒体 channel ,例如:

  • 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位YouTube追踪者
  • 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

    我使用以下代码:

    Facebook
    <?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.'';
    ?>
    

    Twitter
    <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?

    提前非常感谢您!

    最佳答案

    好的,这是我当前的代码。到目前为止,除G +之外,它都运行良好,到目前为止我还无法运行。
    Twitter甚至不需要 token 即可工作。老实说,我不知道为什么。

    <?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();
    ?>
    

    关于php - 将多个社交媒体网站上的关注者数量提升到一种XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24524869/

    相关文章:

    php - 纪元日期转换

    php - 实时拉取API数据并发布

    php - Symfony FK 约束问题

    php - 如何从 CakePHP 中的 Controller 访问助手?

    php - 未启用安全浏览的用户无法看到我的 Facebook 应用程序

    java - Apache 配置 - 处理同名的多个 XML 条目

    c# - 如何在 asp.net mvc View 页面中显示人类可读的 xml

    c# - 如何从 XmlDocument 中删除空格

    objective-c - 无法使用访问 token 创建 Facebook 用户

    facebook - 结合 Facebook 喜欢的 URL 和 Facebook 页面