Question

So I wrote this script here for PHP since I couldn't find anything anywhere about it. What this does is takes the thumbnail image codes for facebook and displays it. My question is testing if those images actually exist before displaying them, since Facebook defaults to an image of a question mark, I can't test for broken links. How would I uniquely identify the question mark image returned by Facebook if the one I'm referencing doesn't exist?

//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>";

?>
Was it helpful?

Solution

Facebook returns an is_silhouette field if you don't get a redirect. Take a look at this result: https://graph.facebook.com?fields=name,picture&ids=490550790968952,171108522930776

If you get this into PHP using cURL, then json_decode() this result, you'll be able to loop through it and test each id to see if it is one of Facebook's undefined images.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top