Grr... I cant seem to get fql working. One thing, I think the docs are old because i dont think api() likes just an array. Anyway:
$user_id = $facebook->getUser();//works
$access_token = $facebook->getAccessToken();//works
$fql = 'SELECT name from user where uid = ' . $user_id;
$ret_obj = $facebook->api(array(
'method' => 'fql.query',
'access_token' => $access_token,
'query' => $fql,
));
print_r($ret_obj);// dont work. actually I get exceptions
I've tried all kinds of combinations of params for api(), to no avail.
$user = curl('https://graph.facebook.com/'.$user_id.'?access_token='.$access_token);
does work, though.
I use this:
function fql($q, $access_token) {
// Run fql query
$fql_query_url = 'https://graph.facebook.com'
. '/fql?q='. urlencode($q)
. '&access_token=' . urlencode($access_token);
$fql_query_result = file_get_contents($fql_query_url);
$length = strlen(PHP_INT_MAX);
$fql_query_result = preg_replace('/"(user_id)":(\d{' . $length . ',})/', '"\1":"\2"', $fql_query_result);
return json_decode($fql_query_result, true);
}
Related
I am fetching tweets of a user. I need to show all tweets with ajax pagination. How can I achieve that?
https://api.twitter.com/1.1/statuses/user_timeline.json
Tried
I am using the above link. I heard about max_id, since_id but do not know how to use that. I have tried with max_id and since_id, then the collection is repeating. I am not getting any cursor response.
my code
$api_key = urlencode('*********'); // Consumer Key (API Key)
$api_secret = urlencode('***********'); // Consumer Secret (API Secret)
$auth_url = 'https://api.twitter.com/oauth2/token';
// what we want?
$data_username = '********'; // username
$data_count = 1; // number of tweets
$data_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
// get api access token
$api_credentials = base64_encode($api_key . ':' . $api_secret);
$auth_headers = 'Authorization: Basic ' . $api_credentials . "\r\n" .
'Content-Type: application/x-www-form-urlencoded;charset=UTF-8' . "\r\n";
$auth_context = stream_context_create(
array(
'http' => array(
'header' => $auth_headers,
'method' => 'POST',
'content' => http_build_query(array('grant_type' => 'client_credentials',)),
)
)
);
$auth_response = json_decode(file_get_contents($auth_url, 0, $auth_context), true);
$auth_token = $auth_response['access_token'];
// get tweets
$data_context = stream_context_create(array('http' => array('header' => 'Authorization: Bearer ' . $auth_token . "\r\n",)));
$datas = json_decode(file_get_contents($data_url . '?include_rts=true&count=' . $data_count . '&screen_name=' . urlencode($data_username), 0, $data_context), true);
// result - do what you want
print('<pre>');
print_r($datas);
Question
What values i have to pass for getting pagination url?
Thank you.
The Twitter API response should include a next_token variable which can be used for subsequent API calls / Pagination.
See https://developer.twitter.com/en/docs/twitter-api/pagination for more information on the topic (this refers to v2, as v1.1 is deprecated by now)
I can send a notification to all user that access my APP, using userid stored in a table "fbuid", and all works fine.
But if an user remove my APP the code fail and nobody get notification. How can I solve this problem.
$app_id = 'AAAAAAAA';
$app_secret = 'BBBBBBBBBBBBBB';
$app_access_token = $app_id . '|' . $app_secret;
$query = pg_query($dbconn, "SELECT * FROM fbuid;");
while ($row = pg_fetch_row($query))
{
$response = $facebook->api( '/'.$row[1].'/notifications', 'POST', array(
'template' => 'Nuovo Annuncio Pubblicato FaiceBuy',
'access_token' => $app_access_token
));
}
That means that the code skips due to exception thrown after failing in 1 case. So, you should write your code in try-catch block, just like this-.
while ($row = pg_fetch_row($query))
{
try
{
$response = $facebook->api( '/'.$row[1].'/notifications', 'POST', array(
'template' => 'Nuovo Annuncio Pubblicato FaiceBuy',
'access_token' => $app_access_token
));
echo '<pre>Post ID: ' . $response ['id'] . '</pre>';
}
catch(FacebookApiException $e)
{
echo $e->getMessage();
}
}
I guess I have read all past posts about the argument, but I was wondering if something has changed on topic.
Is it now possible to mention a page inside a message text using Facebook PHP SDK?
Something like this:
$post_params = array(
'access_token' => PAGE_TOKEN,
'message' => 'This is a message tagged to #[PAGE_ID]
);
$postStream = $this->facebook->api("/" . PAGE_ID . "/feed", 'post', $post_params);
I am referring to this page: https://developers.facebook.com/docs/opengraph/guides/tagging/
Actually, so far, I solved the problem the following way:
1) "Normalize" the page name through a regex replacement of possible "facebook related" uri
$replacePattern = '((https|http)?(:\/\/)?(www\.)?(facebook\.com)?(\/)?)';
$page_name = preg_replace($replacePattern, '', $page_name);
$page_name = 'https://www.facebook.com/' . $page_name;
2) Make a call to Facebook API using the "normalized" uri:
$fql = "SELECT id, name FROM profile WHERE id in (SELECT id FROM object_url WHERE url='" . $page_name . "')";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$response = $facebook->api($param);
<?
$fb_signed_request = $facebook->getSignedRequest();
$page_id = $fb_signed_request["page"]["id"];
$fb_like_status = $fb_signed_request["page"]["liked"];
$fb_is_admin = ($fb_signed_request["page"]["admin"]==1)?TRUE:FALSE;
$fql = "SELECT uid FROM page_admin WHERE page_id='" . $page_id . "'";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $facebook->api($param);
print_r ($fqlResult);
?>
I'm get the PAGE_ID ok, but FQLRESULT is null. I'm trying to get the id of the owner of the page..
Did you ask for manage_pages permission from the user? Check what access was granted via calling /me/permissions to see the complete list of items granted.
What I'm trying to do is a counter of facebook friends in php, so the result of the code would be something like "You have 1342 Friends!".
So this is the code im using:
<?php
require_once("../src/facebook.php");
$config = array();
$config[‘appId’] = 'MY_APP_ID';
$config[‘secret’] = 'MY_APP_SECRET';
$facebook = new Facebook($config);
$user_id = "MY_USER_ID";
//Get current access_token
$token_response = file_get_contents
("https://graph.facebook.com/oauth/access_token?
client_id=$config[‘appId’]
&client_secret=$config[‘secret’]
&grant_type=client_credentials");
// known valid access token stored in $token_response
$access_token = $token_response;
$code = $_REQUEST["code"];
// If we get a code, it means that we have re-authed the user
//and can get a valid access_token.
if (isset($code)) {
$token_url="https://graph.facebook.com/oauth/access_token?client_id="
. $app_id
. "&client_secret=" . $app_secret
. "&code=" . $code . "&display=popup";
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
}
// Query the graph - Friends Counter:
$data = file_get_contents
("https://graph.facebook.com/$user_id/friends?" . $access_token );
$friends_count = count($data['data']);
echo "Friends: $friends_count";
echo "<p>$data</p>"; //to test file_get_contents
?>
So, the result of the echo $Friends_count its always "1".
And then I test $data with an echo and it give me the list of all the friends so it is getting the content but its not counting it right... how could i fix it?
I've already tried changing the
$friends_count = count($data['data']);
for
$friends_count = count($data['name']);
and
$friends_count = count($data['id']);
but the result is always "1".
The result of the above code looks like this;
Friends: 1
{"data":[{"name":"Enny Pichardo","id":"65601304"},{"name":"Francisco Roa","id":"500350497"},etc...]
You have a JSON object; a string, not anything PHP can "count" with count(). You need to parse the JSON first:
$obj=json_decode($data);
$friends_count = count($obj->data); // This refers to the "data" key in the JSON
Some references I quickly googled:
http://php.net/manual/en/function.json-decode.php
http://roshanbh.com.np/2008/10/creating-parsing-json-data-php.html
You can get friend count by fql easily.
There is a friend_count field against the user table that you can query.
SELECT friend_count FROM user WHERE uid = me();
https://graph.facebook.com/fql?q=SELECT friend_count FROM user WHERE uid={any id}