有没有一个API来强制Facebook再刮一个页面?

我知道你可以强制更新页面的caching,通过在Facebook的debugging工具上inputURL作为该应用/页面的pipe理员login: https : //developers.facebook.com/tools/debug

但是我需要的是当我们的销售部门有人更新我们的一个页面的主图像时自动调用API端点或者来自我们内部应用程序的一种方法 。 要求成千上万的销售人员以pipe理员身份login并手动更新页面的caching,无论何时更新我们的项目描述或图像,都不是一种select。

我们不能等待24小时让Facebook更新caching,因为我们每天都会收到我们客户的投诉,只要我们在我们一边改变它就看不到变化。

页面元数据不是那种经常改变的东西,但你可以通过转到Facebook的debugging工具并input你想要的URL来手动清除caching

还有一个API用于执行此操作, 适用于任何OG对象 :

curl -X POST \ -F "id={object-url OR object-id}" \ -F "scrape=true" \ -F "access_token={your access token}" \ "https://graph.facebook.com" 

现在需要一个access_token。 这可以是一个应用程序或页面access_token; 不需要用户authentication。

如果你想在不用等待回复的情况下在PHP中这样做,下面的函数将会这样做:

 //Provide a URL in $url to empty the OG cache function clear_open_graph_cache($url) { $vars = array('id' => $url, 'scrape' => 'true'); $body = http_build_query($vars); $fp = fsockopen('ssl://graph.facebook.com', 443); fwrite($fp, "POST / HTTP/1.1\r\n"); fwrite($fp, "Host: graph.facebook.com\r\n"); fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n"); fwrite($fp, "Content-Length: ".strlen($body)."\r\n"); fwrite($fp, "Connection: close\r\n"); fwrite($fp, "\r\n"); fwrite($fp, $body); fclose($fp); } 

如果你正在使用javascript sdk,这个你想要使用的版本是

 FB.api('https://graph.facebook.com/', 'post', { id: [your-updated-or-new-link], scrape: true }, function(response) { //console.log('rescrape!',response); }); 

我碰巧喜欢承诺,所以使用jQuery Deferreds的替代版本可能是

 function scrapeLink(url){ var masterdfd = $.Deferred(); FB.api('https://graph.facebook.com/', 'post', { id: [your-updated-or-new-link], scrape: true }, function(response) { if(!response || response.error){ masterdfd.reject(response); }else{ masterdfd.resolve(response); } }); return masterdfd; } 

然后:

 scrapeLink([SOME-URL]).done(function(){ //now the link should be scraped/rescraped and ready to use }); 

请注意,刮刀可能需要不同的时间来完成,所以不能保证它会很快。 我也不知道Facebook对这种方法的重复或自动化使用情况有什么看法,因此使用它可能是明智和保守的。

这是一个简单的ajax实现。 把这个放在你想要facebook的任何页面上,

 var url= "your url here"; $.ajax({ type: 'POST', url: 'https://graph.facebook.com?id='+url+'&scrape=true', success: function(data){ console.log(data); } }); 

在使用curl的Drupal节点更新中的替代解决scheme可能是这样的:

 <?php function your_module_node_postsave($node) { if($node->type == 'your_type') { $url = url('node/'.$node->nid,array('absolute' => TRUE)); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v1.0/?id='. urlencode($url). '&scrape=true'); $auth_header = 'Oauth yOUR-ACCESS-TOKEn'; curl_setopt($ch, CURLOPT_HTTPHEADER, array($auth_header)); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $r = curl_exec($ch); curl_close ($ch); } } 

注意hook_node_postsave()实现,它不是标准的Drupal核心支持。 我必须使用www.drupal.org/project/hook_post_action才能得到这个facebook抓取最后一次对节点进行的更改,因为在数据库更新后不会触发hook_node_update()。

Facebook需要现在的访问令牌才能完成。 获取令牌的准则可以在这里find: https : //smashballoon.com/custom-facebook-feed/access-token/

我正面临着同样的问题。 有一个简单的方法来清除caching。

  1. http://developers.facebook.com/tools/debug
  2. 通过fbrefresh = CAN_BE_ANYTHINGinput以下URL

例如: http : //www.example.com? fbrefresh= CAN_BE_ANYTHING

PHP Facebook SDK的解决scheme:

 <?php try { $params = [ 'id' => 'https://www.mysitetoscrape.com/page', 'scrape' => 'true', ]; $response = $fb->post('/', $params); print_r($response); } catch(\Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error echo 'Graph returned an error: ' . $e->getMessage(); } catch(\Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'Facebook SDK returned an error: ' . $e->getMessage(); } ?> 

我是Facebook Object Debugger CLI (用PHP编写的一个命令行界面)的作者,旨在使用input文本文件来刷新Facebookcaching中的单个URL或一组URL。 该软件包也可在Packagist上使用 ,并可使用Composer进行安装。

Graph API v2.10中有更改:

当我们之前没有抓取一个URL的GET请求时,我们也会忽略og_object字段。 要触发一次刮取并填充og_object,请发出POST / {url}?scrape = true。 一旦被抓取,og_object将保持caching并在所有未来的读取请求上返回。

从2017年10月16日开始,我们将在所有版本的Graph API中为这些请求提供访问令牌。

资料来源: Graph API v2.10简介

所以现在我们应该使用POST方法进行刮取:

POST /{url}?scrape=true