【问题标题】:Facebook Chat Open Graph Image Code ParsingFacebook Chat Open Graph 图像代码解析
【发布时间】:2013-01-24 10:22:30
【问题描述】:

所以我在这里为 PHP 编写了这个脚本,因为我在任何地方都找不到任何关于它的东西。 这样做是获取 facebook 的缩略图图像代码并显示它。我的问题是在显示这些图像之前测试它们是否真的存在,因为 Facebook 默认使用问号图像,我无法测试断开的链接。如果我引用的问号图像不存在,我将如何唯一识别 Facebook 返回的问号图像?

//define our test subject
$testvar = "[[490550790968952]][[490550800968951]][[490550807635617]]  
[[490550820968949]][[490550837635614]][[490550847635613]]  
[[490550857635612]][[490550867635611]][[490550877635610]]  
[[490550884302276]][[490550894302275]][[490550900968941]]  
[[490550914302273]][[490550924302272]][[490550937635604]]  
[[490550947635603]][[490550954302269]][[490550967635601]]";

//this one is a broken link and generates the question mark, remove this line to test the above code
$testvar = "[[171108522930776]]";

//convert new lines to <br>
$testvar = nl2br($testvar);

//replace all numbers with links
$numfilter = "/([0-9]){1,}/"; //match all number blocks without brackets
$testvar = preg_replace($numfilter, "<img src='https://graph.facebook.com/\\0/picture'>", $testvar);

//strip the brackets
$stripbrackets = array("[[","]]");
$testvar = str_replace($stripbrackets,"",$testvar);

//echo a div with the output, setting font-size:0 so the <br> tags don't create vertical spacing between new lines
echo "<div style='font-size:0px; line-height:0px;'>";
echo $testvar;
echo "</div>";

?>

【问题讨论】:

    标签: php facebook-graph-api facebook-chat


    【解决方案1】:

    如果您没有收到重定向,Facebook 会返回一个 is_silhouette 字段。看看这个结果:https://graph.facebook.com?fields=name,picture&ids=490550790968952,171108522930776

    如果您使用 cURL 将其输入 PHP,然后 json_decode() 这个结果,您将能够遍历它并测试每个 id 以查看它是否是 Facebook 的未定义图像之一。

    【讨论】:

    • 完美!非常感谢。我会将此标记为答案。我将使用它来测试较大请求集群中的第一个图像,因此我不会为较大图像的每个部分生成 json 请求。我只需要假设一个存在,其余的应该存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 2016-02-01
    • 2012-09-10
    • 1970-01-01
    • 2012-02-21
    相关资源
    最近更新 更多