Limitation of number posts on API - facebook-graph-api

I want to build a FB app which posts messages to the walls of those registered for the app.
There are two setups:
One message to many people (1-many, could occur few times a day)
Many user-specific massages (1-1, but many of them, could occur few times a day for each user)
All in all; one user could get a few different updates on his wall per day, but it could affect many users (that's pretty much the whole point of my idea)
Into what extend is Facebook going to allow me to do this, and won't think I'll be spamming.
PS:
I've come along this post, which seems to have remained unsolved...:
Post on Multiple Friend's Wall
And this post, which doesn't make it clear for me whether my idea is something I should start or not ;)
Graph API post to wall limitation

You can use Facebook batch API to do what you intent.
You can get more information on Facebook batch request at: http://25labs.com/tutorial-post-to-multiple-facebook-wall-or-timeline-in-one-go-using-graph-api-batch-request/
$batchPost[] = array(
'method' => 'POST',
'relative_url' => "/{ID1}/feed?access_token={ACCESS_TOKEN_FOR_ID1}",
'body' => http_build_query($body) );
$batchPost[] = array(
'method' => 'POST',
'relative_url' => "/{ID2}/feed?access_token={ACCESS_TOKEN_FOR_ID2}",
'body' => http_build_query($body) );
$batchPost[] = array(
'method' => 'POST',
'relative_url' => "/{ID3}/feed?access_token={ACCESS_TOKEN_FOR_ID3}",
'body' => http_build_query($body) );
$multiPostResponse = $facebook->api('?batch='.urlencode(json_encode($batchPost)), 'POST');

I developed an app in my website http://www.cefozyt.com which can post links, message etc. to multiple facebook users wall & groups. I used :-
if($user){
// Proceed knowing you have a logged in user who has a valid session.
//========= Batch requests over the Facebook Graph API using the PHP-SDK ========
// Save your method calls into an array
$queries = array(
array('method' => 'GET', 'relative_url' => '/'.$user),
array('method' => 'GET', 'relative_url' => '/'.$user.'/friends'),
array('method' => 'GET', 'relative_url' => '/'.$user.'/groups'),
array('method' => 'GET', 'relative_url' => '/'.$user.'/likes'),
);
// POST your queries to the batch endpoint on the graph.
try{
$batchResponse = $facebook->api('?batch='.json_encode($queries), 'POST');
}catch(Exception $o){
error_log($o);
}
//Return values are indexed in order of the original array, content is in ['body'] as a JSON
//string. Decode for use as a PHP array.
$user_info = json_decode($batchResponse[0]['body'], TRUE);
$friends_list = json_decode($batchResponse[1]['body'], TRUE);
$groups = json_decode($batchResponse[2]['body'], TRUE);
$pages = json_decode($batchResponse[3]['body'], TRUE);
//========= Batch requests over the Facebook Graph API using the PHP-SDK ends =====
if(isset($_POST['submit_x'])){
if($_POST['message'] || $_POST['link'] || $_POST['picture']) {
$body = array(
'message' => $_POST['message'],
'link' => $_POST['link'],
'picture' => $_POST['picture'],
'name' => $_POST['name'],
'caption' => $_POST['caption'],
'description' => $_POST['description'],
);
$batchPost=array();
$i=1;
$flag=1;
foreach($_POST as $key => $value) {
if(strpos($key,"id_") === 0) {
$batchPost[] = array('method' => 'POST', 'relative_url' => "/$value/feed", 'body' => http_build_query($body));
if($i++ == 50) {
try{
$multiPostResponse = $facebook->api('?batch='.urlencode(json_encode($batchPost)), 'POST');
}catch(FacebookApiException $e){
error_log($e);
echo("Batch Post Failed");
}
$flag=0;
unset($batchPost);
$i=1;
}
}
}
if(isset($batchPost) && count($batchPost) > 0 ) {
try{
$multiPostResponse = $facebook->api('?batch='.urlencode(json_encode($batchPost)), 'POST');
}catch(FacebookApiException $e){
error_log($e);
echo("Batch Post Failed");
}
$flag=0;
}
}
else {
$flag=2;
}
}
}
?>

Related

Facebook Graph API - Post comment with photo

I have a PHP script like:
$facebook = new Facebook( array( 'appId' => APP_ID, 'secret' => APP_SECRET ) );
$facebook->setDefaultAccessToken( ACCESS_TOKEN );
$post_comment = $facebook->api( $object_id . "/comments", "POST", array(
'source' => $facebook->fileToUpload('http://healthhub.co/wp-content/uploads/2014/02/Group-Slider.jpg'),
'message' => "Comment with photo!"
) );
Then, I checked on Facebook, it just like [result of post comment][1]
[1]: https://i.stack.imgur.com/6l5yp.png , in while, I want it like that: result wanted

Create new page locations FACEBOOK API

I'm trying to create a new location for an existing page using an APP with Standard Access, but It's show me an error "Only white-listed APP can create new pages using this endpoint /{page_id}/locations".
Facebook SDK PHP
Facebook Graph API v2.9
My code:
$fb = new Facebook(['app_id' => app_idp,'app_secret' => app_secret,]);
$fb->setDefaultAccessToken(master_page_access_token);
$new_location['hours'] = array("mon_1_open"=> "10:00",
"mon_1_close"=>"19:00",
"tue_1_open"=> "10:00",
"tue_1_close"=> "19:00",
"wed_1_open"=>"10:00",
"wed_1_close"=> "19:00",
"thu_1_open"=> "10:00",
"thu_1_close"=> "19:00",
"fri_1_open"=> "10:00",
"fri_1_close"=> "19:00",
"sat_1_open"=>"10:00",
"sat_1_close"=>"19:00"
);
$location = array(
'hours'=>$new_location['hours'],
'location'=> array(
'street'=>$full_address,
'latitude' => $latitude,
'longitude'=> $longitude,
'city_id'=> $city_id,
'zip'=> $zip),
'phone' => $phone ,
'place_topics'=>array('210979565595898',
'199512783398620',
'108472109230615'),
'price_range' => '$$',
'store_location_descriptor' => $store_location_descriptor,
'store_name'=> $store_name,
'store_number'=> $store_id,
);
try {
$new_pages = $fb->post('/{page_id}/locations',$location,master_page_access_token);
}
catch (Exceptions\FacebookResponseException $exc) {
echo $exc->getMessage();
}
Error
enter image description here

Mocking Cake Request

I am developing a test for a controller function and basically it just acts upon a cake request, is there anyway to mock cake request inside the test function so that whenever the controller tries to access $this->request->data it returns the data i have set in the test case? if there is a way please tell me how.
Regards
The documentation contains an example of how to set the request data. For quick reference:
public function testIndexPostData() {
$data = array(
'Article' => array(
'user_id' => 1,
'published' => 1,
'slug' => 'new-article',
'title' => 'New Article',
'body' => 'New Body'
)
);
$result = $this->testAction(
'/articles/index',
array('data' => $data, 'method' => 'post')
);
debug($result);
}

fql multiquery error

i have written following code to learn how fql multiquery works:
include_once "fbmain.php";
try{
$your_id=xxxxxxxx;
$fql= '{
"friends":"SELECT uid2 FROM friend WHERE uid1='.$your_uid.',"
"friendinfo":"SELECT * FROM standard_user_info WHERE uid IN (SELECT uid2
FROM #friends)"
}';
$res = $facebook->api_client->fql_multiquery($fql);
}
catch(Exception $o){
d($o); // d() is function to print
}
but the output shows error
Call to a member function fql_multiquery() on a non-object on line 9
i have also tried other queries instead of these two , but the problem remains same. i am using php sdk 3.0.
help pls.
I do not use Multi Query, since fql has moved to the graph api. Instead i use batch request.
Here is a sample of a request of /feed, the comments of the 1st 10 results and fql multi of the permalink from the 1st 10 posts. I am sure this is a bit over kill for what you need but is the best example i have for mixing graph and fql and using name feature, which seems is what you need to carry friends.
https://developers.facebook.com/docs/reference/api/batch/
<?php
if($type=="home"){
$relURL = '/'.$pageid.'/'.$type.'?'.$access_token.'%26limit='.$limit.'%26return_ssl_resources=1%26fields=id,from,name,message,likes,comments,link,picture,caption,story,source,created_time,type,actions,application,object_id,description%26since='.$since.'%26until='.$until.'%26offset='.$offset.'';
}else{
$relURL = '/'.$pageid.'/'.$type.'?'.$app_access_token.'%26limit='.$limit.'%26return_ssl_resources=1%26fields=id,from,name,message,likes,comments,link,picture,caption,story,source,created_time,type,actions,application,object_id,description%26since='.$since.'%26until='.$until.'%26offset='.$offset.'';
}
$queryProfile = array(
array('method' => 'GET', 'relative_url' => '/'.$pageid.'?fields=id,name,link,picture%26'.$app_access_token.''),
array('method' => 'GET', 'relative_url' => ''.$relURL.'', 'name' => 'comments', 'omit_response_on_success' => false),
array('method' => 'GET', 'relative_url' => '/{result=comments:$.data.0.id}/comments?fields=id,from,message,created_time%26limit=3%26offset=0'),
array('method' => 'GET', 'relative_url' => '/{result=comments:$.data.1.id}/comments?fields=id,from,message,created_time%26limit=3%26offset=0'),
array('method' => 'GET', 'relative_url' => '/{result=comments:$.data.2.id}/comments?fields=id,from,message,created_time%26limit=3%26offset=0'),
array('method' => 'GET', 'relative_url' => '/{result=comments:$.data.3.id}/comments?fields=id,from,message,created_time%26limit=3%26offset=0'),
array('method' => 'GET', 'relative_url' => '/{result=comments:$.data.4.id}/comments?fields=id,from,message,created_time%26limit=3%26offset=0'),
array('method' => 'GET', 'relative_url' => '/{result=comments:$.data.5.id}/comments?fields=id,from,message,created_time%26limit=3%26offset=0'),
array('method' => 'GET', 'relative_url' => '/{result=comments:$.data.6.id}/comments?fields=id,from,message,created_time%26limit=3%26offset=0'),
array('method' => 'GET', 'relative_url' => '/{result=comments:$.data.7.id}/comments?fields=id,from,message,created_time%26limit=3%26offset=0'),
array('method' => 'GET', 'relative_url' => '/{result=comments:$.data.8.id}/comments?fields=id,from,message,created_time%26limit=3%26offset=0'),
array('method' => 'GET', 'relative_url' => '/{result=comments:$.data.9.id}/comments?fields=id,from,message,created_time%26limit=3%26offset=0'),
array('method' => 'GET', 'relative_url' => '/fql?q=SELECT+permalink+FROM+stream+WHERE+source_id=\''.$pageid.'\'+AND+post_id=\'{result=comments:$.data.0.id}\''),
array('method' => 'GET', 'relative_url' => '/fql?q=SELECT+permalink+FROM+stream+WHERE+source_id=\''.$pageid.'\'+AND+post_id=\'{result=comments:$.data.1.id}\''),
array('method' => 'GET', 'relative_url' => '/fql?q=SELECT+permalink+FROM+stream+WHERE+source_id=\''.$pageid.'\'+AND+post_id=\'{result=comments:$.data.2.id}\''),
array('method' => 'GET', 'relative_url' => '/fql?q=SELECT+permalink+FROM+stream+WHERE+source_id=\''.$pageid.'\'+AND+post_id=\'{result=comments:$.data.3.id}\''),
array('method' => 'GET', 'relative_url' => '/fql?q=SELECT+permalink+FROM+stream+WHERE+source_id=\''.$pageid.'\'+AND+post_id=\'{result=comments:$.data.4.id}\''),
array('method' => 'GET', 'relative_url' => '/fql?q=SELECT+permalink+FROM+stream+WHERE+source_id=\''.$pageid.'\'+AND+post_id=\'{result=comments:$.data.5.id}\''),
array('method' => 'GET', 'relative_url' => '/fql?q=SELECT+permalink+FROM+stream+WHERE+source_id=\''.$pageid.'\'+AND+post_id=\'{result=comments:$.data.6.id}\''),
array('method' => 'GET', 'relative_url' => '/fql?q=SELECT+permalink+FROM+stream+WHERE+source_id=\''.$pageid.'\'+AND+post_id=\'{result=comments:$.data.7.id}\''),
//array('method' => 'GET', 'relative_url' => '/'.$pageInfo[id].'/photos?fields=id,name%26limit=9')
);
$batchResponse = $facebook->api('?batch='.json_encode($queryProfile), 'POST');
$pageInfo = json_decode($batchResponse[0]['body'], true);
$pageType = $pageInfo[type];
$pageLink = $pageInfo[link];
$thispageid = $pageInfo[id];
$MEcomments = json_decode($batchResponse[1]['body'], true);
?>
Looks rather a problem with the instantiation of
$facebook->api_client
Worth to note also that in [standard_user_info] the column [uid] is of type string so you would probably also need to add quotes.
https://developers.facebook.com/docs/reference/fql/standard_user_info/
According to FB docs, FQL multiquery should be faster than subsequent batch calls.
https://developers.facebook.com/docs/reference/fql/

How to send email notifier with facebook php sdk?

i can do it via url:
https://api.facebook.com/method/notifications.sendEmail?recipients=ID_USER&subject=test&text=test&access_token=USER_ACCESS_TOKEN
http://developers.facebook.com/docs/reference/api/message/
EDIT:
I see now you are trying to use the PHP SDK. Perhaps something like the following will work for you (saw this on another stackoverflow question):
$parameters = array(
'app_id' => $facebook->getAppId(),
'to' => $facebookUserId,
'link' => '(required) The link to send in the message. (??)',
'redirect_uri' => 'URL_TO_REDIRECT_TO_AFTER_USER_CLICKS_SEND_OR_CANCEL',
'picture' => 'OPTIONAL_URL_TO_IMG--AUTOGENERATED BY LINK',
'name' => 'OPTIONAL_NAME_OF_MESSAGE/ARTICLE--AUTOGENERATED BY LINK',
'description' => 'OPTIONAL_DESCRIPTION_TEXT--AUTOGENERATED BY LINK'
);
$url = 'http://www.facebook.com/dialog/send?'.http_build_query($parameters);
echo '<script type="text/javascript">window.open('.json_encode($url).', "_blank", options, false);</script>';