Grabbing number of Followers from multiple Social Media sites to one XML - php

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
#twchannel1<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!

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

Related

json for displaying youtube video description and view count is not working after a youtube update

I have a facebook like wall, in my project, that when a user posts a youtube link, it is displayed like in facebook wall. First the image and then the description and the views. My code used to work sucessfully, but after an update at youtube, there are bugs, Image displayed sucessfully but not the title, description and the views. Any ideas how to fix this? This is my code for getting title, video description and views count:
<?php
$find_youtube = "://www.youtube.com/";
if(preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT'])) {
if(preg_match("[$find_youtube]", $row['comment'])){
parse_str( parse_url( $row['comment'], PHP_URL_QUERY ), $my_array_of_vars );
$regex='#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n ]+#';
preg_match($regex, $row['comment'], $mid);
$id = $mid[0];
$json_output = file_get_contents("http://gdata.youtube.com/feeds/api/videos/{$id}?v=2&alt=json");
$json = json_decode($json_output, true);
$video_title = $json['entry']['title']['$t'];
$video_description = $json['entry']['media$group']['media$description']['$t'];
$view_count = $json['entry']['yt$statistics']['viewCount'];
$video_perigrafh = substr($video_description , 0, 160);
}
}
?>
You can call Video.list() method of V3 API To get the video resource with all the associated details. Check following link:
https://developers.google.com/youtube/v3/docs/videos
$json_output = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id={$id}&part=snippet,statistics&alt=json");
$json = json_decode($json_output, true);
$video_title = $json['entry']['snippet']['title'];
$video_description = $json['entry']['snippet']['description'];
$view_count = $json['entry']['statistics']['viewCount'];
$video_perigrafh = substr($video_description , 0, 160);

Namespace in MRSS feed using simplexml PHP Script

Tried to research what I'm doing wrong here, but no luck so far. I want to pull the links and URLs in this MRSS feed using this script, but it's not working. Thought all I needed to do was use namespaces to get the child elements out, but no luck:
<?php
$html = "";
$url = "http://feeds.nascar.com/feeds/video?command=search_videos&media_delivery=http&custom_fields=adtitle%2cfranchise&page_size=100&sort_by=PUBLISH_DATE:DESC&token=217e0d96-bd4a-4451-88ec-404debfaf425&any=franchise:%20Preview%20Show&any=franchise:%20Weekend%20Top%205&any=franchise:Up%20to%20Speed&any=franchise:Press%20Pass&any=franchise:Sprint%20Cup%20Practice%20Clips&any=franchise:Sprint%20Cup%20Highlights&any=franchise:Sprint%20Cup%20Final%20Laps&any=franchise:Sprint%20Cup%20Victory%20Lane&any=franchise:Sprint%20Cup%20Post%20Race%20Reactions&any=franchise:All%20Access&any=franchise:Nationwide%20Series%20Qualifying%20Clips&any=franchise:Nationwide%20Series%20Highlights&any=franchise:Nationwide%20Series%20Final%20Laps&any=franchise:Nationwide%20Series%20Victory%20Lane&any=franchise:Nationwide%20Series%20Post%20Race%20Reactions&any=franchise:Truck%20Series%20Qualifying%20Clips&any=franchise:Truck%20Series%20Highlights&any=franchise:Truck%20Series%20Final%20Laps&any=franchise:Truck%20Series%20Victory%20Lane&any=franchise:Truck%20Series%20Post%20Race%20Reactions&output=mrss";
$xml = simplexml_load_file($url);
$namespaces = $xml->getNamespaces(true); // get namespaces
for($i = 0; $i < 50; $i++){ // will return the 50 most recent videos
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
$pubDate = $xml->channel->item[$i]->pubDate;
$description = $xml->channel->item[$i]->description;
$titleid = $xml->channel->item[$i]->children($namespaces['bc'])->titleid;
$url = $xml->channel->item[$i]->children($namespaces['media'])->url;
$html .= "<h3>$title</h3>$description<p>$pubDate<p>$link<p>Video ID: $titleid<p>
<iframe width='480' height='270' src='http://link.brightcove.com/services/player/bcpid3742068445001?bckey=//my API token goes here &bctid=$titleid&autoStart=false' frameborder='0'></iframe><hr/>";/* this embed code is from the youtube iframe embed code format but is actually using the embedded Ooyala player embedded on the Campus Insiders page. I replaced any specific guid (aka video ID) numbers with the "$guid" variable while keeping the Campus Insider Ooyala publisher ID, "eb3......fad" */
}
echo $html;
?>
I take it this isn't the right approach:
$url = $xml->channel->item[$i]->children($namespaces['media'])->url;
What am I doing wrong here?
Thanks for any and all help!
MD
SimpleXML is deceptively named as it is more difficult to use than DOMDocument or the other PHP XML extensions. To get the URL, you'll need to access the url attribute of the media:content node:
<media:content duration="95" medium="video" type="video/mp4"
url="http://brightcove.meta.nascar.com.edgesuite.net/vod/etc"/>
Target the first <media:content> node using
$xml->channel->item[$i]->children($namespaces['media'])->content[0]
and get its attributes:
$m_attrs =
$xml->channel->item[$i]->children($namespaces['media'])->content[0]->attributes();
You can then access the url attribute:
echo "URL: " . $m_attrs["url"] . "\n";
Your code should thus be:
$titleid = $xml->channel->item[$i]->children($namespaces['bc'])->titleid;
$m_attrs = $xml->channel->item[$i]->children($namespaces['media'])->content[0]->attributes();
$url = $m_attrs["url"];
$html .= "<h3>$title</h3>$description<p>$pubDate<p>$link<p>Video ID: $titleid<p> (etc.)";

YouTube API Videocount

I'm trying to pull the count of how many videos is uploaded onto an YouTube channel, but I'm having problems. I want to show the number of videos uploaded to the channel, like this does with other statistics:
$data = file_get_contents('http://gdata.youtube.com/feeds/api/users/imsparky15?alt=json');
$data = json_decode($data, true);
$stats_data = $data['entry']['yt$statistics'];
echo 'lastWebAccess = '.$stats_data['lastWebAccess'].'<br />';
echo 'subscriberCount = '.$stats_data['subscriberCount'].'<br />';
echo 'videoWatchCount = '.$stats_data['videoWatchCount'].'<br />';
echo 'viewCount = '.$stats_data['viewCount'].'<br />';
echo 'totalUploadViews = '.$stats_data['totalUploadViews'].'<br />';
Instead you can just use the Data API v3, do a channels->list API call.
In the response, you will get it with statistics.videoCount
GET https://www.googleapis.com/youtube/v3/channels?part=statistics&id={CHANNEL_ID}&fields=items%2Fstatistics&key={YOUR_API_KEY}
Also usernames are not unique, use channel id's everywhere.
[This answer was originally edited into the question by user2690217. The original question has been reinstated, and the answer moved into this Community Wiki post.]
This will give you a count of videos that a channel has uploaded:
<?php
$data = file_get_contents('http://gdata.youtube.com/feeds/api/users/USERNAME?alt=json');
$data = json_decode($data, true);
$stats_data = $data['entry']['gd$feedLink'];
echo $stats_data[4]['countHint'];
?>
Updated with YouTube-API v3:
<?php
$data = file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=statistics&id={CHANNEL_ID}&fields=items%2Fstatistics&key={YOUR_API_KEY}');
$data = json_decode($data, true);
$stats_data = $data['items']['0']['statistics'];
echo $stats_data['videoCount'];
?>

Getting title, description and number of views of a youtube video

$youtube = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2');
$title = $youtube->title;
This gets the title. But how could I get the viewcount and description? tried $youtube->description; and $youtube->views;
I suggest you to use the JSON output instead of the XML one.
You can get it by adding the alt=json parameter to your URL:
http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2&alt=json
Then you have to load the json and parse it:
<?php
$json_output = file_get_contents("http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2&alt=json");
$json = json_decode($json_output, true);
//This gives you the video description
$video_description = $json['entry']['media$group']['media$description']['$t'];
//This gives you the video views count
$view_count = $json['entry']['yt$statistics']['viewCount'];
//This gives you the video title
$video_title = $json['entry']['title']['$t'];
?>
Hope this helps.
UPDATE
To see what variables does the JSON output have, add the prettyprint=true parameter to the URL and open it in your browser, it will beautify the JSON output to make it more comprehensible:
http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2&alt=json&prettyprint=true
Instead of browsing the URL you can just write
echo "<pre>";
print_r($json);
echo "</pre>";
after
$json = json_decode($json_output, true);
and it will print the formatted JSON output
My code:
$vId = "Jer8XjMrUB4";
$gkey = "AIzaSyCO5lIc_Jlrey0aroqf1cHXVF1MUXLNuR0";
$dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=".$vId."&key=".$gkey."");
$data = json_decode($dur, true);
foreach ($data['items'] as $rowdata) {
$vTime = $rowdata['contentDetails']['duration'];
$desc = $rowdata['snippet']['description'];
}
$interval = new DateInterval($vTime);
$vsec = $interval->h * 3600 + $interval->i * 60 + $interval->s;
if($vsec > 3600)
$vsec = gmdate("H:i:s", $vsec);
else
$vsec = gmdate("i:s", $vsec);
echo $vsec."--".$desc;
Result:
02:47--Following the critically acclaimed global smash hit X-Men:
Youtube API V2.0 has been deprecated.You must have to switch to V3 API.Which need API key.
So prefer to use but In this way so can not get video description
http://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=ktt3C7nQbbA&format=json

Can get JSON from Google Maps call

I have managed to get the longitude and latitude of postcodes from Google Maps but I am unable to then get the distance between the two. The following url gives me some JSON:
https://maps.googleapis.com/maps/api/distancematrix/json?origins=51.6896118,-0.7846495&destinations=51.7651382,-3.7914676&units=imperial&sensor=false
But I can't strip it out using PHP:
<?php
$du = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=51.6896118,-0.7846495&destinations=51.7651382,-3.7914676&units=imperial&sensor=false";
$djd = json_decode(utf8_encode($du),true);
print("-".$djd."-");
?>
Anybody know why this is?
EDIT
The following worked just fine:
<?php
$pc1 = 'SA92NH';
$pc2 = 'HP270SW';
$url1 = "https://maps.googleapis.com/maps/api/geocode/json?address=".$pc1."&sensor=false";
$url2 = "https://maps.googleapis.com/maps/api/geocode/json?address=".$pc2."&sensor=false";
$url1_data = file_get_contents($url1);
$url2_data = file_get_contents($url2);
$json1_data = json_decode(utf8_encode($url1_data),true);
$json2_data = json_decode(utf8_encode($url2_data),true);
$longlat1 = $json1_data['results'][0]['geometry']['location']['lat'].",".$json1_data['results'][0]['geometry']['location']['lng'];
$longlat2 = $json2_data['results'][0]['geometry']['location']['lat'].",".$json2_data['results'][0]['geometry']['location']['lng'];
print($longlat1."<br>\n");
print($longlat2."<br>\n");
?>
Is this what you are looking for?
You need to get the data, you cannot only type the url.
In my example i use get file_get_contents.
<?php
$du = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?origins=51.6896118,-0.7846495&destinations=51.7651382,-3.7914676&units=imperial&sensor=false");
$djd = json_decode(utf8_encode($du),true);
print_r($djd);
?>
http://codepad.viper-7.com/LqxpJW
$url = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins=51.6896118,-0.7846495&destinations=51.7651382,-3.7914676&units=imperial&sensor=false';
$content = file_get_contents($url);
$json = json_decode($content, true);
And after that access $json like an array !
Something like that:
echo $json['rows']['elements']['distance']['text'];

Categories