Reputation: 8877
I have created a Facebook Canvas App. This canvas app is essentially an image library, the user is able to view images by clicking a 'Next' button - pretty simple.
I am using the Graph API to post to a custom object when a user has 'looked at' an image. I do this like so (PHP SDK):
$this->data['facebook']->api('/me/myapp:look_at', 'post', array(
'picture' => current_url()
));
current_url() is the current URL of the Facebook canvas (https://apps.facebook.com/myapp/image/id). This page has a series of og meta tags:
<meta property="og:title" content="<?=$image->title?>" />
<meta property="og:description" content="Pic of <?=$image->title?>" />
<meta property="og:image" content="https://www.***.com/***/images/<?=$image->src?>" />
<meta property="fb:app_id" content="<?=FB_APP_ID?>" />
<meta property="og:url" content="<?=current_url();?>" />
<meta property="og:type" content="myapp:picture" />
I have read that when adding the image to the library I need to get Facebook to scrape the page for the og:tags. I do this using the following:
file_get_contents('https://graph.facebook.com?id=https://apps.facebook.com/myapp/image/'.url_title($_POST['title']).'/'.$data['image_id'].'&scrape=true');
However, I'm pretty certain that this isn't working, after placing a mail() call in the app, visiting that URL and waiting nothing comes through, suggesting that it isn't being scraped.
When running the first block of code (calling Graph API myapp:look_at) I get the following exception from Facebook:
Fatal error: Uncaught OAuthException: (#3502) Object at URL https://apps.facebook.com/myapp/image/img/id has og:type of 'website'. The property 'picture' requires an object of og:type 'myapp:picture'. thrown in /****/application/libraries/base_facebook.php on line 1106
If I use Facebook's debugger tool to scrape the page I get all of the info. If I then revisit the canvas page where the FB Graph API call is made then everything goes through fine. I have read on other SO posts that this is a common problem, and I need to ask Facebook to scrape the page before calling the Graph API, which is precisely what I'm trying to do in the third block of code above (file_get_contents) without luck.
I guess my question would be: Am I going about this in the right way? I am struggling to find any clear documentation on the process when it is used in this way.
If I am going about it the right way, why is my page not being scraped when calling file_get_contents('https://app....)?
Sorry for the long post, I'm trying to give you the full picture.
Thanks
Upvotes: 4
Views: 2672
Reputation: 23
You shouldn't need to do the manual scrape to get the object to register. According to the Facebook documentation:
When your app tries to make a connection between the user and the object via an action on Open Graph, Facebook will scrape the object webpage and read the meta tags to associate the user with the object. The object scrape occurs when:
- A user, for the first time, takes an action on the object (such as: like, listen, read, or any custom action on the object). Learn more
about how to publish actions.- List item
- Linting the object URL via the Object Debugger
- Linting the object URL via the Linter API Every 7 days after the first scrape
If the publishing code is called, the connected object will be scraped automatically if its the first time being referenced. Otherwise using the debugger, as you mentioned have already doing, will have the same effect.
EDIT-> However, there are some inconsistencies (as you mentioned) and developers noting their "fresh" objects not being scraped properly. Facebook not this bug and will hopefully sort it out soon. A work around seems to be to force linting the items using the debugger.
https://developers.beta.facebook.com/bugs/141650809281976/?browse=search_4f488911648042f90826189
Documentation: https://developers.facebook.com/docs/opengraph/objects/#connect
Hope this helps :) Good luck
Upvotes: 1
Reputation: 8877
Fixed it. I needed a user-agent check on the code that posts the Graph API to prevent that from posting when FB hits it:
if(strpos($_SERVER['HTTP_USER_AGENT'], 'facebookexternalhit') === false)
Upvotes: 0