0

I am looking for a script which grabs the Followers from multiple Social Media Sites to one XML.

Example:

I do have multiple Social Media Channels, e.g.:

  • 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/

Now I want to sum up all the Followers of each network, say:

  • facebook.com/fbchannel1 //200 Followers
  • facebook.com/fbchannel2 //300 Followers

    = 500 FB Followers

  • twitter.com/twchannel1 //200 Followers
  • twitter.com/twchannel2 //300 Followers

    = 500 TW Followers

  • youtube.com/user/ytchannel1 //200 Followers
  • youtube.com/user/ytchannel2 //300 Followers

    = 500 YT Followers

  • plus.google.com/11111/ //200 Followers
  • plus.google.com/22222/ //300 Followers

    = 500 G+ Followers

The script should then put these data into the following 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>

What I did so far and where my limited skills end.
Merging various codes into one .php gave me this: http://img.524d.de/i/xqki8x1mwqp4.jpg

I used following code:

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



How do I get these scripts to parse their output in the XML I mentioned before?

Thank you so much in advance!

  • check this: http://stackoverflow.com/questions/486757/how-to-generate-xml-file-dynamically-using-php – Qarib Haider Jul 02 '14 at 07:15
  • Thank you very much, I am much closer to my goal. Now I am able to read the amout of likes and paste the number into the .xml The last thing I need to do, is to sum up all Facebook/YouTube/Twitter likes before I paste them into the .xml. I think the current scripts don't work, because they are independent php-containers. How can I solve this problem? Thanks so much! – user3796457 Jul 02 '14 at 12:25
  • @user3796457: Please provide the whole code so that one could reproduce your issue, including the updates you've been commenting on Jul 2 at 12:25. – hakre Jul 06 '14 at 18:55

1 Answers1

0

Alright, this is my current code. It works quite well so far, except of G+, which I could not get running until now. Twitter does not even need a token to work. I dont know why, to be honest.

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