Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Facebook API - Force facebook to reload cache from your website
<?php
class FacebookDebugger
{
/*
* https://developers.facebook.com/docs/opengraph/using-objects
*
* Updating Objects
*
* When an action is published, or a Like button pointing to the object clicked,
* Facebook will 'scrape' the HTML page of the object and read the meta tags.
* The object scrape also occurs when:
*
* - Every 7 days after the first scrape
*
* - The object URL is input in the Object Debugger
* http://developers.facebook.com/tools/debug
*
* - When an app triggers a scrape using an API endpoint
* This Graph API endpoint is simply a call to:
*
* POST /?id={object-instance-id or object-url}&scrape=true
*/
public function reload($url)
{
$graph = 'https://graph.facebook.com/';
$post = 'id='.urlencode($url).'&scrape=true';
return $this->send_post($graph, $post);
}
private function send_post($url, $post)
{
$r = curl_init();
curl_setopt($r, CURLOPT_URL, $url);
curl_setopt($r, CURLOPT_POST, 1);
curl_setopt($r, CURLOPT_POSTFIELDS, $post);
curl_setopt($r, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($r, CURLOPT_CONNECTTIMEOUT, 5);
$data = curl_exec($r);
curl_close($r);
return $data;
}
}
?>
<?php
$fb = new FacebookDebugger();
$fb->reload('http://example.com/');
$fb->reload('http://example.com/foo');
$fb->reload('http://example.com/bar');
?>
@Allerby

This comment has been minimized.

Copy link

@Allerby Allerby commented Sep 16, 2015

Great! This is exactly what I needed. Thank you so much 👍

@benomatis

This comment has been minimized.

Copy link

@benomatis benomatis commented Oct 7, 2015

This indeed works really well, thanks!

@gdelcurla

This comment has been minimized.

Copy link

@gdelcurla gdelcurla commented Nov 8, 2015

Much appreciated! Great little piece of code.

@zigomario

This comment has been minimized.

Copy link

@zigomario zigomario commented Apr 30, 2016

Thanks.
But i have some trouble ...( noob php )
It's working if i use that :

    FB.api('https://graph.facebook.com/','post',  {
        id: 'http://example.fr/',
        scrape: true,
        access_token:'xxxxx|xxxxxx'
    }, function(response) {
        console.log('rescrape!',response);

    });

If i use ur code ( with ajax to send url ) :


    public function reload($url)
    {
        $token = 'xxxxxxx|xxxxxxxx';
        $graph = 'https://graph.facebook.com/';
        $post = 'id='.urlencode($url).'&scrape=true&access_token='.$token;

        return $this->send_post($graph, $post);
    }

It's not working.
In respons ajax

   $fb = new FacebookDebugger();
        $fb = $fb->reload($url)

        echo var_dump($fb);

I have " boolean false".

An idea ?

@Parrou

This comment has been minimized.

Copy link

@Parrou Parrou commented Jan 21, 2017

Thank you for this is working for me :)

@alexey-krivoshapko

This comment has been minimized.

Copy link

@alexey-krivoshapko alexey-krivoshapko commented Jan 26, 2017

It's the best way! Very thanks )

@pinetrees1

This comment has been minimized.

Copy link

@pinetrees1 pinetrees1 commented Jun 3, 2017

Why Fatal error: Class 'FacebookDebugger' not found when load usage.php?

@faridlab

This comment has been minimized.

Copy link

@faridlab faridlab commented Jun 12, 2017

@pinetress1 don't forget to include FacebookDebugger.php in usage.php

@VishnuGoyal

This comment has been minimized.

Copy link

@VishnuGoyal VishnuGoyal commented Jul 20, 2017

I am using WordPress and placed the code above (on line 1-43) in functions.php but did not get the desired results. Does anybody has idea where I am wrong?

@hariom7mahawar

This comment has been minimized.

Copy link

@hariom7mahawar hariom7mahawar commented Jul 21, 2017

how can we use this code in wordpress ?

@afeijo

This comment has been minimized.

Copy link

@afeijo afeijo commented Jul 22, 2017

Save the class php file into your wp theme folder, then in functions.php, add the class usage code and the line with your site url in a new function. Dont forget to add include 'FacebookDebugger.php' before the class is instantiated

@GirijaPugazhenthi

This comment has been minimized.

Copy link

@GirijaPugazhenthi GirijaPugazhenthi commented Aug 23, 2017

Is it possible to implement the same in c#??

@sayhicoelho

This comment has been minimized.

Copy link

@sayhicoelho sayhicoelho commented Oct 4, 2017

Whool! Thank you guy! This helped me a lot! :-)

@mikeskiddle

This comment has been minimized.

Copy link

@mikeskiddle mikeskiddle commented Oct 31, 2017

Is this still valid? I seem to have found that doing a POST via cURL throws an access token error, whereas calling via GET seems to work fine

@FDBenevides

This comment has been minimized.

Copy link

@FDBenevides FDBenevides commented Nov 8, 2017

@mikeskiddle it seems that recently it started throwing a HTTP 400 with the "access token required" message that you referred.
The thing is....with the post method it allowed us to force facebook to scrape and then refresh the graph's cache for the URL....with the get method it won't have the same behaviour :| according to the Facebook's specifications (on July,2017)

Previously, GET /{url} would trigger a scrape if that URL had not been encountered before. With v2.10, these requests will not trigger a scrape nor an update to the Open Graph object.
(...) When making a GET request against a URL we haven't scraped before, we will also omit the og_object field. To trigger a scrape and populate the og_object, issue a POST /{url}?scrape=true. Once scraped, the og_object will remain cached and returned on all future read requests.

We will require an access token for these requests in all versions of the Graph API beginning October 16, 2017.

@DrLightman

This comment has been minimized.

Copy link

@DrLightman DrLightman commented Jan 6, 2018

Exactly:

https://developers.facebook.com/blog/post/2017/07/18/graph-api-v2.10/

Since 16 Oct 2017 the code above shouldn't work anymore. An access token is required. Anyone has any idea how to accomplish that?

@samcilla

This comment has been minimized.

Copy link

@samcilla samcilla commented Jul 15, 2018

I need help fixing this Facebook open graph issue: The following properties are specified on the webpage but NOT supported for the specified 'og:type': article:publisher, article:author, article:tag, article:section, article:published_time

@pcbehara

This comment has been minimized.

Copy link

@pcbehara pcbehara commented Jul 27, 2018

Thanks for the code. However, the image doesn't seem to update. I was able to get the updated Title, Description and other properties excepting the og:image property. Any help here?

@MujurID

This comment has been minimized.

Copy link

@MujurID MujurID commented Jul 28, 2018

thx. i cron post https://graph.facebook.com/
id={URL}&scrape=true&access_token={TOKEN} access token permanent

@raazsandee

This comment has been minimized.

Copy link

@raazsandee raazsandee commented Aug 6, 2018

Hey, I had different cat images and i am changing the og:image when page loads.

<meta property="og:image"       content="http://example.com/images/<?php echo $randomCatImage; ?>" >

When i inspect the image is changing... but when i share or type my url... i am getting same preview (image)...
How to Fetch new scrape information when someone types or shares my url ?

@araeuchle

This comment has been minimized.

Copy link

@araeuchle araeuchle commented Mar 30, 2020

This works really really well and solved our problem with fb caching images.
Thank you so much!

@FrostyX

This comment has been minimized.

Copy link
Owner Author

@FrostyX FrostyX commented Mar 30, 2020

Thank you guys for all the positive feedback!

@luiscastillocr

This comment has been minimized.

Copy link

@luiscastillocr luiscastillocr commented May 20, 2020

can confirm this still works in 2020, here a python implementation extending the Facebook Graph SDK

class GraphAPIExt(GraphAPI):

    def scrape_url(self, url, **kwargs):

        assert url, "object URL is required"

        args = {
            "id": url,
            "scrape": True
        }

        args.update(kwargs)

        return self.request("/", method="POST", args=args)
@FrostyX

This comment has been minimized.

Copy link
Owner Author

@FrostyX FrostyX commented May 20, 2020

I wrote that code in high school nearly ten years ago (I published it later) and I would never guess that it would be still useful even today.
Thank all of you guys.

@aryeharmon

This comment has been minimized.

Copy link

@aryeharmon aryeharmon commented Oct 21, 2020

Must have a valid access token or a valid url_hmac

@linh-ebisolvn

This comment has been minimized.

Copy link

@linh-ebisolvn linh-ebisolvn commented Nov 10, 2020

can confirm this still works in 2020, here a python implementation extending the Facebook Graph SDK

class GraphAPIExt(GraphAPI):

    def scrape_url(self, url, **kwargs):

        assert url, "object URL is required"

        args = {
            "id": url,
            "scrape": True
        }

        args.update(kwargs)

        return self.request("/", method="POST", args=args)

does this update the image
I've tried another simple approach. get the response but it does not seem to update anything
python code:
res = requests.post(f'{host}/?id={uri}&scrape=true&access_token={token}')

@luiscastillocr

This comment has been minimized.

Copy link

@luiscastillocr luiscastillocr commented Nov 10, 2020

yes, this is what we are using right now in our production environment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment