Google+ API - get User ID by e-mail? - php

It's just a few moments Google published its API for Google+ social network
But how can I get numeric ID of user ? Or am I forced to use oAuth?
I know it's written there in URL when you access your/foreign profile page, but how to do it programmaticaly with
info:
http://developers.google.com/+/api/latest/people/get
http://developers.google.com/+/api/oauth

(I'm using the Python API)
When using OAuth you can use the string 'me' instead of userId, that way you can retrieve the public content of the authenticated user:
print service.activities().list(userId='me',collection='public').execute()
I assume you are using PHP API (looking at your tags), try putting 'me' instead of userId.
Edit:
When retrieving the authenticated users' profile, you can get the userId from the "id" field (JSON response taken from http://developers.google.com/+/api/):
{
"kind": "plus#person",
"id": "118051310819094153327",
"displayName": "Chirag Shah",
"url": "https://plus.google.com/118051310819094153327",
"image": {
"url": "https://lh5.googleusercontent.com/-XnZDEoiF09Y/AAAAAAAAAAI/AAAAAAAAYCI/7fow4a2UTMU/photo.jpg"
}
}

This is what you can do in php
$client->setScopes(array('https://www.googleapis.com/auth/plus.me','https://www.googleapis.com/auth/userinfo.email'));
$url = 'https://www.googleapis.com/oauth2/v1/userinfo';
$request = $client->getIo()->authenticatedRequest(new apiHttpRequest($url));
if ($request->getResponseHttpCode() != 200) {
throw new apiException("http code: " . $request->getResponseHttpCode() . ", response body: " . $request->getResponseBody());
}
$temp = json_decode($request->getResponseBody(), true);
print_r($temp);

Related

How to capture POST request from another web service

I am using Block.io Web Hooks. It says that the notification service requires to communicate through POST requests. It also stated that all notification events will use the following JSON objects structure:
{
"notification_id" : "...", // a unique identifier issued when you created the notification
"delivery_attempt" : 1, // number of times we've tried deliverying this object
"type" : "...", // the type of notification, helps you understand the data sent below
"data" : {
... // the notification data
},
"created_at" : 1426104819 // timestamp of when we created this notification
}
I have provided my callback URL and I saw there is row inserted into my database but the value is blank. Yes I know that my code will insert blank but when I trigger the API it is also insert blank.
<?php
$post = '';
foreach($_POST as $k => $v){
$post .= $k.'='.$v.'&';
}
// save data into database
?>
The webhook returns json and this will not be parsed to the $_POST array by PHP.
Instead, you need to get the string from:
file_get_contents('php://input')
This you can parse yourself. To get an array:
$array = json_decode(file_get_contents('php://input'), true);

i am using a rest API i need to parse a forever changing json result

Okay so here goes i am using a rest api called strichliste
i am creating a user credit payment system
i am trying to grab a users balance by username problems is
my restapi i can only get the blanace via its userid
I have created a bit of php that grabs all the current users and the corresponding id and balance using this below
function getbal(){
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://example.io:8081/user/'
)
);
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
print_r($resp);
}
this is the resulting respinse i get after using this in my main php script
<? getbal(); ?>
result --- #
{
"overallCount":3,
"limit":null,
"offset":null,"entries":[
{"id":1,
"name":"admin",
"balance":0,
"lastTransaction":null
},
{"id":2,
"name":"pghost",
"balance":0,
"lastTransaction":null
},
{"id":3,
"name":"sanctum",
"balance":0,
"lastTransaction":null
}
]
}
as you can see there are only currently 3 users but this will grow everyday so the script needs to adapt to growing numbers of users
inside my php script i have a var with the currently logged in use so example
$user = "sanctum";
i want a php script that will use the output fro gatbal(); and only output the line for the given user in this case sanctum
i want it to output the line in jsondecode for the specific user
{"id":3,"name":"sanctum","balance":0,"lastTransaction":null}
can anyone help
$user = "sanctum";
$userlist = getbal();
function findUser($u, $l){
if(!empty($l['entries'])){
foreach($l['entries'] as $key=>$val){
if($val['name']==$user){
return $val;
}
}
}
}
This way, once you have the list, and the user, you can just invoke findUser() by plugging in the userlist, and the user.
$userData = findUser($user, $userlist);
However, I would suggest finding a way to get the server to return only the user you are looking for, instead of the whole list, and then finding based on username. But thats another discussion for another time.

how to get top article based on number of like in mediawiki

I'm relatively new to mediawiki and has just started last week.
Anyone can point me to the correct direction of getting the top article (based on number of likes) in mediawiki? I've already implemented the fblikebutton extension on every article and managed to retrieve the number of likes for each article.
Code that I used to check the number of likes in each article on different URLS
$query_for_fql = "SELECT like_count FROM link_stat WHERE url = '".$full_url."'";
$fqlURL = "https://api.facebook.com/method/fql.query?format=json&query=" . urlencode($query_for_fql);
$response = file_get_contents($fqlURL);
$json_data = json_decode($response);
$fb_like_count = $json_data[0]->like_count;
echo 'Facebook Like:'.$fb_like_count .'<br/>';
eg:
example.com/wiki/index.php?title=ABC (1 like)
example.com/wiki/index.php?title=XYZ (2 likes)
I tried this but this is not working
$highest = 0;
while($highest < $fb_like_count)
{
if($fb_like_count > $highest) //if loop at first
{
$highest = $fb_like_count;
echo "highest value is " . $highest . '<br/>';
}
}
I want to retrieve the content in example.com/wiki/index.php?title=XYZ and display in the "Top Article Page". What should I do next after retrieving the number of likes for each article on each url. The extensions I found for top articles are based on the number of views. But I want to classify the top article based on number of likes.
Thanks a million for helping!
As I said in my comment to you question, FQL is deprecated, and the https://api.facebook.com/method/fql.query endpoint as well.
If you want something future-proof, then you should switch to the /?ids={url1},{url2},... endpoint. You can use this one to generate the comma-separated list of URLs inn the forehand, and then retrieve all the shares in one request, for example
GET /?ids=http://www.techcrunch.com,http://www.google.com
returns
{
"http://www.techcrunch.com": {
"og_object": {
"id": "433841427570",
"title": "TechCrunch",
"type": "website",
"updated_time": "2015-05-27T21:31:39+0000",
"url": "http://techcrunch.com/"
},
"share": {
"comment_count": 0,
"share_count": 20914
},
"id": "http://www.techcrunch.com"
},
"http://www.google.com": {
"og_object": {
"id": "381702034999",
"description": "Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for.",
"title": "Google",
"type": "website",
"updated_time": "2015-05-28T07:10:18+0000",
"url": "http://www.google.com/"
},
"share": {
"comment_count": 2,
"share_count": 12340803
},
"id": "http://www.google.com"
}
}
The sorting issue you have is not related to the Facebook API, but PHP.
See
https://developers.facebook.com/docs/graph-api/reference/v2.3/url
Sort JSON object in PHP by a key value
How to sort JSON objects by a certain key's value

Facebook API - Get friends hometown

Im looking to retrieve all of my friends hometowns, I am using similar PHP as that used to retrieve their names. I think the code im using should work but I think because some users havent set a hometown it wont work and says the for each argument is invalid (although this could be completely wrong).
When simply typing in the URL to the web browser I get the following structure:
{
"id": "z",
"friends": {
"data": [
{
"name": "x",
"hometown": {
"id": "x",
"name": "x"
},
"id": "x"
},
Which is then repeated for every user in the friend list, although for friends without a hometown the following is omitted:
"hometown": {
"id": "x",
"name": "x"
},
To retrieve the name and birthday I have used the following PHP:
$response2 = curl_exec($ch2);
$user2 = json_decode($response2, true);
$user3=$user2{'friends'};
$user4=$user3['data'];
echo ("<h2>Friend List</h2><br>");
foreach($user4 as $friend) {
echo $friend["name"] . "<br />" . $friend["birthday"] . "<br /><br />";
}
}
PS I know the variable names arent particularly good but I will change them once ive figured out what im doing for the hometown!
Thanks a lot for any help in advance.
Not everyone makes their hometown or location available to their friends. In that case you won't be able to get the data over the API and need to check for that.
To get a bit more data than just querying for the hometown, we also read out the current_location.
Through the Graph API you would use this resource: https://graph.facebook.com/me/friends?fields=hometown,location
If you are using FQL you can use a query like this:
SELECT uid, first_name, last_name, current_location, hometown_location, friend_count, mutual_friend_count FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
Remember that you need the friends_location and friends_hometown permission to read these fields.

Instagram Real time API - PHP - Unable to get POST Update

I'm trying to connect with the instagram API, the connection works fine and I am receiving the updates just as described in the API documentation, the issue is that I cannot access to the data send to my callback function.
According to the doc
When someone posts a new photo and it triggers an update of one of your subscriptions, we make a POST request to the callback URL that you defined in the subscription
This is my code :
// check if we have a security challenge
if (isset ($_GET['hub_challenge']))
echo $_GET['hub_challenge'];
else // This is an update
{
// read the content of $_POST
$myString = file_get_contents('php://input');
$answer = json_decode($myString);
// This is not working starting from here
$id = $answer->{'object_id'};
$api = 'https://api.instagram.com/v1/locations/'.$id.'/media/recent?client_secret='.INSTA_CLI_SECRET.'&client_id='.INSTA_CLI_ID;
$response = get_curl($api); //change request path to pull different photos
$images = array();
if($response){
$decode = json_decode($response);
foreach($decode->{'data'} as $item){
// do something with the data here
}
}
}
Displaying the $myString variable I have this result, don't know why it is not decoded to json :(
[{"changed_aspect": "media", "subscription_id": 2468174, "object":
"geography", "object_id": "1518250", "time": 1350044500}]
the get_curl function is working fine when I hardcode my $id.
I guess something is wrong with my $myString, unfortunately the $_POST cvariable is not populated, Any idea what I am missing ?
Looking at the example JSON response included in your question, I can conclude that the object you are trying to talk with is wrapped in an array (hence the [ and ] around it in the JSON string).
You should access it using $answers[0]->object_id.
If that doesn't work, you can always use var_dump to check out the data in one of your variables.

Categories