ich want to code a site with informations about some youtube-channels. I got the following parameters:
Subscribers:
<?php
$json = file_get_contents("http://gdata.youtube.com/feeds/api/users/*username*?alt=json");
$data = json_decode($json, true);
echo '' . $data['entry']['yt$statistics']['subscriberCount'];
?>
Chanel-Views:
<?php
$json = file_get_contents("http://gdata.youtube.com/feeds/api/users/*username*?alt=json");
$data = json_decode($json, true);
echo '' . $data['entry']['yt$statistics']['viewCount'];
?>
Total-Views:
<?php
$json = file_get_contents("http://gdata.youtube.com/feeds/api/users/*username*?alt=json");
$data = json_decode($json, true);
echo '' . $data['entry']['yt$statistics']['totalUploadViews'];
?>
But how could I get the count of the uploaded Videos. I only want to show the count how many videos were uploaded by this user.
And i want to show the age, description, snippet, the date when he joined youtube.
Could anybody help me?
The number of public videos in a given YouTube channel can be obtained via the openSearch$totalResults->$t property of the uploads feed for that channel. Here's an example for GoogleDevelopers:
http://gdata.youtube.com/feeds/api/users/googledevelopers/uploads?v=2&alt=json
"openSearch$totalResults": {
"$t": 1458
}
Most of that other information you're looking for, regarding the channel creator's personal details, is no longer exposed on YouTube.com or via the API.
Related
My inexperience has left me short of understanding how to hide an API Key. Sorry, but I've been away from web development for 15 years as I specialized in relational databases, and a lot has changed.
I've read a ton of articles, but don't understand how to take advantage of them. I want to put my YouTube API key(s) on the server, but have the client able to use them w/o exposure. I don't understand how setting an API Key on my server (ISP provided) enables the client to access the YouTube channel associated with the project. Can someone explain this to me?
I am not sure what you want to do but for a project I worked on I needed to get a specific playlist from YouTube and make the contents public to the visitors of the website.
What I did is a sort of proxy. I set up a php file contains the api key, and then have the end user get the YT content through this php file.
The php file gets the content form YT using curl.
I hope it helps.
EDIT 1
The way to hide the key is to put it in a PHP file on the server.
This PHP file will the one connecting to youtube and retrieving the data you want on your client page.
This example of code, with the correct api key and correct playlist id will get a json file with the 10 first tracks of the play list.
The $resp will have the json data. To extract it, it has to be decoded for example into an associative array. Once in the array it can be easily mixed in to the html that will be rendered on the client browser.
<?php
$apiKey = "AIza...";
$results = "10";
$playList = "PL0WeB6UKDIHRyXXXXXXXXXX...";
$request = "https://www.googleapis.com/youtube/v3/playlistItems?part=id,contentDetails,snippet&maxResults=" . $results .
"&fields=items(contentDetails%2FvideoId%2Cid%2Csnippet(position%2CpublishedAt%2Cthumbnails%2Fdefault%2Ctitle))" .
"&playlistId=" . $playList .
"&key=" . $apiKey;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $request,
CURLOPT_SSL_VERIFYPEER => false
));
$resp = curl_exec($curl);
if (curl_errno($curl)) {
$status = "CURL_ERROR";
}else{
// check the HTTP status code of the request
$resultStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($resultStatus == 200) {
$status = "OK";
//Do something with the $resp which is in JSON format.
//Like decoding it into an associative array
} else {
$status = "YT_ERROR";
}
}
curl_close($curl);
?>
<html>
<!-- your html here -->
</html>
Note: CURLOPT_SSL_VERIFYPEER is set to false. This is in development. For prod it should be true.
Also note that using the api this way, you can restrict the calls to your api key bounding them to your domain. You do that in the googla api console. (Tip for production)
I am trying to achive is whenever I receive an text message with certain keywords, I will get a call saying that I have received a text message and press 1 to talk to the person who sent the text message.
I have done it by creating three files, these are below
Twilio.php
This receives the post request on a text message and call me
$name = $_POST['name'];
$phone = $_POST['phone'];
$client = new Services_Twilio($AccountSid, $AuthToken);
try {
// make call
$call = $client->account->calls->create(
$caller,
$number,
array("url" => "http://somewebsite.net/twilio/twiml.php?phone=$phone&name=$name")
);
} catch (Exception $e) {
echo 'Error starting phone call: ' . $e->getMessage();
}
The next file if twiml.php which handle the call and ask me if I want to speak and press 1 to speak
twiml.php
$nm = $_GET['name'];
$ph = $_GET['phone'];
$name = "Deepak";
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<Say>Hello <?php echo $name ?>.</Say>
<Gather numDigits="1" action="http://somewebsite.net/twilio/call.php?phone=<?php echo $ph ?>" method="POST">
<Say>You have a text message, press 1 to speak.</Say>
</Gather>
</Response>
The third file is called if I press 1 to speak and then it dials that number : below is the code:
Call.php
<?php
if($_REQUEST['Digits'] != '1') {
header("Location: twiml.php");
die;
}
$ph = $_GET['phone'];
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<Dial>+1 <?php echo $ph ?></Dial>
</Response>
I am trying to achieve is to merge these three files into one so I need not to make a Post request and I could handle the whole call in a single file or at least I can merge twiml.php and call.php
Is there a way to combine these?
Twilio developer evangelist here.
You could put it all into one file and use the URI that was requested ($_SERVER['REQUEST_URI']) to switch on the code you then run. Or, it might be more interesting to try a small PHP framework that abstracts that away. Something like Slim or Silex might work for you.
I would ask why you need to run this from one file when your feature looks like it works?
I am trying to extract the video link of my private playlist. but i am unable to do
I used the below code to get the list
<?php
if(isset($_POST['send']))
{
$cont = json_decode(file_get_contents('http://gdata.youtube.com/feeds/api/playlists/'.$_POST['a'].'/?v=2')); ?>
<?php $feed = $cont->feed->entry; ?>
<?php if(count($feed)): foreach($feed as $item): // youtube start ?>
<?php echo $item->{'media$group'}->{'yt$videoid'}->{'$t'} ?>
<?php echo $item->title->{'$t'} ?>
<?php endforeach; endif; }?>
but this doesn't work. i need to extract only link with video title.
i have my account details with me how its possible using php
Since you want a private playlist, this will need to be an authenticated call, so you'll first have to set up an oAuth exchange. In PHP the easiest way to do this is with the gapi client; you can see more info (and get the library and sample code) here:
https://developers.google.com/youtube/v3/code_samples/php#retrieve_my_uploads
It will require you to use the client ID and secret for your registered app, though (don't forget to turn on the YouTube API v3 in the registered app!).
Once the oAuth is working, you can change the sample code listed above to very easily get your private playlist; starting with line 40, you'll have to use a call that's something like this:
$playlistResponse = $youtube->playlist->listPlaylists('contentDetails', array(
'id' => $_POST['a'], // assuming this variable represents the id of your playlist
));
You'll also have to somewhat modify the code after that block to be able to parse the playlist feed and get your info.
I am working on pulling the number of twitter followers into my Wordpress page but running into a small problem I cannot figure out. I imagine it would be a quick fix...do you think you can help?
The issue is the output reads:
11131
I would like it to read:
11,131
You see the comma in the correct position...not sure how to get that to render in the right format. Here is the code I am working with:
<?php
$data = json_decode(file_get_contents('https://api.twitter.com/1/user/lookup.json?screen_name=erinschreyer'), true);
echo $data[0]['followers_count'];
?>
Any help is appreciated...
It would work with that:
<?php
$data = json_decode(file_get_contents('https://api.twitter.com/1/users/lookup.json?screen_name=erinschreyer'), true);
$followers = $data[0]['followers_count'];
echo number_format($followers);
?>
See number_format()
I know you can use the API (https://dev.twitter.com/docs/api/1/get/users/show), But how can I get this to appear on my website? E.g. 'Username' has 'follower count'.
Here you go:
<?php
$url = "http://api.twitter.com/1/users/show.json?screen_name=jjmpsp&include_entities=true";
$feed = file_get_contents($url);
$twitter_decoded = json_decode($feed);
echo $twitter_decoded->followers_count;
?>