I want to fetch data from a facebook event using graph API. I wrote the following code :
<?php
$url = 'https://graph.facebook.com/events/1588093858105216';
$contents = file_get_contents( $url );
if( $contents )
{
$data = json_decode( $contents, true );
echo '<pre>';
print_r( $data );
echo '</pre>';
}
?>
My event page address URL is https://www.facebook.com/events/1588093858105216/.
The above code is not working properly. When I am using the same code for a page,e.g., https://www.facebook.com/TangeloTown , it works fine.
<?php
$url = 'https://graph.facebook.com/TangeloTown';
$contents = file_get_contents( $url );
if( $contents )
{
$data = json_decode( $contents, true );
echo '<pre>';
print_r( $data );
echo '</pre>';
}
?>
Also, I am unable to run it on localhost. Please help me to solve this or please tell me any other method to solve this.
Generally, you need an access token to access all graph api. Please check the doc here for event api: https://developers.facebook.com/docs/graph-api/reference/v2.3/event
In your case, if you want to access event, you need a user access token or app access token. To get user access token you check this doc: https://developers.facebook.com/docs/facebook-login/access-tokens in this way, you need to ask user for authorization. But if you only want to access public event, you can simply use your app access token, which can be found here: https://developers.facebook.com/tools/accesstoken/, but the prerequisite is that you need to register a FB app.
Related
I have tried every single answer on this forum and couldn't get a proper working code. So I am posting this again.
I want to get the thumbnail of private and hidden Videos from Vimeo. I also have an access token generated which I tried to use with the solutions provided for the old questions which also didn't work.
I tried this code
https://vimeo.com/api/oembed.json?url=https://vimeo.com/531126552/access_token
Also tried using cURL
curl_setopt($curl_h, CURLOPT_HTTPHEADER,
array(
'Authorization: bearer {access_token}',
'Accept: application/vnd.vimeo.*+json;version=3.4',
)
);
curl_setopt($curl_h, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl_h);
The response for above cURL code is this:
{"error":"Something strange occurred. Please contact the app owners.","link":null,"developer_message":"No user credentials were provided.","error_code":8003}
Please suggest me a way to do this or help me find where the error is.
The thumbnails can be accessed by the thumbnail_url in JSON data provided by Oembed API
sample :
https://vimeo.com/api/oembed.json?url=https://vimeo.com/{video_id}/
Here is what works for public videos. I know you asked about private, so I am going to ask, if you ever figured it out...ping me. Trying to figure out the same.
For others, here is the public solution. It is the private videos that it fails on.
function mmd_get_vimeo_info( $video_id )
{
// https://vimeo.com/api/oembed.json?url=https://vimeo.com/
$VimeoConnectLink = "https://vimeo.com/api/oembed.json?url=https://vimeo.com/" . $video_id ;
$request = wp_remote_get( esc_url_raw($VimeoConnectLink));
if ('error' == $request || is_wp_error($request))
return "";
$response = wp_remote_retrieve_body( $request );
if ('error' == $response || is_wp_error($response))
return "";
$video_array = json_decode( $response, true );
// Looking for [title], [thumbnail_url] [duration]
return $video_array;
}
But it can fail with domain privacy in place.
So I was trying to fetch facebook comments then put it into my own website so then I could reply it through my website. After doing some research, I found it Graph API could be the solution to my problem. Below is the code
<?php
$group_id ="14********_1**********99";
$access_token="accesstoken";
$url = "https://graph.facebook.com/v2.11/" . $group_id .
"/comments?
fields=message,from{name,id}&access_token=".$access_token;
$json= file_get_contents($url);
$data = json_decode($json);
foreach ($data->comments->data as $key) {
if(isset($key->message))
{
echo $key->message;
echo "<br>";
}
}
?>
How do I fetch comments from Facebook through the URL above? Sorry I'm still new to programming.
I'm trying to track my users' file changes with Dropbox's Webhooks interface. I expected the call to include POST data, but there doesn't seem to be POST data (or GET data, for that matter). Here is my PHP code, where the if part is for Dropbox to validate the webhook, and the else part saves the $_POST and $_GET variables to a file.
<?php
if( isset( $_GET['challenge'] ) ) {
echo $_GET['challenge'];
} else {
$output = print_r($_POST, true);
file_put_contents('file.txt', $output, FILE_APPEND );
$output = print_r($_GET, true);
file_put_contents('file.txt', $output, FILE_APPEND );
}
?>
After a short while, file.txt fills up with this:
Array
(
)
Array
(
)
Following this answer to a related question, you need to get the JSON data like this:
$output = file_get_contents('php://input');
Or, since it's JSON:
$output = json_decode( file_get_contents('php://input') );
I am using the Google Api PHP Client.
I want to get the details of the user who has logged in.
I am setting the following Scopes
$client->setScopes('https://www.googleapis.com/auth/userinfo.profile');
$request = new apiHttpRequest("https://www.googleapis.com/oauth2/v1/userinfo?alt=json");
$userinfo = $client->getIo()->authenticatedRequest($request);
$response = json_encode(simplexml_load_string($userinfo->getResponseBody()));
print "<pre>" . print_r(json_decode($response, true), true) . "</pre>";
But i am not able to get the jSON from the above code.
Wanted to Deleted the Question but didnt find any Answer that uses the PHP Client. so answering my own question
Just by replacing
$response = json_encode(simplexml_load_string($userinfo->getResponseBody()));
by
$response = $userinfo->getResponseBody();
I have integrated Facebook Like into my website.Now i need to get/list the count of the Facebook like in the Admin panel of my website. Is there any way to get this done?Need suggestions.Thanks in advance.
$url = 'http://graph.facebook.com/PAGE_OR_APP_ID';
echo '['.$url.']: '.json_decode(file_get_contents($url))->{'likes'};
In order to get the number of likes of any object using Facebook API, use a Facebook API of your choice and commands like this:
https://graph.facebook.com/< your object id>/
You will receive a JSON object and you can extract the number of likes from it:
{
"id": "567454",
"link": "http://www.facebook.com/pages/PHP-Developer/103146756409401",
"likes": 250,
"type": "page"
}
More info on https://developers.facebook.com/docs/reference/api/
The more direct / updated link for this topic on facebook is
https://developers.facebook.com/docs/reference/api/page/
function fbLikeCount($appid,$appsecret){
//Construct a Facebook URL
$json_url ='https://graph.facebook.com/'.$appid.'?access_token='.$appsecret;
$json = file_get_contents($json_url);
$json_output = json_decode($json);
//Extract the likes count from the JSON object
if($json_output->likes){
return $likes = $json_output->likes;
}else{
return 0;
}
}
Facebook - <?php echo fbLikeCount('app/page id here','token here'); ?>
You can achieve that using the Graph API (v3)
function checkFacebookReactions($url){
$access_token = 'YOUR-FACEBOOK-TOKEN'; // Generate a Facebook Token first
$api_url = 'https://graph.facebook.com/v3.0/?id=' . urlencode( $url ) . '&fields=engagement&access_token=' . $access_token;
$fb_connect = curl_init(); // initializing
curl_setopt( $fb_connect, CURLOPT_URL, $api_url );
curl_setopt( $fb_connect, CURLOPT_RETURNTRANSFER, 1 ); // return the result, do not print
curl_setopt( $fb_connect, CURLOPT_TIMEOUT, 20 );
$json_return = curl_exec( $fb_connect ); // connect and get json data
curl_close( $fb_connect ); // close connection
$body = json_decode( $json_return );
// Print each key values, if needed
foreach($body->engagement as $k=>$v){
echo $k .": " .$v ."<br>";
}
$count = $body->engagement->reaction_count; // Return only reactions (likes + other reactions)
return $count;
}
$url = "REPLACE-WITH-YOUR-URL"; // The url you want to fetch details from
checkFacebookReactions($url);
First, you will need to generate a Facebook token. You can fin plenty of resources that explain how to get a token. This one will do the trick: https://elfsight.com/blog/2017/10/how-to-get-facebook-access-token