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);
Related
I am having trouble getting all the facebook posts, message, photos etc... that show up on my wall via the Facebook API. Basically, many items are missing in the result set. I tried the following with FQL:
$facebook = new Facebook(array(
'appId' => FB_APPID,
'secret' => FB_SECRET,
'cookie' => true
));
$fql = "SELECT post_id, actor_id, target_id, message FROM stream WHERE source_id =me() ORDER BY updated_time DESC";
//Create Query
$params = array(
'method' => 'fql.query',
'query' => $fql,
);
//Run Query
$result = $facebook->api($params);
But this feed seems to be missing some items, and i don't know why.
I also tried with reading the graph feed at http://graph.facebook.com/me/feed?access_token=<Access+token> and again, i also don't get all the feed items.
What is the problem? Why am I missing some items?
use facebook->getLoginUrl with Permissions, see https://developers.facebook.com/docs/reference/login/#permissions
$applicationurl = 'http://{yourwebsite}/facebook.php';
// Get User ID
$user = $facebook->getUser();
if(empty($user))
{
$params = array(
'scope' => 'email',
'redirect_uri' => $applicationurl
);
$loginUrl = $facebook->getLoginUrl($params);
header('Location: ' . $loginUrl ."\r\n");
exit;
}
Use this code before your query.
You can use $user in your query like uid = '.$user.
Set your permissions in the scope part of your request, email in the example above.
EDIT OP says use the scope 'read_stream', and it works
I just testing something and found out by script not working . It actually upload a pic and then use it pic id to tag some random peoples from the friend lists . My app have user_photos,publish_stream permissions . I can successfully upload the photo but i am getting error on making tags .
Here is The code:
$f1 = $facebook->api('me/friends?limit=19');
$img = $_REQUEST['imgl'];
$access_token = $facebook->getAccessToken();
$args = array(
'message' => $_REQUEST['m_Config']['appTitle'],
'source' => '#' . $img,
'access_token' => $access_token,
);
$photo = $facebook->api ( $user . '/photos', 'post', $args );
foreach($f1['data'] as $fbu){
$tagx = array('tag_uid' => $fbu['id'],'x' => rand(100,350),'y' => rand(100,350));
$ftags[] = $tagx;
}
$args = array (
'tags' => json_encode($ftags),
'access_token' => $access_token,
);
$result = $facebook->api('/' . $photo['id'] . '/tags', 'post', $args);
print_r($result);
Now the Error which , I am getting is Fatal error: Uncaught OAuthException: (#100) Invalid parameter thrown in C:\xampp\htdocs\fb\base_facebook.php on line 1254
My question is where , My code is wrong . WHy I am getting error , I also tried searching but cant get it fixed .
Thanks
You seem to be assigning the tags at random locations for random friends, which is completely wrong.
The user should be selecting where in the photo their friends are - anything else would be against policy and likely to be shut down as spam
As for your code problem, this isn't working because tag_uid isn't a listed parameter name in the API - the parameter to use for specifying which user to tag is to
See the Photo object's Tag connection documentation for more details and some example code.
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);
}
<?
$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.
Here is the code:
$file= 'bbbb.jpg';
$data = array(
basename($file) => "#".realpath($file),
"caption" => "Uploaded using graph api",
"aid" => '13595',
"access_token" => $accessToken,
'method' => 'photos.upload'
);
$sds =$facebook->api($data);
This is the error
Uncaught CurlException: 26: failed creating formpost data
What to do?
Here are some various ways to upload photos using the Graph API. The examples assume you've instantiated the $facebook object and have a valid session for the current user.
1 - Default Application Album of Current User
This example will upload the photo to your default application album of the current user. If the album does not yet exist it will be created.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/me/photos', 'post', $args);
print_r($data);
2 - Target Album
This example will upload the photo to a specific album.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos', 'post', $args);
print_r($data);
3 - Target Album with Access Token
This example will upload a photo to a specific album which requires an access token.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos?access_token='. $ACCESS_TOKEN, 'post', $args);
print_r($data);
your $data array should have "message" instead of "caption",
also, remove "aid", "method", and "access_token"
your $data has to have the file data and "message", that is it.
$sds =$facebook->api('/me/13595/photos', 'POST', $data);
where instead of 13595 just use the variable with the album aid
also, if needed, access_token is best appended to api uri like this:
$sds =$facebook->api('/me/13595/photos?access_token='.$access_token, 'POST', $data);
also, if the php sdk doesn't work for you, I have successfully used cURL instead if your php installation supports it. in that case see cURL example at Upload Photo To Album with Facebook's Graph API
The latest version of the Facebook PHP SDK wont work with the above examples without the following update to the code.
class Facebook {
...
*Line #539*
protected function makeRequest($url, $params, $ch=null) {
if (!$ch) {
$ch = curl_init();
}
if( isset($params['doMultiPart']) ) {
$doMultiPart= true;
unset($params['doMultiPart']);
} else {
$doMultiPart= false;
}
$opts = self::$CURL_OPTS;
$opts[CURLOPT_POSTFIELDS] = $doMultiPart ? $params : http_build_query($params, null, '&');
...
Basically the problem is that the PHP SDK uses "curl_setopt_array" which if you pass it a url encoded string as the option value it will pass the data as application/x-www-form-urlencoded when what you really want is multipart/form-data; to do this we simply switch to passing in the array of options if we have a param of doMultiPart in the params array.
This was a quick hack I put together to get something working, probably need to review the code to make sure it doesnt break anything else you are doing. Otherwise enjoy.