Facebook Graph api doesn't return everything - php

I just found out that Facebook updated their graph api and now require an access token to retrieve like counts for a page to be displayed on an external website. I went through the motions of setting up an app to get the token and have the following:
$fbData = json_decode(file_get_contents('https://graph.facebook.com/myFacebookName?access_toke=MyAppId|MySecretToken'));
print_r($fbData);
It seems to only be returning objects:
stdClass Object ( [name] => My Facebook Name [id] => id number )
And that is all, giving me nothing to parse through. The name and id coming through are correct so there is a connection happening. Note that I have edited out some information so MyFacebookName and MyAppId|MySecretToken are actually populated with the correct info. Any ideas how to get the full JSON list to grab page likes? Am I missing something?
UPDATE
Thanks to Tobi and further reading the graph api, documentation I was able to get the number of likes with with following:
$fbData = json_decode(file_get_contents('https://graph.facebook.com/name?fields=likes&access_token=appId|accessToken'));
<?php echo number_format($fbData->likes);?>

You need to specifically request the fields you want in the response. In your case this would be
https://graph.facebook.com/myFacebookName?fields=id,name,likes&access_toke=MyAppId|MySecretToken
I don't understand why you say there "nothing to parse", because the object IS returned and can be used. So what's the problem?
Using echo $fbData['likes'] should give you the number of likes.
See
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.4#fields
https://developers.facebook.com/docs/graph-api/reference/page#Reading

Related

Facebook API : Empty Result for Ad Leads

I am building a PHP script based on Facebook Marketing API,
The API system works fine,
I can get the result for the campaigns, the ads, and full insights as well.
But, I want to get the result of the leads.
Note (1): I got admin access to the APP, the Ad account, and the page as well,
Note (2): App Status: In Development
Note (3) : I send the API request for "leads_retrieval" and "leads_retrieval",
$permissions = ['email','public_profile','leads_retrieval','ads_management', 'ads_read', 'business_management', 'read_audience_network_insights', 'read_insights', 'manage_pages', 'pages_show_list', 'pages_manage_cta','pages_manage_instant_articles', 'publish_pages', 'read_page_mailboxes'];
My Code to get the lead result :
$ad = new Ad('<AD_ID>');
$leads = $ad->getLeads();
$lead = $leads->getResponse()->getBody();
print_r($lead);
Result:
{"data":[]}
So for some reason, I still get the incorrect empty result for the leads!
Any idea about how to get the correct lead result for the Ad?
Reference : https://developers.facebook.com/docs/marketing-api/guides/lead-ads/retrieving/v2.9
I have same problem before but I found that I'm using incorrect ad_id.
double check you ad id, its possible that your not getting the full list of ad id inside adset.
use $ads->fetchAfter() to get the next page of ads
check complete code here https://stackoverflow.com/a/56500241/2588592

Linkedin API - get full data

I try to make an script to get data from LINKEDIN, i am able to get many of information about profile like name, mail, phone, picture url and so on, unfortunately how ever I try I can't get to informations like work experience and many other.
My Request looks like:
$xml_response = $linkedin->getProfile("~:(id,first-name,last-name,interests,publications,patents,languages,skills,date-of-birth,email-address,phone-numbers,im-accounts,main-address,twitter-accounts,headline,picture-url,public-profile-url)");
This work correctly, but lets try to add companies
Error:
I added it at the end of $linkedin->getProfile and here is the
error message what I get:
[message] => Unknown field {companies} in resource {Person}
Full function looks like:
public function linkedinGetUserInfo( $requestToken='', $oauthVerifier='', $accessToken=''){
include_once 'linkedinoAuth.php';
$linkedin = new LinkedIn($this->config['linkedin_access'], $this->config['linkedin_secret']);
$linkedin->request_token = unserialize($requestToken); //as data is passed here serialized form
$linkedin->oauth_verifier = $oauthVerifier;
$linkedin->access_token = unserialize($accessToken);
try{
$xml_response = $linkedin->getProfile("~:(id,first-name,last-name,interests,publications,patents,languages,skills,date-of-birth,email-address,phone-numbers,im-accounts,main-address,twitter-accounts,headline,picture-url,public-profile-url,educations,companies)");
}
catch (Exception $o){
print_r($o);
}
return $xml_response;
}
Documentation I found (but didn't help me )
https://developer.linkedin.com/docs/fields
I expect I miss something really stupid but cant figurate it out, can somone help me?
LinkedIn provided all these fields before May 12th 2015.
https://api.linkedin.com/v1/people/~:(id,first-name,last-name,email-address,picture-url,industry,associations,interests,num-recommenders,date-of-birth,honors-awards,three-current-positions,three-past-positions,volunteer,location," +
"positions:(id,title,summary,start-date,end-date,is-current,company:(id,name,type,size,industry,ticker))," +
"educations:(id,school-name,field-of-study,start-date,end-date,degree,activities,notes)," +
"publications:(id,title,publisher:(name),authors:(id,name),date,url,summary),"+
"languages:(id,language:(name),proficiency:(level,name)),"+
"skills:(id,skill:(name)))";
"courses:(id,name,number),"+
"recommendations-received:(id,recommendation-type,recommendation-text,recommender))";
"patents:(id,title,summary,number,status:(id,name),office:(name),inventors:(id,name),date,url))";
But, they have updated their APIs link:https://developer.linkedin.com/support/developer-program-transition
According to which only these
Profile API — /v1/people/~
Share API — /v1/people/~/shares
Companies API — /v1/companies/{id}
endpoints will be active, Rest require a Apply with Linkedin /Partner with Linked association.
phone-numbers,im-accounts,main-address,twitter-accounts,headline would come under r_contactinfo which needs a Apply with linkedin
Instead of companies, you must use positions keyword.
$xml_response = $linkedin->getProfile("~:(id,first-name,last-name,positions)");
Positions field description in the following address.
https://developer.linkedin.com/docs/fields/positions

parse foursquare json feed using php

Using Foursquare API it returns me the following JSON feed
{"meta":{"code":200},"notifications":[{"type":"notificationTray","item":{"unreadCount":1}},{"type":"message","item":{"message":"OK! We've got you # Place XXX. You've been here 2 times."}},{"type":"insights","item":{"insights":{"count":4,"items":[{"type":"pointsReward","image":"https://foursquare.com/img/points/discovery-venue3.png","title":"First of your friends to check into Place XXX!","shareable":false,"points":{"image":{"prefix":"https://foursquare.com/img/points/discovery-venue3_","sizes":[44,60,120],"name":".png"},"message":"First of your friends to check into Place XXX!","points":5}},{"type":"pointsReward","image":"https://foursquare.com/img/points/discovery-venue3.png","title":"First time at Place XXX.","shareable":false,"points":{"image":{"prefix":"https://foursquare.com/img/points/discovery-venue3_","sizes":[44,60,120],"name":".png"},"message":"First time at Place XXX.","points":5}},{"type":"pointsReward","image":"https://foursquare.com/img/points/category-outdoor.png","title":"First Plaza check-in of your friends!","shareable":false,"points":{"image":{"prefix":"https://foursquare.com/img/points/category-outdoor_","sizes":[44,60,120],"name":".png"},"message":"First Plaza check-in of your friends!","points":6}},{"type":"pointsReward","image":"https://foursquare.com/img/points/discovery-venue3.png","title":"First of your friends to check in here.","shareable":false,"points":{"image":{"prefix":"https://foursquare.com/img/points/discovery-venue3_","sizes":[44,60,120],"name":".png"},"message":"First of your friends to check in here.","points":2}}]}}},{"type":"leaderboard","item":{"total":18,"scores":[{"icon":"https://foursquare.com/img/points/discovery-venue3.png","message":"First of your friends to check into...
What I would like to do is to display the message "OK! We've got you # Place XXX. You've been here 2 times." which is embedded in the second "type" tag
I'm used to do get this kind of value using a foreach function but with this one I cannot get it.
I need help from an expert, thanks for your participation.
Code Im trying to use without success:
$data = json_decode($response, true); // return array not object
foreach($data['notifications']['type'][0] as $item) {
echo $item['item']['message'];
}
notifications is an array, so you can't hash into it with 'type' like you are trying to do. You should iterate through $data['notifications'] until you find an object with a "type" property of "message". Then dig into that object's "item" and get its "message". For more details about the notifications field in a response, see https://developer.foursquare.com/docs/responses/notifications.
In the sample JSON you provided, to directly access the content you want, you could use $data['notifications'][1]['item']['message']. The 1 may change depending on what API version you're passing in, but should be relatively stable as long as you keep using the same version.

(#100) At least one reference object must be specified - Simple POST of OG action

I am using the PHP SDK to try and post a simple action using the Graph API.
I have set up an object called 'Competition' and an action called 'Enter'. As such, a user enters a competition.
My code is like so:
try {
$statusUpdate = $this->data['facebook']->api('/me/lovepoppycompetition:enter', 'post', array(
'competition' => 'https://apps.facebook.com/lovepoppycompetition/'
));
echo '<pre>SUCCESS: '.print_r($statusUpdate, true).'</pre>';
} catch(FacebookApiException $e) {
echo '<pre>FAILED: '.print_r($e, true).'</pre>';
}
I am getting an error returned (FacebookApiException):
(#100) At least one reference object must be specified
I'm not really sure why, especially as I am using this code (with different objects/actions) on another app with no issues just fine.
It might be worth noting that I haven't submitted the action, although I don't think I have to for it to work for me?
Thanks in advance.
I think the problem lies with you redirecting the action back to your facebook application. You should create a competition page on your website, and use that to redirect users back to your website.
Make sure you use the correct og tags on the redirect page.
This was caused by a FB bug, which is now resolved

how to tag a friend using FB graph API?

I am trying to tag a friend in FB message to be posted, I am using PHP, and for FB I am using FB Auth API 2.0, I have tried to get related information from facebook developers forum, however can't find enough information for this one.
Can anyone suggest me from where to get started for this? any links to related documentation or forum links using which I can get started?
You use the message_tags field of the Post object. Check out the docs:
message_tags: object containing fields whose names are the indexes to
where objects are mentioned in the message field; each field in turn
is an array containing an object with id, name, offset, and length
fields, where length is the length, within the message field, of the
object mentioned
Something like this:
$facebook->setFileUploadSupport(true);
$tags = array(
‘tag_text’ => ‘ak Yip’,
‘tag_uid’ => 642536423,//it’s my id on facebook as a sample
‘x’ => 10,
‘y’ => 10,
);
$args['tags'] = array($tags);
$args['image'] = ‘#’ . realpath($fn);
$data = $facebook->api(‘/me/photos’, ‘post’, $args);
Note:
**One:Your app need to get permissions like ‘user_photo_video_tags’ and ‘friends_photo_video_tags’.
Two:X,Y is proportion, not coordinate. Don’t put value over 100.**

Categories