On the Internet there are a few topics about this problem, but I have not found any complex solution. Therefore, I would like to ask you for help.
I need to change facebook id to username.
When you type web site like this:
http://facebook.com/profile.php?id=4 (num 4 is FB id), it will give you http://www.facebook.com/zuck, which is Mark Zuckerberg's profile.
On this principle I need to find out who a id is.
I have typed id 4 a got it is zuck.
But I need it for more ids, so it would take a lot of time do it manually. Please help me, how I can do it.
If you already have an ID of that particular user, then just add it on this url:
https://graph.facebook.com/<USER_ID>
Simple example:
function get_basic_info($id) {
$url = 'https://graph.facebook.com/' . $id;
$info = json_decode(file_get_contents($url), true);
return $info;
}
$id = 4;
$user = get_basic_info($id);
echo '<pre>';
print_r($user);
This should basically yield:
Array
(
[id] => 4
[first_name] => Mark
[gender] => male
[last_name] => Zuckerberg
[link] => https://www.facebook.com/zuck
[locale] => en_US
[name] => Mark Zuckerberg
[username] => zuck
)
Then you could just call it like a normal array:
echo $user['username'];
Sidenote: Why not use the PHP SDK instead.
https://developers.facebook.com/docs/reference/php/4.0.0
As the username is NO more available from Graph API endpoint /user-id as discussed here, I propose another workaround here (but with Python code)
In brief, we open the page at fb.com/USER_ID and scrape the username from it
#get html of a page via pure python ref. https://stackoverflow.com/a/23565355/248616
import requests
r = requests.get('http://fb.com/%s' % FB_USER_ID) #open profile page of the facebook user
r.raise_for_status()
html = r.content
#search string with regex ref. https://stackoverflow.com/a/4667014/248616
import re
# m = re.search('meta http-equiv="refresh" content="0; URL=/([^?]+)\?', html)
m = re.search('a class="profileLink" href="([^"]+)"', html)
href = m.group(1) #will be https://www.facebook.com/$FB_USER_NAME on 201705.24
username = href.split('/')[-1]
print(href)
print(username)
As of October 2019, The only way it is possible is to request for user_links in request and permissions
And then pass it in a scope, something like this
scope: ['user_link']
Related
I try to make dhru api based on this code https://github.com/dhru/dhru-fusion-api-standards .
My problem is to get customer input on web which is sent by dhru web in parameters.
I already determinated what exacly website send to my API:
[username] => login
[apiaccesskey] => pass
[action] => placeimeiorder
[requestformat] => JSON
[parameters] => <PARAMETERS><ID>100</ID><CUSTOMFIELD>eyJ1ZGlkIjoiMDAwMDgwMjAtMDAxQzM0MTIyNjgxMDAyRSJ9</CUSTOMFIELD><MODELID></MODELID><PROVIDERID></PROVIDERID><NETWORK></NETWORK><PIN></PIN><KBH></KBH><MEP></MEP><PRD></PRD><TYPE></TYPE><LOCKS></LOCKS><REFERENCE></REFERENCE><IMEI>423545675567658</IMEI><udid>00008020-001C34122681002E</udid></PARAMETERS>
But i'm unable to get any value from this parameters.
I already tried all of this but nothing work:
1) $udid = json_decode($parameters['udid'],true);
2) $udid = $parameters['udid'];
3) $imei = $parameters['IMEI'];
4) $udid = (string)$parameters['udid'];
Can somebody give me advice?
Ok so yesterday I was working on a PHP API all day (2:30PM - 3:30AM CT).
The user/client/front end of the API can submit a filename or link to the API so it can view it, edit it, or delete it.
But that's a problem because if the user can do that that means they could edit or delete the website with the client.
So I'm wondering can I store a bunch of variables that are equal to links, so when the client submits, the API will see if that link is unauthorized or authorized
Could you do it like this
<?php
$myArray = array( //Unauthorized Links
$link1 => "http://example.com" //First Link
);
?>
Then check the links using $myArray?
If you need the API code I'll provide it.
use this code and it will work for you
<?php
$myArray = array(
1 => 'http://example-1.com',
2 => 'http://example-2.com',
3 => 'http://example-3.com',
4 => 'http://example-4.com'
);
foreach($myArray as $key => $value){
echo 'link '.$key.' = '.$value.' <br>';
}
?>
I'm using the twitter api to get my latest mentions.
Twitter api link: https://dev.twitter.com/rest/reference/get/statuses/mentions_timeline
Function used to get last mentions:
function twitter_get_tweets(){
$connection = $this->twitter_auth();
$latest_tweet = $connection->get('statuses/mentions_timeline', array('count' => 1));
return $latest_tweet;
}
Preview of response: http://projects.helpful.ninja/twitter/
Sadly im not able to access the variables in there directly, such as the text node.
Example:
[text] => #pr0b_ Submit a request to our Support team and they'll be in touch ASAP
Been testing something like:
$twitter_bot = new twitter_bot_functions($twitter_settings['api_key'], $twitter_settings['api_secret'], $twitter_settings['access_token'], $twitter_settings['access_token_secret']);
$get_latest_mention = $twitter_bot->twitter_get_tweets();
echo $get_latest_mention['text'];
Does someone have a idea what I'm doing wrong?
It is an array first and then a stdClass object. So you should use it like
echo $get_latest_mention[0]->text;
I'm looking for retrieve all of the friends who use the same app (PHP SDK 4.), with the uid,username, and score
I have this permission for my app:
('email', 'user_about_me', 'user_friends', 'publish_actions',
'user_games_activity')
now, I'm working with this script that retrive just the uid of the user who use the same app:
$me = (new \Facebook\FacebookRequest($session, 'GET', '/me/friends'))->execute()->getGraphObject(\Facebook\GraphUser::className());
$result = $me->asArray();
// Get user's friends
$friends = $result['data'];
// Converting classes to array
foreach ($friends as $key => $value) {
$friends[$key] = (array)$value;
echo $friends[0]['id']; //show the id of the user 0
echo "|";
}
and I have a problem just for check all of the friends, I try to change this script, but I can't get all friends, but just the first one... [0]
now, I search a lot about this topic... but I find just for the old SDK, with a new, what I should do to retrive the uid,username and score of the same app? like this:
echo $friends[0]['id']; //show the id of the user 0
echo "|";
echo $friends[0]['name']; //show the name of the user 0
echo "|";
echo $friends[0]['score']; //show the score of the user 0
echo "|";
[number_uid0] | [nameusername0] | [score0] | [number_uid1] | [nameusername1] | [score1] etc....
Thank you very much :)
You can't get ALL friends anymore, only those which also use your app. And all friends_* permissions have been removed, so you can't simply retrieve your friends' scores...
But have a look here to find a solution to (what I think is) your problem:
https://developers.facebook.com/docs/games/scores#read-many-scores
You can read the set of scores for a player and their friends by issuing an HTTP GET request to /APP_ID/scores with the user access_token for that app. The user_friends permission is required in order to view friends' scores. This returns a list of scores for a player and friends who have authorized the app. The list is sorted by descending score value, so it returns friends with the highest scores first. You can use this call to generate a leader board for a player and friends.
I am trying to get information by json_decode from Google maps API, but for some reason I get a strange output. Can someone help me with this?
$request = file_get_contents ('https://maps.googleapis.com/maps/api/geocode/json?address=meerpaal%208d%20oosterhout&sensor=true');
$data = json_decode( $request, true );
foreach ( $data['results'][0]['geometry'] as $value ) {
echo $value['lat'] . '<br />';
echo $value['lng'] . '<br />';
}
This is my output:
51.6303446
4.8655305
R
R
It looks like the script takes the first letter of:
"location_type" : "ROOFTOP",
what comes next if you watch this: https://maps.googleapis.com/maps/api/geocode/json?address=meerpaal%208d%20oosterhout&sensor=true
I don't need the 2 R's.. Does anyone know what I am doing wrong?
EDIT:
var_dump($data['results'][0]['geometry'])
Array
(
[location] => Array
(
[lat] => 51.6303446
[lng] => 4.8655305
)
[location_type] => ROOFTOP
[viewport] => Array
(
[northeast] => Array
(
[lat] => 51.631693580291
[lng] => 4.8668794802915
)
[southwest] => Array
(
[lat] => 51.628995619708
[lng] => 4.8641815197085
)
)
)
Late to the party, but I wanted to add what has worked for me as I searched high and low early on with Google Maps and how to dynamically collect lat and long much the way the OP has described. Please correct me if I am incorrect as I am still learning PHP and Google Maps in general, but again, this code works for my needs and I wanted to share this with the OP and others that may be searching for examples of working code that fits the OPs criteria.
I created this function after much banging of head to keyboard, this works for me in PHP 7. I see no need for a loop if you are collecting one addresses geo location.
Simply decode your http request from google api, check the request for validation, then assign the values of the lat and long to variables to be used as needed in your application/project.
I have included an example of how I collect Lat and Long of customers that sign up through a form with a simple street address.
*#NOTE: I have not shown any example of cleaning the posted variables in this answer. Please make sure to clean your posts of unwanted html entities/slashes/empty spaces, etc... *
function get_lat_and_long($address){
// $address = The address string in a proper address format, we run through urlencode();
// Get JSON results from this request
// APIKEY = *Google API personal key* and is a constant coming from an external constants.php file.
// You can add your key statically though I recommend saving all sensitive variables/constants and arrays in a server side file that has no access by regular users.
$geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false&key='.APIKEY);
// Convert JSON and validate
$geo = json_decode($geo, true);
if ($geo['status'] == 'OK') {
// Get Lat & Long
$latitude = $geo['results'][0]['geometry']['location']['lat'];
$longitude = $geo['results'][0]['geometry']['location']['lng'];
}else{
$latitude = NULL;
$longitude = NULL;
}
return $latitude;
return $longitude;
}
Without a function...
//create string holding address to run through google geolocation API for Lat/Long
$address = $_POST['usr_street'].' '.$_POST['usr_city'].' '.$_POST['usr_state'].', '.$_POST['usr_zip'];
// Get JSON results from this request
$geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false&key='.APIKEY);
// Convert JSON
$geo = json_decode($geo, true);
if ($geo['status'] == 'OK') {
// Get Lat & Long
$latitude = $geo['results'][0]['geometry']['location']['lat'];
$longitude = $geo['results'][0]['geometry']['location']['lng'];
}else{
$latitude = NULL;
$longitude = NULL;
}
From here, I can push $latitude and $longitude into my DB array and save the value in my DB column. Check it.
EX.
$userData = array(
'usr_first_name' => $_POST['usr_first_name'],
'usr_last_name' => $_POST['usr_last_name'],
'usr_password' => password_hash($_POST['usr_password'], PASSWORD_DEFAULT),
'usr_email' => $_POST['usr_email'],
'usr_company_name' => $_POST['usr_company_name'],
'usr_website' => $_POST['usr_website'],
'usr_street' => $_POST['usr_street'],
'usr_city' => $_POST['usr_city'],
'usr_state' => $_POST['usr_state'],
'usr_zip' => $_POST['usr_zip'],
'usr_phone1' => $_POST['usr_phone1'],
'usr_lat' => $latitude,
'usr_long' => $longitude,
//->etc...etc...etc...
);
//->$user is user object declared further up in document and comes from external user class -> user.class.php
//-> insert is function from external USER class that inserts query into DB.
$insert = $user->insert($userData);
Hope this helps others that are struggling with Google Maps API geo Location with PHP. In terms of dynamically collecting Lat and Long using simple address string constructed from form entries.