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
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: