Is it possible to do batch POST API calls? - facebook

I'm writing an app that makes a daily post as a user, and having benchmarked the PHP code that does this, it seems to take about two seconds per user. I'm dividing the work up in to chunks, and using multiple cron jobs to do each chunk. I'd like to scale to many thousands of users one day, but this kind of work load is just too much. It would take my server literally all day to post to each user one at a time using this method.
How do people normally do this? I've seen other apps that do it. Is there some way of sending all these posts off at once using just one API call? Using individual API calls per user is crazy slow.
Thanks.

On one hand, this is entirely dependent on the API.
However, you could use a multi-threaded or pseudo-parallel approach to this, such that your program sends, say, 100 HTTP POST requests at a time, rather than generating one request after another in series.
Since you're using PHP, multi-threading is out (I think) but this question is very similar to others. For example, see how these folks recommend curl_multi.

You can use batch query to achieve what you need.
The code for batch query is mentioned below. You can refer more about Facebook batch query at : http://25labs.com/tutorial-post-to-multiple-facebook-wall-or-timeline-in-one-go-using-graph-api-batch-request/
$body = array(
'message' => $_POST['message'],
'link' => $_POST['link'],
'picture' => $_POST['picture'],
'name' => $_POST['name'],
'caption' => $_POST['caption'],
'description' => $_POST['description'],
);
$batchPost[] = array(
'method' => 'POST',
'relative_url' => "/{ID1}/feed",
'body' => http_build_query($body) );
$batchPost[] = array(
'method' => 'POST',
'relative_url' => "/{ID2}/feed",
'body' => http_build_query($body) );
$batchPost[] = array(
'method' => 'POST',
'relative_url' => "/{ID3}/feed",
'body' => http_build_query($body) );
$multiPostResponse = $facebook->api('?batch='.urlencode(json_encode($batchPost)), 'POST');

Related

How I can attach multiple image to facebook feed message?

Right now I use this method to add feed message:
$response = (new FacebookRequest(
$session, 'POST', '/me/feed', array(
'link' => $this->cafe->getUrlForPost(),
'name' => $this->cafe->getTextForPost(),
'picture' => 'http://' . $_SERVER['HTTP_HOST'] . $this->getImageForPost()
)
))->execute()->getGraphObject();
Is there any way to add a multiple image in picture field? Or maybe do you have a workaround for this?
Thank you for your answers.
Unfortunately, right now you canĀ“t add more than one image in one API call. You can only do multiple calls and you can use Batch Requests for it: https://developers.facebook.com/docs/graph-api/making-multiple-requests

How to access two similar but located in different API in a single Apigility project?

Suppose we do have an Apigility URL like localhost:8888/user.
But I do have two API's in a single Apigility project, ApiOne and ApiTwo.
Doing a GET request from localhost:8888/user always returns resources generated by ApiTwo. But when I want to get the same user resource rom ApiOne because it is from a different DB I can't retrieve it. I already tried to supply the Accept media type with the proper version (hoping it would help), to access ApiOne (e.g. Accept application/vnd.apione.v1+json)
I'm guessing based on your question that you've got ApiOne with a route to /user and ApiTwo with a route to /user?
The problem is that they are not different routes. ZF2 is going to use (I believe) the one that is defined last in the combined configuration file. If you want to be able to use both API resources, you'll need to have different routes somehow. This could be literally changing one of them to something like /user1.
You could also theoretically route to two different endpoints for entities if you provide constraints on the id that would make them differentiable. What I mean is you could have both routes like this:
/api/user[/:user_id]
/api/user[/:user_name]
As far as the ZF2 router is concerned, at this point, those routes are identical. The variable parts user_id and user_name could be the same thing.
However, you'd need something else that makes the routes different. Apigility (and ZF2) allow for constraints on the parts of the URL. So you could put a constraint on user_id of [0-9]+ and a constraint on user_name of [a-z]+. These would make the routes mutually exclusive for entities, but the collection version of the routes is still the same.
Constraints are added in the route under options like so:
'router' => array(
'routes' => array(
'your-api.rest.user' => array(
'type' => 'Segment',
'options' => array(
'route' => '/user[/:user_id]',
'defaults' => array(
'controller' => 'YourApi\\V1\\Rest\\User\\Controller',
),
'constraints' => array(
**'user_id' => '[0-9]+'**,
),
),
),
'your-api.rest.username' => array(
'type' => 'Segment',
'options' => array(
'route' => '/user[/:user_name]',
'defaults' => array(
'controller' => 'YourApi\\V1\\Rest\\Username\\Controller',
),
'constraints' => array(
**'user_name' => '[a-z]+'**,
),
),
),
),
),
)
In this example, if no user_id or user_name were provided, the Username resource should be called since it is defined last.
Overall though, my recommendation would be that you change the routes so that they are different. The accept header with version is essentially used for versioning, not routing to a different API resource.

Is there a way to make a Zend Framework Application to act like Facebook in profile links?

My accurate question would be, is there any routes that can make possible that when i go
mydomain.com/profilename
it'd be redirect to the profile controllers instead of index, and sitll, if i provide no parameter, he'd load the index page, and even still, if theres a controller by that name, that he'd run that controller instead of searching for a profile...
Pretty much complicated i know, that's why i'm asking for your help, you geniouses! <3
Thanks in advance, Jorge.
ZF1 doesn't have route priority as such, but routes are matched LIFO (last in, first out). So as long as you were able to hard code controller names into your routes, and put this after your profile route, you could do something like this:
$router->addRoute('profile',
new Zend_Controller_Router_Route('/:profilename', array(
'module' => 'default',
'controller' => 'profile',
'action' => 'view'
))
);
$router->addRoute('something',
new Zend_Controller_Router_Route('/:controller/:action', array(
'module' => 'default',
'action' => 'index'
), array(
'controller' => '(foo|bar)' // names of your controllers
))
);
Alternatively, if this isn't possible, or you want a more robust (but more difficult) solution, I wrote a blog post a while back with a detailed explanation of how to achieve this with a custom route class: http://tfountain.co.uk/blog/2010/9/9/vanity-urls-zend-framework

Get nested ressources when using cakePhp restfull

I'm using cakePhp to create a Rest api (see http://book.cakephp.org/2.0/fr/development/rest.html) and I need to get nested resources. The documentation tells how to get let's say books implementing a URI /books.json. But does not tell how to get for example reviews for a given book. What I'm trying to make is somthing like this: /books/14/reviews.json that returns Review resources.
Can any one tell me hwo to make this?
See the Custom REST Routing section of the docs you've linked. In case the default routing doesn't work for you, you'll have to create your own custom routes that either replace or extend the default ones.
Your /books/14/reviews.json URL could for example be mapped to BooksController::reviews() likes this:
Router::connect(
'/books/:id/reviews',
array(
'[method]' => 'GET',
'controller' => 'books',
'action' => 'reviews'
),
array(
'id' => Router::ID . '|' . Router::UUID,
'pass' => array(
'id'
)
)
);
When placed before Router::mapResources() it should work fine together with the default routes.

Zend Framework - adding a custom field to the session handler?

hey all I'm using the Zend_Session_SaveHandler_DbTable Session handler and I was curious if anyone knows how to best add a custom field to the table that it stores the session data in? for example I'd like to be able to log the username of the person in the sessions table.
Anyone know if that's possible with the Zend_Session_SaveHandler_DbTable?
here's my set up code now..
$config = array(
'name' => 'session',
'primary' => 'id',
'modifiedColumn' => 'modified',
'dataColumn' => 'data',
'lifetimeColumn' => 'lifetime'
);
Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config));
Zend_Session::start();
i don' think it's possible, i have been searching for the same thing, but if you look at http://tig12.net/downloads/apidocs/zf/Session/SaveHandler/Zend_Session_SaveHandler_DbTable.class.html i don't see anything one could use,
i guess the way to do it, is to build another table, with session id as one column and any other columns you would want