Retrieving App Store application icon URL with PHP - php

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

Related

Json decode in wordpress

here I use this code in my local host
$fromdata= $_POST["data"];
$fromValue=json_decode($fromdata);
$patientid= $fromValue->patientid;
$patientname= $fromValue->name;
its working fine... if i use
print_r($fromdata);
it print the following format
{"patientid":"55","name":"Sow"}
. the same code is used in wordpress the print_r($fromdata); return {\"patientid\":\"16\",\"name\":\"Ravindran\"} this. and unable to get value
how to get value from this object
thanks
You can try this.
<?php
$data = '{\"patientid\":\"16\",\"name\":\"Ravindran\"}';
$data = stripslashes($data);
$return = json_decode($data);
print_r($return);

Error in getting data from Json in PHP

I am trying to access data from json in PHP but it seems not working.
code:
$raw =file_get_contents("http://api.mydomain.com/data.json");
$data = json_decode($raw->list);
echo $data;
I'm getting error that list is not an object.
Here is my json
{ "list" : [ { "data1":" my data"}, {"data2": "my data 2"}]};
What did u do wrong? Also how can i access data1 and others?
You don't need the $raw->list bit and you are getting an object back from json_decode so use print_r and not echo
$raw = file_get_contents("http://api.mydomain.com/data.json");
$data = json_decode($raw);
print_r($data);

How to get artist name form last.fm api

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.

Get JSON object from URL Difficulties

I'm having difficulties grabbing any of the JSON information from this URL.
I've tried other JSON snippets and they seem to work so I'm not sure if it's the way that the URL is structured or something.
Basic example below.
<?php
$json = file_get_contents('http://nhs-sh.cfpreview.co.uk/api/version/fetchLatestData?dataType=Clinics&versionNumber=-1&uuID=website&dt=');
$obj = json_decode($json);
echo "Body: " . $obj->Body;
?>
The link provided starts with
{ data :
which is valid javascript but invalid json. You can test it on http://jsonlint.com. To fix this we can replace the data with "data" :
$json = file_get_contents('http://nhs-sh.cfpreview.co.uk/api/version/fetchLatestData?dataType=Clinics&versionNumber=-1&uuID=website&dt=');
$obj = json_decode($json);
if (json_last_error() !== JSON_ERROR_NONE) { //check if there was an error decoding json
$json = '{ "data" :'. substr(trim($json), 8); // replace the first 8-1 characters with { "data" :
$obj = json_decode($json);
}
print_r($obj->data); //show contents of data
Please note that this fix is dependent on the data source e.g. if they change data to dataset. The correct measure would be to ask the developers to fix their json implementation.

decoding json with php - get url from Facebook profile pic

I need to decode this json value using PHP. I need the value 'url'.
Here is what I have but does not work.
$json = file_get_contents('http://graph.facebook.com/{fb-ID}/picture?type=large&redirect=false');
$decoded = json_decode($json,true);
$url = $decoded[???]; // NEED THIS VAR.
Since $decoded is a PHP array you can access it like any other array:
$url = $decoded['data']['url'];
$decoded=(object)json_decode( $json['data'], true );
echo $decoded->url
This worked:
$json = file_get_contents('http://graph.facebook.com/{fb-ID}/picture?type=large&redirect=false');
$decoded = json_decode($json,true);
$url = $decoded['data']['url'];

Categories