Looking for effiicient way to pull data from facebook graph api - php

I've been recently working with facebook graph api , when I'm pulling user's recents status messages using https://graph.facebook.com/me/statuses?access_token=... all I get is a long list which contains details about the status content, and I want to pull the names of people who liked the status.
The current solution I've been thinking about is using preg match,
I'd like to know if you guys have any other suggestions,
Thanks in advance

I'm not sure what path you're using, but you can fetch the list of users who like a particular status update by requesting the likes connection on an update.
For example, requesting https://graph.facebook.com/10150710880081168/likes yields something like the following for me:
{
"data": [
{
"id": "276700623",
"name": "Vikki Carter"
},
{
"id": "1514365200",
"name": "Darren Bean"
},
{
"id": "539760281",
"name": "Ian Sidaway"
}
],
"paging": {
"next": "https://graph.facebook.com/10150710880081168/likes?format=json&limit=25&offset=25&__after_id=539760281"
}
}
You'll need the ID of the status update, but also the user_status permission from the logged-in user.
EDIT: Iterating over the returned data and assigned users into an array:
<?php
$result = $facebook->api('[status_id]/likes');
$names = array();
foreach ($result['data'] as $user) {
$names[] = $user['name'];
}
You should now have an array of names of users who liked the status in question.

Related

How to set Updates to this data - Laravel/Lumen

I have a web application where the user shall be able to change MariaDB records via a GUI inside the browser.
In this GUI, the user sees a list of the records visible for him. Whatever he can see, he has permission to delete or change as well.
To change a DB record, he simply has to click the fields he wants to change inside the list and then press the "send" button.
In his HTTP request, the id associated with the record will be transmitted so the backend can identify the respective record inside the DB and apply the changes.
Now, Im rather new to Laravel/Lumen. To fetch the list the user can apply changes to in the first place, I have the following code:
$join = coretable::with($permittedTables)->get();
The $permittedTables is an array of tablenames, so any number or combination of tables might be joined to coretable.
For example, a fetch can look like this:
[{
"Internal_key": "TESTKEY_1",
"extensiontable_itc": {
"description": "EXTENSION_iTC_1"
},
"extensiontable_sysops": {
"description": "EXTENSION_SYSOPS_1"
}
}, {
"Internal_key": "TESTKEY_2",
"extensiontable_itc": {
"description": "EXTENSION_ITC_2"
},
"extensiontable_sysops": {
"description": "EXTENSION_SYSOPS_2"
}
}, {
"Internal_key": "TESTKEY_3",
"extensiontable_itc": {
"description": "EXTENSION_ITC_3"
},
"extensiontable_sysops": {
"description": "EXTENSION_SYSOPS_3"
}
}, {
"Internal_key": "TESTKEY_4",
"extensiontable_itc": {
"description": "EXTENSION_ITC_4"
},
"extensiontable_sysops": {
"description": "EXTENSION_SYSOPS_4"
}
}, {
"Internal_key": "TESTKEY_5",
"extensiontable_itc": {
"description": "EXTENSION_ITC_5"
},
"extensiontable_sysops": {
"description": "EXTENSION_SYSOPS_5"
}
}]
Now, I wondered if I could just reuse the code I've created to fetch the data for setting the data.
So I used the above shown $join and tried to determine the datarecords I want to change.
The attempt looks like this:
$join = $join->find("TESTKEY_1");
The find however doesnt return anything.
Considering the structure of the results I'm querying here, is this approach even feasible? Or should I build some new code, fetching the results in a different structure, better suited to have changes applied to it?
Still, is there a way to search these fetchresults for a subset of data and then apply changes to this subset (and persist those changes to the DB, of course)?
$join = $join->find("TESTKEY_1");
find method works only for ids.
e.g. $join = $join->find(1); //1 is id (primary_key) in here.
if you want to find the record by TESTKEY_1
$join = $join->where("Internal_key","TESTKEY_1")->get();
// will return array of all records having Internal_key TESTKEY_1
if you want only the first record to be fetched
$join = $join->where("Internal_key","TESTKEY_1")->first();
// will return only first having Internal_key TESTKEY_1 as an array.

How to get other pages followers count number in Instagram?

is there possibility to get other pages follower count number in Instagram?
I can get only my profile followers count number, but I want to get other followers too? (for example in php)
Any ideas?
Yes, it's possible with ?__a=1 queries which return json.
$otherPage = 'nasa';
$response = file_get_contents("https://www.instagram.com/$otherPage/?__a=1");
if ($response !== false) {
$data = json_decode($response, true);
if ($data !== null) {
$follows = $data['graphql']['user']['edge_follow']['count'];
$followedBy = $data['graphql']['user']['edge_followed_by']['count'];
echo $follows . ' and ' . $followedBy;
}
}
Jumping through hoops to get an app approved for quickly checking follower counts seemed excessive so I had a look at network requests on Instagram's web search and saw that typing in the search box fires off requests to this URL:
https://www.instagram.com/web/search/topsearch/?query={searchquery}
That returns a JSON feed which has a bunch of info on the user including a value for follower_count. If you are logged in, the results are weighted by relevance to your account, but you don't have to be authenticated or even logged in to get a result:
{
"has_more": false,
"status": "ok",
"hashtags": [],
"users": [
{
"position": 0,
"user": {
"username": "thenativepaul",
"has_anonymous_profile_picture": false,
"byline": "457 followers",
"mutual_followers_count": 0.0,
"profile_pic_url": "https://scontent-lhr3-1.cdninstagram.com/t51.2885-19/11373838_482400848607323_1803683048_a.jpg",
"full_name": "Paul Almeida-Seele",
"follower_count": 457,
"pk": 21072296,
"is_verified": false,
"is_private": false
}
},
{
"position": 1,
"user": {
"username": "the_native_warrior",
"has_anonymous_profile_picture": false,
"byline": "251 followers",
"mutual_followers_count": 0.0,
"profile_pic_url": "https://scontent-lhr3-1.cdninstagram.com/t51.2885-19/s150x150/14473927_312669442422199_4605888521247391744_a.jpg",
"profile_pic_id": "1352745514521408299_1824600282",
"full_name": "Paul Carpenter",
"follower_count": 251,
"pk": 1824600282,
"is_verified": false,
"is_private": true
}
}
],
"places": [ ]
}
Using yahoo query language
https://query.yahooapis.com/v1/public/yql?q=select * from html where url='https://www.instagram.com/USERNAME/' and xpath='/html/body/script[1]'&format=json
you can get any user's followers count but you cant get the user's followers details, you can only get your follower list.
This is API to get follower count of any user:
https://api.instagram.com/v1/users/{user-id}/?access_token=ACCESS-TOKEN
https://www.instagram.com/developer/endpoints/users/#get_users
UPDATE: I noticed that didn't work and I just tried this: https://api.instagram.com/v1/users/self/?access_token=XXXXX and got some good info ... MQ
There are a few analytics tools that allow you to get the follower count without an access token other than your own. If you login to Crowdbabble and add an Instagram account, you will be asked to login to your Instagram account. After that, though, you can add any Instagram account and get the number of followers. This will give you the follower list, most engaging followers, and most influential followers for any account.
If you apply for the Instagram API (now requires a written application and a video submission), you can set up an app like this yourself. You can make calls to different accounts using just one access token.
I've used yahoo API, in my case I figured out like this,
hope this helps someone because I've searched a lot for this.
$url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url=%27https://www.instagram.com/USERNAME/%27%20and%20xpath=%27/html/body/script[1]%27&format=json";
$json = file_get_contents($url);
$obj = json_decode($json, true);
$content = $obj['query']['results']['script']['content'];
$content = str_replace("window._sharedData =", "", $content);
$content = str_replace(";", "", $content);
$content = trim($content);
$json = json_decode($content);
$instagram_follower_count = $json->entry_data->ProfilePage{0}->user->followed_by->count;
anyone know better way get directly with JSON object, without replacing special characters ? please correct me if yes.

YouTube API v3 get latest video uploads for a username with PHP

I am trying to get a list of a user's latest videos/uploads. I am trying to get my head around this new v3 API, but I cannot find any properly working PHP code examples, not even on the dedicated YouTube API website.
I have a list of YouTube usernames, and I just want to simply get a list of their latest videos with video ID, title, thumbnail URL, etc. Basically in a foreach loop.
Any advice?
I have the following code, I need to make it work by the actual YouTube usernames.
$url = 'https://www.googleapis.com/youtube/v3/channels?part=id&forUsername=ferrariworld&key=ZZZZZZZZ';
$json = json_decode(file_get_contents($url), true);
foreach ($json as $item){
echo $item[0]['id'];
echo $item[0]['title'];
}
Use the channels endpoint with a forUsername attribute (see the documentation for the endpoint).
GET https://www.googleapis.com/youtube/v3/channels?part=id&forUsername={USERNAME}&key={YOUR_API_KEY}
It will give you this kind of response:
{
"kind": "youtube#channelListResponse",
"etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/7Q8DJLb6b2hwYAXUQLI3ftt3R8U\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#channel",
"etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/pDjr9xPn51KtRM3gbMOq2aHBIbY\"",
"id": "UCr3LuetcEeidWiuhxaw4B2g"
}
]
}
The channel ID of that user can be found at items[0]['id'] if you convert the JSON to array.
As for your edit, you’re accessing the JSON object incorrectly. You need to use something like this.
if (array_key_exists('items', $json) && count($json['items']) === 1) {
$item = $json['items'][0];
$channelId = $item['id']; // The users channel ID.
}

Parsing FB Graph API data Cleanly

I am currently working to parse through FB api data. Take the following for example...
{
"data": [
{
"id": "ID",
"name": "Creative!"
},
{
"id": "ID",
"name": "a name"
}
],
"paging": {
"cursors": {
"after": "fdsagfsganhdfs==",
"before": "gfdwiolrukjhteqrfgbh"
}
},
"summary": {
"total_count": 2
}
}
This is an example of what is returned when the Graph API is queried for likes. The issue I am having is that I want a clean way to get the total_count out of this data. Often times it will come in without the summary field if there are no 'likes'. This is easily parsed by doing a few if isset() and if array_key_exists but I will be dealing with a lot of data and this use case applies to many different type of data from FB. Any advice on just getting the total_count field? FQL would work but seems to be deprecated. Thanks.
If the total_count field is in fact the sum of the number of elements in the data array then you can simply use the count function in PHP to count the length of the array.
$json_array = json_decode($fb_json_string, true);
$total_count = (isset($json_array['total_count']))? $json_array['total_count']:((isset($json_array['summary']['total_count']))? $json_array['summary']['total_count']:0);
I don't know if I can make it any uglier, it's simply a suggestion though.

How to use Facebook graph API to retrieve fan photos uploaded to wall of fan page?

I am creating an external photo gallery using PHP and the Facebook graph API. It pulls thumbnails as well as the large image from albums on our Facebook Fan Page.
Everything works perfect, except I'm only able to retrieve photos that an ADMIN posts to our page. (graph.facebook.com/myalbumid/photos) Is there a way to use graph api to load publicy uploaded photos from fans?
I want to retrieve the pictures from the "Photos from" album, but trying to get the ID for the graph query is not like other albums... it looks like this: http://www.facebook.com/media/set/?set=o.116860675007039
Another note: The only way i've come close to retreiving this data is by using the "feed" option.. ie: graph.facebook.com/pageid/feed
EDIT:
This is about as far as I could get- it works, but has certain issues stated below. Maybe someone could expand on this, or provide a better solution. (Using FB PHP SDK)
<?php
require_once ('config.php');
// get all photos for album
$photos = $facebook->api("/YourID/tagged");
$maxitem =10;
$count = 0;
foreach($photos['data'] as $photo) {
if ($photo['type'] == "photo"):
echo "<img src='{$photo['picture']}' />", "<br />";
endif;
$count+= 1;
if ($count >= "$maxitem") break;
}
?>
Issues with this:
1) The fact that I don't know a method for graph querying specific "types" of Tags, I had to run a conditional statement to display photos.
2) You cannot effectively use the "?limit=#" with this, because as I said the "tagged" query contains all types (photo, video, and status). So if you are going for a photo gallery and wish to avoid running an entire query by using ?limit, you will lose images.
3) The only content that shows up in the "tagged" query is from people that are not Admins of the page. This isn't the end of the world, but I don't understand why Facebook wouldn't allow yourself to be shown in this data as long as you posted it "as yourself" and not as the page.
You need to retrieve all the albums and then get all the photos. This can be easily done by using FQL:
SELECT pid,owner,src_small,src_big
FROM photo
WHERE aid IN (
SELECT aid
FROM album
WHERE owner = your_page_id
)
EDIT:
Also you need to query the stream table to get the wall posts then check if you have attachments and the type of the attachment:
SELECT attachment
FROM stream
WHERE source_id = 116860675007039
Result:
[
{
"attachment": {
"media": [
{
"href": "http://www.facebook.com/photo.php?fbid=1801693052786&set=o.116860675007039",
"alt": "test",
"type": "photo",
"src": "http://photos-f.ak.fbcdn.net/hphotos-ak-snc6/185861_1801693052786_1553635161_1863491_4978966_s.jpg",
"photo": {
"aid": "6672812206410774346",
"pid": "6672812206412558147",
"fbid": 1801693052786,
"owner": 1553635161,
"index": 11,
"width": 246,
"height": 198
}
}
],
"name": "",
"caption": "",
"description": "",
"properties": [],
"icon": "http://b.static.ak.fbcdn.net/rsrc.php/v1/yz/r/StEh3RhPvjk.gif",
"fb_object_type": "photo",
"fb_object_id": "6672812206412558147"
}
},
{
"attachment": {
"description": ""
}
},
...etc
]
try this FQL:
SELECT message,attachment,comments
FROM stream
WHERE source_id=YOUR_PAGE_ID AND filter_key="others" AND type=""
filter_key="others" - this will filter posts from everyone else, but not from Admin
type="" - this will filter out comments, so will leave you with photos (maybe videos and other strenge things, but removing text posts will reduce result massively)

Categories