PHP & Facebook: facebook-debug a URL using CURL and Facebook debugger

curl, facebook, facebook-graph-api, php

Solution

You can debug a graph object using Facebook graph API with `PHP-cURL`, by doing a `POST` to

https://graph.facebook.com/v1.0/?id={Object_URL}&scrape=1

to make thing easier, we can wrap our debugger within a function:

function facebookDebugger($url) {

        $ch = curl_init();
              curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v1.0/?id='. urlencode($url). '&scrape=1');
              curl_setopt($ch, CURLOPT_POST, 1);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
              curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $r = curl_exec($ch);
        return $r;

}

though this will update & clear Facebook cache for the passed `URL`, it's a bit hard to print out each key & its content and avoid errors in the same time, however I recommended using `var_dump()` or `print_r()` OR PHP-ref

usage with `PHP-ref`

r( facebookDebugger('http://retrogramexplore.tumblr.com/') );

Problem

Facts: I run a simple website that contains articles, articles dynamically acquired by scraping third-party websites/blogs etc (new articles arrive to my website every half an hour or so), articles which I wish to post on my facebook page. Each article typically includes an image, a title and some text. Problem: Most (almost all) of the articles that I post on Facebook are not posted correctly - their images are missing. Inefficient Solution: Using Facebook's debugger (this one) I submit an article's URL to it (URL from my website, not the original source's URL) and Facebook then scans/scrapes the URL and correctly extracts the needed information (image, title, text etc). After this action, the article can be posted on Facebook correctly - no missing images or anything. Goal: What I am after is a way to create a process which will submit a URL to Facebook's debugger, thus forcing Facebook to scan/scrape the URL so that it can then be posted correctly. I believe that what I need to do is to create an HTML POST request containing the URL and submit it to Facebook's debugger. Is this the correct way to go? And if yes, as I have no previous experience with CURL, what is the correct way to do it using CURL in PHP? Side Notes: As a side note, I should mention that I am using short URLs for my articles, although I do not think that this is the cause of the problem because the problem persists even when I use the canonical URLs. Also, the Open Graph meta tags are correctly set (og:image, og:description, etc).

Original source