Using Twitch's API to get multiple channels with one request - php

I am using Twitch's API to request some details about a user, in particular I need to get their: twitch user, channel, live channel
I am having no problem getting any of their details, I have it set up so I can get a bulk amount of users with one request. The same with their live channels. But with just getting channel information, according to Twitch's API I must send a request per user. I can't see anyway to get a bulk list of channels
For example, using the code posted below, I can get 10-100s of users twitch user data in one simple request
But when I try to send too many requests I get blocked from their API
Code for getting bulk twitch user data
public static function setTwitchUserBulk($users)
{
// Key by their twitch_id
$users = $users->keyBy('twitch_id');
$twitch_key = env('TWITCH_KEY');
$url = 'https://api.twitch.tv/helix/users?';
foreach($users as $user) {
$url .= 'login='.$user->twitch_username.'&';
}
$data = \App\CustomHelper\ZarlachTwitchHelper::_basicCURL($url, array(
'Client-ID' => $twitch_key,
));
$data = json_decode($data, true);
if(isset($data['data'])) {
$data = $data['data'];
foreach($data as $twitchUser) {
if(isset($users[$twitchUser['id']])) {
// ... handle and store their data
}
}
}
}
According to Twitch's API, I must use this URL to get their channel
GET https://api.twitch.tv/kraken/channels/<channel ID>
Where as, when I get their twitch user, I can simply keep adding parameters onto the url to set the users i want to fetch
GET https://api.twitch.tv/kraken/users?login=<user IDs>

Related

How to get email address of shared file user (Google Drive api PHP)

I'm trying to get the role and email address of persons whom I shared my google drive file.
//this->drive is object of service_drive
$permissions = $this->drive->permissions->listPermissions($file->id);
foreach ($permissions->getPermissions() as $permission){
echo $permission['emailAddress'];
}
this is returning me null, is there anyway I can know completely about the person or at least email address and his role ?
Yes, you can get info as the email, name or role of the people you share your files with using the Permissions: list endpoint. Try this API can help you to play around with the info you want to retrieve using the fields parameters, which uses partial responses.
Translating the explanation from above to PHP code, this is what you would need to do:
// Build a parameters array
$parameters = array();
// Specify what fields you want
$parameters['fields'] = "permissions(*)";
// Call the endpoint
$permissions = $service->permissions->listPermissions($file->id, $parameters);
// print results
foreach ($permissions->getPermissions() as $permission){
echo $permission['emailAddress'];
}

PUT / Soundcloud API with cURL

I'm using this code to get a user but how do it "like" a track via a PUT request in cURL & PHP?
// build our API URL
$url = "http://api.soundcloud.com/resolve.json?"
. "url=http://soundcloud.com/"
. "USERNAME-HERE"
. "&client_id=CLIENT-ID-HERE";
// Grab the contents of the URL
$user_json = file_get_contents($url);
// Decode the JSON to a PHP Object
$user = json_decode($user_json);
// Print out the User ID
echo $user->id;
See SoundCloud API Documentation - /users.
You can favourite a track for a user with a PUT request in the following format:
/users/{user_id}/favorites/{track_id}
If you don't know how to make the cURL request, Google it - there are hundreds of tutorials and answers on StackOverflow. Here is one example:
Querying API through Curl/PHP

Get my own full profile with LinkedIn API

For testing purposes, I'd like to get my own full profile datas from LinkedIn API.
So far my code looks like this :
// Fill the keys and secrets you retrieved after registering your app
$oauth = new OAuth("APIKEY", "SECRETKEY");
$oauth->setToken("Token OAuth", "Secret User OAuth");
$oauth->disableSSLChecks();
$params = array();
$headers = array();
$method = OAUTH_HTTP_METHOD_GET;
// Specify LinkedIn API endpoint to retrieve your own profile
$url = "https://api.linkedin.com/v1/people/~:(first-name,last-name,headline,location:(name),skills:(name),educations:(id,school-name,field-of-study))?format=json";
// By default, the LinkedIn API responses are in XML format. If you prefer JSON, simply specify the format in your call
// $url = "https://api.linkedin.com/v1/people/~?format=json";
// Make call to LinkedIn to retrieve your own profile
$oauth->fetch($url, $params, $method, $headers);
$oProfile = json_decode($oauth->getLastResponse());
var_dump($oProfile);
Although I am getting basic profile informations (firstName,headline etc...) but when it comes to full profile informations I get an object with '...' as value everytime, although the informations exist.
I have r_fullprofile ticked in my LinkedIn app interface, so I don't know what I have to do to get these values.
I tried your query with my own account. It looks you issue is with the skills field.
You can see in the LinkedIn API documentation that skills are made up of a skill, and each skill has a name. If you only want the name returned the proper way to ask for it is
skills:(skill:(name)), whereas your request asks for skills:(name).
Here is an updated request for you:
GET https://api.linkedin.com/v1/people/~:(first-name,last-name,headline,location:(name),skills:(skill:(name)),educations:(id,school-name,field-of-study))?format=json

graph api notifications returns empty data

im building a facebook app and i want to notify the user
https://developers.facebook.com/docs/games/notifications
im using facebook php sdk
what i do:
user auths the app and accepts permission
i get the accesstoken like:
$facebook->getAccessToken()
and then i generate a long-time token like:
public function generateLongTimeToken($token){
$long_time_token_req_body = array(
"grant_type"=>"fb_exchange_token",
"client_id"=>$this->facebookOptions["appId"],
"client_secret"=>$this->facebookOptions["secret"],
"fb_exchange_token"=>$token
);
$query = http_build_query($long_time_token_req_body);
$lttreq = file_get_contents("https://graph.facebook.com/oauth/access_token?".$query);
$lttresp = parse_str($lttreq, $output);
if ( array_key_exists("access_token", $output)){
$this->logger->info("Facebook-app: Successfuly generated long_time_token");
return $output["access_token"];
}else {
$this->logger->err("Facebook-app: generating oauth long_time_token failed \n".$lttreq);
return false;
}
}
some later i use this token for background processes to post on the users wall and them all work fine
now i also want to notificate the user like that :
public function notifyUser($message,$facebookClientId,$token){
$appsecret_proof= hash_hmac('sha256', $token, $this->facebookOptions["secret"]);
$req_body = array(
"access_token"=>$token,
"appsecret_proof"=>$appsecret_proof,
"href"=>"/index",
"template"=>$message,
"ref"=>"post"
);
$query = http_build_query($req_body);
$url = "https://graph.facebook.com/".$facebookClientId."/notifications?".$query;
$lttreq = file_get_contents($url);
return $lttreq;
}
but when i try to notify the user i always get empty data back
when i open the url in browser with all parameters facebook returns the same
{
data: [ ]
}
so i have no idea whats going on,when i look on SO i only find about people posting to sites but i want to notify the user itself
thanks for any help
First, from the Facebook docs:
Currently, only apps on Facebook.com can use App Notifications.
Notifications are only surfaced on the desktop version of
Facebook.com.
Also, an App Token is needed, not a User Token.
Btw, file_get_contents is very bad, use CURL for Facebook. May be another reason why it does not work. A basic example of using CURL with the Facebook API: http://www.devils-heaven.com/extended-page-access-tokens-curl/
Additional Info: I recently wrote a blogpost about App Notifications, it is in german but the small code part may be interesting for you: http://blog.limesoda.com/2014/08/app-notifications-facebook-apps/

Access to page's photo albums from within web app using tokens?

Im using Facebook's PHP sdk for the first time in a clients site and I'm a little confused. I'm trying to get some data from the clients facebook page and inject it into their site. One of the objects I am trying to get is a list of the clients photo albums for a photo gallery.
<?php
$albums = $Facebook->api("/169639127532/albums?fields=id,name,cover_photo");
$photos = array();
$queries = array();
foreach($albums['data'] as $album) {
if(array_key_exists("cover_photo", $album))
$queries[] = array("method" => "GET", "relative_url" => "/".$album['cover_photo']."?fields=source");
}
$rawPhotos = $Facebook->api('?batch='.json_encode($queries), 'POST');
foreach($rawPhotos as $photo) {
//print_r(json_decode($photo['body'])); echo "<br /><br />";
$photo = json_decode($photo['body']);
$photos[$photo->id] = $photo->source;
}
As you can see, im getting a list of the albums, then using a batch request to get all the cover photos for those albums.
Using my clients page ID, this doesn't work, nothing is returned. However if I use another page's ID this works.
So I put this down to authorisation. I noticed that if I used the Graph API explorer with an access token I could get the photos. Because of this, I copied the Token I had generated with the API explorer, and set it globally on the site using the following function and this works.
$Facebook->setAccessToken($token)
Obivously this isn't a very robust solution as the tokens only exist for a finite amount of time. I tried getting a token for the app using the /oauth/access_token API call with my apps details and successfully got a token back. If I use this as the token I can no longer see the data I was getting with the temporary token from the graph explorer however.
I do not want users to have to login just in order to see the photo gallery on the site.
How do I go about getting a token for the app that would allow me to do this?
Thanks.

Categories