1

I have successfully created a custom Facebook open graph action, and have been able to post it from my facebook app. However, Facebooks API to post, was slowing my user experience down, so I created a queue table, that I want to execute from cron outside the user experience.

The table contains a field with the full post request, something like this:

https://graph.facebook.com/me/mynamespace:my_action?custom_item=https://www.mysite .com/thefolder/&access_token=XXXXXXX

So my cron worker will run every 2 mins.. How do I get PHP to run CURL (or something) on the above Asynchronously, i.e. not wait for facebook to respond, so I can churn through say 100 in a minute (should I be so lucky)?

oh and b) should I replace "/me/" with the persons fb user id?

Note, I am not a linux person. thanks!

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191

2 Answers2

1

However, Facebooks API to post, was slooooowing my user experience down

First of all, how is it slowing it down? Are you making a server-side or client-side call to the API?

If you’d do it client-side, it’d already be asynchronous, so there shouldn’t be much slowing down that would affect user experience.

How do I get PHP to run CURL (or something) on the above Asynchronously, i.e. not wait for facebook to respond, so I can churn through say 100 in a minute (should I be so lucky)?

I haven’t use cURL “asynchronously” yet, but a quick google search suggest that you can try it setting CURLOPT_TIMEOUT or on up-to-date systems even CURLOPT_TIMEOUT_MS to a value of 1. But I can’t tell you how reliable that’ll be; if Facebook’s API will process it correctly if the client (your server in this case) closes the connection quickly, etc.

I’m not sure if doing this by cron is such a good idea.

If you can’t do the API call client-side, as suggested above, because you require your app access token to be send for your actions to be published, then I’d suggest still doing it server-side, but not via cron, but triggered by the user himself, by embedding an IMG or SCRIPT element into you’re page (put out by PHP if you’re directing the user to a new page, or created and put into the DOM client-side via JavaScript), which has it’s source set as the address of your PHP script publishing the action. If you use a SCRIPT element you could even control the outcome of the operation and react to it client-side, by just returning appropriate JavaScript code in the response.

I’d think this approach should not slow down user experience noticably, and I’d also guess it might be more reliable, than “asynchronous” cURL request fired in the hundreds by a cron-called script.

oh and b) should I replace "/me/" with the persons fb user id?

With a user access token provided in the call the API can figure this out by itself.

But if you’re using an app access token, then you have to use the user-id, because otherwise the API has no way of knowing which user is meant in the call.

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • thanks for your super insight. It is slow, because I am using the php sdk api it adds about 10 secs, to my pageload, which impacts user experience. The img script idea, is an interesting way to fire the script, which even if the user clicks to another page, the post/request will still have been generated, and thus even if we dont get a response, it doesn't matter then action is posted. thanks! – Paolo Di Terlizzi Jun 23 '12 at 12:42
0

You should store the user session somewhere(db, etc). Then you can send through cron worker as long as the user session is active. And use the token while posting actions. (you should use token belonging to user only :))

b) should I replace "/me/" with the persons fb user id?

then you can use /me or /id

To make the aynbchronus http call, see this post Asynchronous PHP calls?

But there is rate limit, which would stop you from posting based on this condition (600 calls per 600 seconds, per token & per IP to be about where they stop you)
Source: http://www.quora.com/Whats-the-Facebook-Open-Graph-API-rate-limit

Community
  • 1
  • 1
Venu
  • 7,243
  • 4
  • 39
  • 54
  • That's really helpful, thank you. I had tried previously using that snippet. I will have another bash, I think my params were not seperated correctly then. thanks. – Paolo Di Terlizzi Jun 23 '12 at 12:38