Youtube Videos Thumbnail - php

I am using this code to get related videos. With this code I get title and link of the video. How can I get the thumb of the video?
<?php
$JSON = file_get_contents("http://gdata.youtube.com/feeds/api/videos/FYpunY-gXxU/related?v=2&alt=json");
$JSON_Data = json_decode($JSON);
$title = $JSON_Data->{'feed'}->{'entry'};
for ($i = 1; $i < 25; $i++)
{
echo ($title[$i]->{'title'}->{'$t'}) . "<br />";
echo $title[$i]->{'link'}[0]->{'href'} . "<br /><br />";
}
?>

When using the json_decode, the best way is to print the entire result for yourself and then see the necessary data you have to get...
Do this:
`<?php
$JSON = file_get_contents("http://gdata.youtube.com/feeds/api/videos/FYpunY-gXxU/related?v=2&alt=json");
$JSON_Data = json_decode($JSON);
echo '<pre>';
print_r($JSON_Data);
?>`
You'll get to know what exactly you have to extract...
I leave that to you... :)

I would :-
<?php
$json = file_get_contents("http://gdata.youtube.com/feeds/api/videos/FYpunY-gXxU/related?v=2&alt=json");
$json_data = json_decode($json, true);
foreach((array)$json_data['feed']['entry'] as $video){
echo $video['title']['$t'].'<br/>';
echo $video['link'][0]['href'].'<br/>';
?>
<img src="<?php echo $video['media$group']['media$thumbnail'][0]['url']?>" />
<img src="<?php echo $video['media$group']['media$thumbnail'][1]['url']?>" />
<?php
}
?>

Related

PHP rest api display an array of values

I am trying to display all the items from an API. How can i display in html or store them in variables. In my code
$api_url = 'demo API';
// Read JSON file
$json_data = file_get_contents($api_url);
// Decode JSON data into PHP array
$response_data = json_decode($json_data, true);
$result = array_values($response_data);
var_dump($result);
but unfortunately i dont know how to display the datas in front
i found a way
<?php
// JSON string
$json_string = '(API)';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
foreach($obj as $row => $innerArray){
foreach($innerArray as $innerRow => $value){
// echo $innerRow.' = '.$value . "<br/>";
if ($innerRow == "thumb_url")
{
?>
<img src="<?php echo $value; ?>" width="280" height="360">
<?php
}
elseif ($innerRow == "title"){
?>
<?php echo $value; ?>
<?php
}
}
}
?>

I want to Echo IMDB ID, Title, Quality and Embed URL From This JSON URL

Now I have this JSON URL https://vidsrc.me/movies/latest/page-1.json I want to echo IMDB ID, Title, Quality and Embed URL for each array as this sounds like a multidimensional array. But every time I try to do this. I get one of the following errors:
- Undefined Index
- Undefined offset
So I want to loop through it and echo each of those items and break line between each,
This is the code
<?php
$url = file_get_contents('https://vidsrc.me/movies/latest/page-1.json');
$decode = json_decode($url, TRUE);
$res = $decode->result;
foreach($decode as $value){
$imdb = $res->imdb_id;
$title = $res->title;
$quality = $res->quality;
$url = $res->embed_url;
echo $imdb;
echo "<br>";
echo $tite;
echo "<br>";
echo $quality;
echo "<br>";
echo $url;
}
?>
json_decode() returns you array but you process it like object, you should process it as array:
<?php
$url = file_get_contents('https://vidsrc.me/movies/latest/page-1.json');
$decode = json_decode($url, TRUE);
foreach($decode['result'] as $value){
$imdb = $value['imdb_id'];
$title = $value['title'];
$quality = $value['quality'];
$url = $value['embed_url'];
echo $imdb;
echo "<br>";
echo $title;
echo "<br>";
echo $quality;
echo "<br>";
echo $url;
}
?>

Get JSON object from URL with php

I have from url this
[{"user_id":"3932131","username":"DanielDimitrov","count300":"1677134","count100":"239025","count50":"41207","playcount":"17730","ranked_score":"1413977663","total_score":"7355146958","pp_rank":"35848","level":"95.5852","pp_raw":"1582.26","accuracy":"97.88556671142578","count_rank_ss":"42","count_rank_s":"337","count_rank_a":"120","country":"BG","events":[]}]
and my php is this
$json = file_get_contents('url');
$obj = json_decode($json);
echo $obj->user_id;
echo 'Username: '.$obj['username'].'<br>';
echo 'PP: '.(int)$obj['pp_raw'].'<br>';
echo 'Level: '.(int)$obj['level'].'<br>';
echo 'Play count: '.$obj['playcount'].'<br>';
I try to remove the brackets from the url and the code run but i get it with brackets... How can i remove them?
<?php
$json = '[{"user_id":"3932131","username":"DanielDimitrov","count300":"1677134","count100":"239025","count50":"41207","playcount":"17730","ranked_score":"1413977663","total_score":"7355146958","pp_rank":"35848","level":"95.5852","pp_raw":"1582.26","accuracy":"97.88556671142578","count_rank_ss":"42","count_rank_s":"337","count_rank_a":"120","country":"BG","events":[]}]';
$in = array("[{", "}]");
$out = array("{", "}");
$obj = json_decode(str_replace($in, $out, $json));
echo $obj->user_id;
echo 'Username: '.$obj->username.'<br>';
echo 'PP: '.(int)$obj->pp_raw.'<br>';
echo 'Level: '.(int)$obj->level.'<br>';
echo 'Play count: '.$obj->playcount.'<br>';
?>
$obj is an array with 1 element.
The short fix is, do $obj = $obj[0] first.
Then you can do $obj["user_id"] etc.

How can I extract the value of this json in php?

I would like to know, how take the value "Tile32px" from this Json:
(http://360api.chary.us/?gamertag=superdeder) with PHP.
I tried this way:
<?php
$json = file_get_contents('http://360api.chary.us/?gamertag=superdeder');
$gamer = json_decode($json);
echo $gamer->Gamertag;
echo "<br><br>";
$data = json_decode($json, true);
$users=$data['LastPlayed'];
foreach ($users as $user)
{
echo $user['Title'];
echo "<br>";
}
echo "<br>";
$users2=$data['Pictures'];
foreach ($users2 as $user2)
{
echo $user2['Tile32px'];
echo "<br>";
}
?>
This is the result:
SuperDeder
Skyrim
Grand Theft Auto V
FIFA 13
L.A. Noire
WSOP: Full House Pro
h
h
h
Thanks a lot.
Andrea.
Try this
<?php
$json = file_get_contents('http://360api.chary.us/?gamertag=superdeder');
$gamer = json_decode($json, true);
$users = $gamer['LastPlayed'];
foreach($users as $user){
echo $user['Pictures']['Tile32px'];
echo '<br>';
}
?>
Try this way.
<?php
$json = file_get_contents('http://360api.chary.us/?gamertag=superdeder');
$data = json_decode($json, true);
foreach ($data['LastPlayed'] as $val)
{
echo $val['Pictures']['Tile32px'];
echo "<br>";
}
?>

Using tumblr API, post image is not showing up

<?php
$feedURL = 'http://########.tumblr.com/api/read/';
$xml = simplexml_load_file($feedURL);
// $posts = $xml->xpath("/tumblr/posts/post");
foreach($xml->posts->post as $post){
$post= $xml->posts->post->{'photo-url'};
}
echo "<pre>";
print_r($post);
echo "</pre>";
?>
This is the script that I have made. I am able to fetch only the first post, but I want all posts to be displayed and the post image is not showing up.
I've got it working using
foreach($xml->posts->post as $post){
$img = (string) $post->{'photo-url'};
echo '<img src="' . $img . '" />';
}
But its only showing a couple image not all of them any ideas ?
Try this:
foreach($xml->posts->post as $post){
$post_urls[] = (string) $post->{'photo-url'};
}
echo "<pre>";
print_r($post_urls);
echo "</pre>";
Update for comment:
foreach($xml->posts->post as $post){
$posts[] = (array)$post->attributes() + (array) $post->children();
}
echo "<pre>";
print_r($posts);
echo "</pre>";
Update 2:
foreach($xml->posts->post as $post){
$img = (string) $post->{'photo-url'};
echo '<img src="' . $img . '" />';
}

Categories