使用php检查facebook链接是否存在


check that facebook link exists using php

我有一个在facebook上添加帖子的用户。我想验证他们的链接,图像是张贴在这些链接。我使用curl,但它不能验证facebook的url,这可以通过facebook开发者API或任何其他方法。由于

$url = 'https://www.facebook.com/photo.php?fbid=620056421410941&set=a.405109092905676.94648.100002197659601&type=1&theater';
$handle = curl_init($url);
curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
    echo 'Not Available';
}
else
{
    echo 'Available';
}
curl_close($handle);

是的,你可以使用稍微hack的方法来做到这一点。

<?php
$url = 'https://www.facebook.com/photo.php?fbid=620056421410941&set=a.405109092905676.94648.100002197659601&type=1&theater';
$handle = curl_init($url);
$agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)';
curl_setopt($handle, CURLOPT_USERAGENT, $agent);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);  
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER , false);  //<-- Add this 
curl_setopt($handle, CURLOPT_FOLLOWLOCATION , true);  //<--- Add this too
$response = curl_exec($handle);
if(stripos($response,'This content is currently unavailable')!==false) { //<-- Inspect the source for this text
    echo 'Not Available';
}
else
{
    echo 'Available';
}
curl_close($handle);

解释:

  • 设置用户代理欺骗facebook,你正在使用一个有效的web浏览器
  • 您需要添加两个cURL参数,如图所示。
  • 检查源代码,而不是HTTP代码。