I want to get artist name from last.fm api
HEre is a code
$jsonData = file_get_contents('http://ws.audioscrobbler.com/2.0/?method=chart.gettoptracks&limit=56&api_key=MYAPIKEY&format=json');
$jsonData = json_decode($jsonData, true);
foreach ($jsonData['tracks']['track'] as $track) {
$title = $track['name'];
$image = $track['image'][3]['#text'];
echo '<div id="track_short"><div class="track_short">
<a href="http://'.$SiteUrl.'/mp3-download-'.cano($title).'/" ><img src="'.$image.'" alt="'.$title.'">
<div class="description"><p class="description_content">'.$title.'</p>
</div></a></div></div>';
}
Example of API data
{"tracks":{"track":[{"name":"Sorry","duration":"0","playcount":"1931615","listeners":"193074","mbid":"","url":"http://www.last.fm/music/Justin+Bieber/_/Sorry","streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Justin Bieber","mbid":"e0140a67-e4d1-4f13-8a01-364355bee46e","url":"http://www.last.fm/music/Justin+Bieber"},"image":[{"#text":"http://img2-ak.lst.fm/i/u/34s/d5af34cbc048b190fc7369acdcf8655b.png","size":"small"},{"#text":"http://img2-ak.lst.fm/i/u/64s/d5af34cbc048b190fc7369acdcf8655b.png","size":"medium"},{"#text":"http://img2-ak.lst.fm/i/u/174s/d5af34cbc048b190fc7369acdcf8655b.png","size":"large"},{"#text":"http://img2-ak.lst.fm/i/u/300x300/d5af34cbc048b190fc7369acdcf8655b.png","size":"extralarge"}]}],"#attr":{"page":"1","perPage":"1","totalPages":"253572626","total":"253572626"}}}
cool thanks i have correct
from : $phpArtist = $phpJson['tracks']['track'][0]['artist'];
to : $Artist = $track['artist']['name'];
and its working, thanks
Using PHP's json_decode() method:
$json = '{"tracks":{"track":[{"name":"Sorry","duration":"0","playcount":"1931615","listeners":"193074","mbid":"","url":"http://www.last.fm/music/Justin+Bieber/_/Sorry","streamable":{"#text":"0","fulltrack":"0"},"artist":{"name":"Justin Bieber","mbid":"e0140a67-e4d1-4f13-8a01-364355bee46e","url":"http://www.last.fm/music/Justin+Bieber"},"image":[{"#text":"http://img2-ak.lst.fm/i/u/34s/d5af34cbc048b190fc7369acdcf8655b.png","size":"small"},{"#text":"http://img2-ak.lst.fm/i/u/64s/d5af34cbc048b190fc7369acdcf8655b.png","size":"medium"},{"#text":"http://img2-ak.lst.fm/i/u/174s/d5af34cbc048b190fc7369acdcf8655b.png","size":"large"},{"#text":"http://img2-ak.lst.fm/i/u/300x300/d5af34cbc048b190fc7369acdcf8655b.png","size":"extralarge"}]}],"#attr":{"page":"1","perPage":"1","totalPages":"253572626","total":"253572626"}}}';
$phpJson = json_decode($json, true, 10); //4 is the depth of the JSON string, but you can make this a big number and it will still work
$phpArtist = $phpJson['tracks']['track'][0]['artist']['name'];
Let me know if that works, or if the php needs to get tweaked, I don't have a PHP client up right now.
Related
I got a new problem while working on my project.
I want to decode this (what I tried until now):
$items = array($_POST['eingabe']);
$strs = file_get_contents("https://open-market.io/api/search/?name=$items&appID=730&limit=5");
$json = json_decode($strs, true);
$picimage = $json['results'][0]['image'];
echo '<a class="test1">' . $picimage . '</a></div></br>';
I really don't know what I did wrong. Can someone please show me my mistake?
example for json:
https://open-market.io/api/search/?name=Chroma%20Case&appID=730&limit=5
implode your array and urlencode it.
<?php
$items = array('counter','age');
$strs = file_get_contents("https://open-market.io/api/search/?name=".urlencode(implode($items,','))."&appID=730&limit=5");
$json = json_decode($strs, true);
$picimage = $json['results'][0]['image'];
echo '<a class="test1"> ' . $picimage . "</a></div></br>";
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'];
?>
$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
I would like to be able to extract a title and description from Wikipedia using json. So... wikipedia isn't my problem, I'm new to json and would like to know how to use it. Now I know there are hundreds of tutorials, but I've been working for hours and it just doesn't display anything, heres my code:
<?php
$url="http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url";
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
$pageid = $data->query->pageids;
echo $data->query->pages->$pageid->title;
?>
Just so it easier to click:
http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url&indexpageids
I know I've probably just done a tiny thing wrong, but its really bugging me, and the code... I'm used to using xml, and I have pretty much just made the switch, so can you explain it a bit for me and for future visitors, because I'm very confused... Anything you need that I haven't said, just comment it, im sure I can get it, and thanks, in advance!
$pageid was returning an array with one element. If you only want to get the fist one, you should do this:
$pageid = $data->query->pageids[0];
You were probably getting this warning:
Array to string conversion
Full code:
$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url&indexpageids';
$json = file_get_contents($url);
$data = json_decode($json);
$pageid = $data->query->pageids[0];
echo $data->query->pages->$pageid->title;
I'd do it like this. It supports there being multiple pages in the same call.
$url = "http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url";
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
$titles = array();
foreach ($data['query']['pages'] as $page) {
$titles[] = $page['title'];
}
var_dump($titles);
/* var_dump returns
array(1) {
[0]=>
string(6) "Google"
}
*/
Try this it will help you 💯%
This code is to extract title and description with the help of Wikipedia api from Wikipedia
<?php
$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url&indexpageids';
$json = file_get_contents($url);
$data = json_decode($json);
$pageid = $data->query->pageids[0];
$title = $data->query->pages->$pageid->title;
echo "<b>Title:</b> ".$title."<br>";
$string=$data->query->pages->$pageid->extract;
// to short the length of the string
$description = mb_strimwidth($string, 0, 322, '...');
// if you don't want to trim the text use this
/*
echo "<b>Description:</b> ".$string;
*/
echo "<b>Description:</b> ".$description;
?>
I found that App Store API support additional information about application.
$keyword = $this->input->post('keyword');
$appstore_api = 'http://itunes.apple.com/search?country=US&entity=software&term='.$keyword;
$data = file_get_contents($appstore_api);
echo $data;
here is the PHP code that I wrote.
I get this result.
{ "resultCount":50, "results": [ {"kind":"software", "features":[], "supportedDevices":["all"], "isGameCenterEnabled":false, "artistViewUrl":"http://itunes.apple.com/us/artist/burbn-inc./id389801255?uo=4", "artworkUrl60":"http://a2.mzstatic.com/us/r1000/105/Purple/v4/f3/0e/e2/f30ee271-c564-ec21-02d6-d020bd2ff38b/Icon.png", "screenshotUrls":["http://a3.mzstatic.com/us/r1000/102/Purple/v4/2d/25/d3/2d25d348-74e9-8365-208c-45e64af73ed6/mzl.xnvocmpt.png", "http://a1.mzstatic.com/us/r1000/077/Purple/v4/04/18/b3/0418b375-c0c1-18c1-1aef-07555c99af46/mzl.pkthtqtv.png", "http://a2.mzstatic.com/us/r1000/069/Purple/v4/31/08/65/31086528-2f37-bca0-71f3-c3095e698f35/mza_8774562250786021670.png", "http://a3.mzstatic.com/us/r1000/091/Purple/v4/95/a2/65/95a265bb-b9f0-8732-9fe4-823ffbc9aba0/mza_5807950548098772841.png"], "ipadScreenshotUrls":[], "artworkUrl512":"http://a4.mzstatic.com/us/r1000/089/Purple/v4/44/76/7f/44767fb5-4cb2-25bf-5361-25138b8c2aeb/mzl.ntalagmr.png",
The question is that how can I extract the variable named "artworkUrl512" ?
I tried like this but failed.
$image_url = $data->results->artworkUrl512;
$image_url2 = $data['results']['artworkUrl512'];
Can you help me how to extract icon image url?
$data is json encoded. Use json_decode():
http://php.net/manual/en/function.json-decode.php
$data = json_decode($json, true);
echo $data['results'][0]['artworkUrl512'];