I want to extract the profile_picture of any instagram user from the JSON url: https://www.instagram.com/just/media/ with PHP and print it out. I've tried:
$instaJSONUrl = 'https://www.instagram.com/just/media/';
$json = file_get_contents($instaJSONUrl);
$json_data = json_decode($json, true);
echo $json_data["profile_picture"];
Not working.. If I print out the $json only, I get:
{status":"ok","items":[],"more_available":false}
Meaning that the problem is with reading the URL. Does this mean I need access token, because I don't want to use one, as I already can get the profile image URL when I open https://www.instagram.com/just/media/ from my browser.
This will result in an array of users and their picture profile links.
<?php
$instaJSONUrl = 'https://www.instagram.com/just/media/';
$json = file_get_contents($instaJSONUrl);
$json_data = json_decode($json, true);
$users = array();
foreach($json_data['items'] as $item)
foreach($item['comments'] as $comment)
foreach($comment as $profile)
$users[$profile['from']['username']] = $profile['from']['profile_picture'];
print_r($users);
Managed to do it via accessing the meta tags directly, and specifically the <meta property="og:image" tag, with the following:
libxml_use_internal_errors(true);
$c = file_get_contents($instaURL);
$d = new DomDocument();
$d->loadHTML($c);
$xp = new domxpath($d);
and then I can print out the image url with:
foreach ($xp->query("//meta[#property='og:image']") as $el) {
echo $el->getAttribute("content");
}
You can even use it to access other properties, such as og:url and og:description for the specific Instagram user.
This is for all those people who needs to access & get Instagram user data (basic: profile_image, profile_url, profile_description, etc.), without dealing with Instagram's API.
Related
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.
Here is my PHP/JSON code:
$json_url = "http://dailydota2.com/match-api";
$json = file_get_contents($json_url);
$json=str_replace('},
]',"}
]",$json);
$decoded= json_decode($json);
$data=$decoded->matches[0];
foreach ($data as $value) {
print_r($value->team1->logo_url);
}
Now I have the following problem
Notice: Trying to get property of non-object
and
Notice: Undefined property: stdClass::$team1
I just want use foreach loop and then show my results in HTML.
Why I am getting the 2 mentioned problems and how can I show the correct results?
Ok so the url your are using return VALID JSON, no need to change any of it!
I suggest using arrays, it has always appeared simpler to me
Do you want the logo_url from team or image_url from league? I will show both in my implementation.
So here is some corrected code
$json_url = "http://dailydota2.com/match-api";
$json = file_get_contents($json_url);
$decoded= json_decode($json,true); // True turns it into an array
$data = $decoded['matches'];
foreach ($data as $value) {
//I am not sure which one you want!!!
echo $value['league']['image_url'] . "<br>";
echo $value['team1']['logo_url'] . "<br>";
echo $value['team2']['logo_url'] . "<br>";
}
*EDIT To show wanted implementation by questions author...
$json_url = "http://dailydota2.com/match-api";
$json = file_get_contents($json_url);
$decoded= json_decode($json,true); // True turns it into an array
$data = $decoded['matches'];
foreach ($data as $value) {
echo "
<img src=\"http://dailydota2.com/{$value['team1']['logo_url']}\">
<img src=\"http://dailydota2.com/{$value['team2']['logo_url']}\">
";
}
I have checked your code and have some notes and hopefully a solution:
1- You are trying to get non existing key from JSON data, that is the message telling you.
2- I am still not sure what do you get from the JSON API. But regarding to dailydota2 documentation there is nothing called image_url under team1. I guess you are looking for logo_url or something like that.
3- Do not change the format of JSON as you do in your code, therefore delete following line:
$json=str_replace('}, ]',"} ]",$json);
Just leave the main JSON output from API as default.
4- When you try to get specific key from the decoded JSON/Array just use following way:
$data = $decoded->{'matches'};
in stead of
$data=$decoded->matches[0];
Reference: http://php.net/manual/en/function.json-decode.php
5- And finally your foreach loop is working but needs the correct key:
foreach ($data as $value) {
print_r($value->team1->logo_url);
}
When all these step is done, it should works.
Here is your final corrected code:
$json_url = "http://dailydota2.com/match-api";
$json = file_get_contents($json_url);
$decoded = json_decode($json);
$data = $decoded->{'matches'};
foreach ($data as $value) {
print_r($value->team1->logo_url);
echo '<img src="http://dailydota2.com/' . $value->team1->logo_url . '">';
}
It returns following output, and I do not get any errors.
/images/logos/teams/cdecgaming.png/images/logos/teams/teamempire.png
/images/logos/teams/ehome.png/images/logos/teams/ehome.png
/images/logos/teams/fnatic.png/images/logos/teams/cloud9.png
/images/logos/teams/teamissecret.png/images/logos/teams/teamissecret.png
/images/logos/teams/natusvincere.png/images/logos/teams/fnatic.png
Again I really do not know which information you want to get from the API but here you have a base of working code that you can work further with to get the required data from the right KEYs.
I am trying to get discriptiobn from youtube using api .... but i dont no where am i wrong its not getting for single video . but if i try playlist its working fine, not with video id
Here is my code
error_reporting(E_ALL);
$feedURL = 'https://gdata.youtube.com/feeds/api/playlists/'.$id.'?v=2&prettyprint=true';
$sxml = simplexml_load_file($feedURL);
echo $feedURL.'</br>';
foreach ($sxml->entry as $entry)
{
echo $media->group->description;
}
Above code is working with playlist ... but if i try one video its not working:
error_reporting(E_ALL);
$feedURL = 'http://gdata.youtube.com/feeds/api/videos/'.$id.'?v=2&alt=json&prettyprint=true';
$sxml = simplexml_load_file($feedURL);
echo $feedURL.'</br>';
foreach ($sxml->entry as $entry)
{
echo $media->group->description;
}
If you call YouTube api with 'alt=json' parameter, the response will be formatted as JSON not XML. So you should use:
$response = json_decode(file_get_contents('... API URL ...'), true);
And then 'description' can be accessed with:
$response['entry']['media$group']['media$description']['$t']
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'];
I'm trying to get the video data from this youtube playlist feed and add the interesting data to an array and use that later, but as you can see from the feed some videolinks are "dead" and that results in problems for my code.
The error I get is "Node no longer exists" when I try to access $attrs['url']. I've tried for hours to find a way to check if the node exists before I access it but I have no luck.
If anyone could help me to either parse the feed some other way with the same result or create a if-node-exists check that works I would be most happy. Thank you in advance
$url = 'http://gdata.youtube.com/feeds/api/playlists/18A7E36C33EF4B5D?v=2';
$sxml = simplexml_load_file($url);
$i = 0;
$videoobj;
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// get video player URL
$attrs = $media->group->player->attributes();
$videoobj[$i]['url'] = $attrs['url'];
// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$videoobj[$i]['thumb'] = $attrs['url'];
$videoobj[$i]['title'] = $media->group->title;
$i++;
}
if ($media->group->thumbnail && $media->group->thumbnail[0]->attributes()) {
$attrs = $media->group->thumbnail[0]->attributes();
$videoobj[$i]['thumb'] = strval($attrs['url']);
$videoobj[$i]['title'] = strval($media->group->title);
}
SimpleXML's methods always return objects, which are themselves linked to the original document (some internal thingy related to libxml.) If you want to store that data for later use, cast it as a string, like this:
$videoobj[$i]['url'] = (string) $attrs['url'];
$videoobj[$i]['thumb'] = (string) $attrs['url'];
$videoobj[$i]['title'] = (string) $media->group->title;