在Facebook上获取最终的图形图片网址

时间:2014-09-02 20:43:12

标签: php image facebook-graph-api redirect http-status-code-403

可以通过

访问Facebook图像
https://graph.facebook.com/{uid}/picture

事情是,然后将其重定向到类似

的网址
https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xfa1/v/t1.0-1/c0.0.50.50/p50x50/10264431_865630423451069_7413030211804680043_n.jpg?oh=96840deb90665b0b5ee39a77591fb521&oe=546D8360&__gda__=1416463680_c

所以为了避免重定向(基本上是为了获得更好的google pagespeed见解)我开始缓存“最终”网址,如下所示:

$preview = get_redirect_url('https://graph.facebook.com/'.$uid.'/picture');
if(!$preview){ 
        /* fallback */
        $preview = 'https://graph.facebook.com/'.$uid.'/picture';
}

其中

function get_redirect_url($url){
    $redirect_url = null;

    $url_parts = @parse_url($url);
    if (!$url_parts) return false;
    if (!isset($url_parts['host'])) return false; //can't process relative URLs
    if (!isset($url_parts['path'])) $url_parts['path'] = '/';

    $sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80), $errno, $errstr, 30);
    if (!$sock) return false;

    $request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1\r\n";
    $request .= 'Host: ' . $url_parts['host'] . "\r\n";
    $request .= "Connection: Close\r\n\r\n";
    fwrite($sock, $request);
    $response = '';
    while(!feof($sock)) $response .= fread($sock, 8192);
    fclose($sock);

    if (preg_match('/^Location: (.+?)$/m', $response, $matches)){
        if ( substr($matches[1], 0, 1) == "/" )
            return $url_parts['scheme'] . "://" . $url_parts['host'] . trim($matches[1]);
        else
            return trim($matches[1]);

    } else {
        return false;
    }

}

它的工作时间很长,现在Facebook正在禁止访问这个网址:

"NetworkError: 403 Forbidden - https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xap1/v/t1.0-1/c0.0.50.50/p50x50/10645271_1540579909486778_4851323870004250654_n.jpg?oh=10622ec9a388b684a1232afff0c11389&oe=547A2A4D&__gda__=1415381628_"

最糟糕的是,我设置的“后备”永远不会触发403状态代码

所以问题是,

你知道任何解决方法吗?

- 编辑 -

更多代码:

$session = $facebook->getUser()>0;
$access_token = 'access_token='.$facebook->getAccessToken();
$app_access_token = file_get_contents('https://graph.facebook.com/oauth/access_token?client_id=123456789&client_secret=a1b2c3d4e5f6&grant_type=client_credentials');
$me = null;
if ($session) {
  try {
        $uid = $facebook->getUser();
        $me = $facebook->api('/me');
        /*Obtener info del usuario*/
        $pageContent = file_get_contents("https://graph.facebook.com/".$uid);
        $parsedJson  = json_decode($pageContent, true);
        /*Agregar a base de datos si no existe*/
        $r = new registro_usuarios();
        $preview = get_redirect_url('https://graph.facebook.com/'.$uid.'/picture');
        if(!$preview){ 
        $preview = 'https://graph.facebook.com/'.$uid.'/picture';
        }
        $r->facebook($uid,$me['name'],$preview,$me['email']); /* register user */
   } catch (FacebookApiException $e) {
  }
}

1 个答案:

答案 0 :(得分:1)

您可以使用图片作为参数之一制作FQL query - 它会为您提供最新的"最终"链接。

它仍然需要2次通话,但您将避免重定向。可能会快一点。