1

所以我在这里为 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>";

?>
4

1 回答 1

1

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

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

于 2013-01-24T12:27:15.523 回答