Can't display Twitch API data using PHP - php

I want to display the last 100 followers on Twitch using their API on my website. However, I know very little about JSON.
Currently I have this:
$string = file_get_contents("https://api.twitch.tv/kraken/channels/pewdiepie/follows.json?limit=100");
$json=json_decode($string,true);
But I can't get it in a while loop.
I tried this:
foreach ($json as $key => $value){
echo $value['name'];
}
Can someone help me? I just want the last 100 follower names displayed on the page.

Assuming that getting the data works, this should do it:
foreach ($json['follows'] as $follow){
echo $follow['user']['name'];
}

Related

Right way of browsing Instagram API result array of images with foreach

I've been recently building a plugin for wordpress which basically uses the instagram API to get an image URL and then place it in a short code.
And I've come to a problem.
I get this error:
E_WARNING : type 2 -- Invalid argument supplied for foreach() -- at
line 22
and I have no idea what am I doing wrong.
My code for the foreach:
//define Access token
$accesst= "ACCESS_TOKEN_GOES_HERE";
//userid
$userid=USERID_GOES_HERE;
//image count to get
$count=20;
//get api contents
$content = file_get_contents('https://api.instagram.com/v1/users/self/media/recent/?access_token='.$accesst.'&count='.$count);
//converting JSON to object
$standardres = json_decode($content, true);
//array method
foreach($standardres['data'][0]['images']['standard_resolution']['url'] as $photo)
{
print $photo['url'][0];
echo "<br>";
}
My JSON var_dump got me this:
https://pastebin.com/3RaL6EUA
The access codes, were of course deleted before posting this.
Does anyone have a clue what I'm doing wrong?
EDIT:
Thanks, everyone, got it figured out in the comments.
Your $standardres['data'] have items which have images, so you must use $standardres['data'] in the foreach loop and then parse the image url from the item data.
foreach($standardres['data'] as $item) {
print $item['images']['standard_resolution']['url'];
echo "<br>";
}
I don't know exactly the hierarchy of the instagram API, but I suggest you to try :
foreach($standardres['data']['images'] as $photo) {
print_r($photo); // to see what the array contains!
echo $photo['standard_resolution']['url'];
echo "<br>";
}

how to show multiple photo twitter status using php

i need to display/show photo with twitter api, but i have only get 1 photo
$results = $toa->get('https://api.twitter.com/1.1/statuses/show.json?id=xxxxxxxxxxxx' );
if (isset($results->entities->media)) {
foreach ($results->entities->media as $media) {
$media_url = $media->media_url; // Or $media->media_url_https for the SSL version.
}
}
echo $media_url;
this code get only 1 photo ,how to get pulling multiple image (max 4) on 1 twitter status ?
thank
It looks like you're overwriting the $media_url variable on each loop. You would want to loop through the $results->entities->media variable when displaying the images like this:
foreach ($results->entities->media as $media) {
echo '<img src="'.$media->media_url.'"/>';
}
I don't know too much about the Twitter API however you are only getting one photo at the end because you are doing this.
Inside the loop you keep updating $media_url to $media->media_url. Then outside the loop you echo $media_url only once.
All you are doing is changing that one variable over and over, you need to have your echo statement inside the loop.
if (isset($results->entities->media)) {
foreach ($results->entities->media as $media) {
$media_url = $media->media_url;
echo $media_url;
}
}
Like I said I don't have too much knowledge of the Twitter API but providing your loop is working to get one of the images then moving the echo statement inside the loop should help.

Evernote API show all note in a notebook

I'm trying to get all of the notes from a particular evernote notebook. I am able to display all of the data as an array, and I'm trying to use a foreach loop to get the title. I also want to be able to get the content, date, etc.
$filter = new NoteFilter();
$filter->notebookGuid = $notebookGuid;
$notelist = $client->getNoteStore()->findNotes($authToken, $filter, 0, 100);
foreach($notelist as $value) {
echo $value->title;
}
I know that I'm being really stupid, but I'm new to php and evernote. Any help is appreciated!
The return value of NoteStore.findNotes is NoteList which is not a collection. You have to get notes attribute from NoteList and then iterate it.
By the way, findNotes is now deprecated so please use findNotesMetadata.
You might want to check the following example from evernote:
https://github.com/evernote/evernote-sdk-php/blob/master/sample/client/EDAMTest.php

Rate limit. Twitter API

I'm working on a small and simple code which basically does some tweets filtering. The problem is that I'm hitting the request limit of Twitter API and I would like to know if there is a workaround or if what I want to do just cannot be done.
First, I type a twitter username to retrieve the ID's of people this user follows.
$user_id = $_GET["username"];
$url_post = "http://api.twitter.com/1/friends/ids.json?cursor=-1&screen_name=" . urlencode($user_id);
$following = file_get_contents($url_post, true);
$json = json_decode($following);
$ids = $json->ids;
Twitter API responds with a list of ID's.
Here comes the problem. The next step is to make a request to find out username, profile picture and description for each one of those ID's.
$following = array();
foreach ($ids as $value)
{
$build_url = 'http://api.twitter.com/1/users/lookup.json?user_id=' . $value . '';
$following[] = $build_url;
}
foreach ($following as $url)
{
$data_names = file_get_contents($url, true); //getting the file content
$json_names = json_decode($data_names);
foreach ($json_names as $tweet) {
$name = $tweet->name;
$description = $tweet->description;
echo '<p>';
echo $name . '<br>';
echo $description;
echo '</p>';
}
}
If the user follows 50 people it works. But if he follows, let's say, 600 hundred, that would be 600 hundred request (for username, description and profile pic) to Twitter API which exceeds the limit.
Is there any way to workaround this o it just cannot be done?
Thank you!
You can and should request users/lookup API endPoint with 100 userIds at a time, instead of doing one request per twitter ID. cf. https://dev.twitter.com/docs/api/1.1/get/users/lookup
You have to replace your forEach loop (foreach ($following as $url)) by a recursive function.
At the end of the function, check the number of hits remaining before calling it again (cf. this link to see how to know the time remining until you get rate limited).
If there is no hit left, sleep 15 minutes before calling the function again, otherwise do the call again.
There is plenty of information on how to do this, use Google and search existing stackOverflow questions.

Displaying a JSON array in PHP (from freebase)

This is a very basic question, so excuse my lack of knowledge.
I'm trying to output a JSON query from Freebase in PHP. I've already been able to parse the JSON into PHP using cURL and json_decode.
Here is a link to the JSON array (for some reason I can't get this to link directly):
http://www.freebase.com/api/service/mqlread?query={%20%22query%22%3A%20[{%20%22type%22%3A%20%22%2Fpeople%2Fperson%22%2C%20%22ns0%3Atype%22%3A%20%22%2Fbase%2Fbillionaires%2Fbillionaire%22%2C%20%22employment_history%22%3A%20[{%20%22company%22%3A%20null%20}]%2C%20%22name%22%3A%20null%20}]%20}
I'm able to ouput the first level of the array (Bill Gates), but not the 2nd level (Microsoft).
I've figured out how to display and loop through the people's names, just not their associated companies.
So my code, thus far, gets me a list of names.
$results = json_decode($response)->result;
foreach ($results as $name) {
echo $name->name . '<br/>';
I want the companies associated with each name to be displayed.
The browser-format should be:
Person 1 Name:
Company Name 1
Company Name 2
etc.
Person 2 Name:
Company Name 1
etc.
Thanks for any pointers--I'm sure that I'm just missing the simple way to structure the PHP code to display this easily.
How about:
$results = json_decode($response)->result;
foreach ($results as $person) {
echo $person->name . '<br/>';
foreach($person->employment_history as $employer) {
echo $employer->company . '<br/>';
}
echo '<hr />'; // horizontal rule for good measure
}

Categories