PHP& Facebook:使用CURL和Facebook调试器调试一个URL [英] PHP & Facebook: facebook-debug a URL using CURL and Facebook debugger
问题描述
事实:我运行一个简单的网站,其中包含文章,通过抓取第三方网站/博客等动态获取的文章(新文章每隔半小时到达我的网站),文章我想在我的Facebook页面上发布。每篇文章通常包括图片,标题和一些文字。
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.
问题:我在Facebook上发布的文章中(几乎全部)没有正确发布 - 他们的图像丢失。
Problem: Most (almost all) of the articles that I post on Facebook are not posted correctly - their images are missing.
无效解决方案:使用Facebook的调试器(这个)我提交了一篇文章的URL(来自我的网站的URL,而不是原始的来源的URL),然后Facebook扫描/删除URL正确提取所需的信息(图像,标题,文本等)。在这个动作之后,这篇文章可以在Facebook上正确发布 - 没有丢失图像或任何东西。
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.
目标:我以后是一种方式创建一个过程,该过程将向Facebook的调试器提交一个URL,从而迫使Facebook扫描/浏览URL,以便可以正确地发布。我相信我需要做的是创建一个包含URL的HTML POST请求并将其提交给Facebook的调试器。这是正确的方式吗?如果是,由于我以前没有CURL的经验,在PHP中使用CURL的正确方法是什么?
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?
侧注 :作为一个附注,我应该提到我使用短的URL我的文章,虽然我不认为这是问题的原因,因为问题仍然存在,即使我使用规范的URL。
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.
此外,Open Graph元标记正确设置(og:image,og:description等)。
Also, the Open Graph meta tags are correctly set (og:image, og:description, etc).
推荐答案
您可以使用Facebook图形API使用 PHP-cURL
调试图形对象,方法是执行 POST
to
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;
}
虽然这会更新&清除通过的 URL
的Facebook缓存,打印出每个密钥&其内容并避免错误的同时,但是我建议使用 var_dump()
或 print_r()
OR PHP-ref
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
使用与 PHP-ref
r( facebookDebugger('http://retrogramexplore.tumblr.com/') );
这篇关于PHP& Facebook:使用CURL和Facebook调试器调试一个URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!