Toni Michel Caubet
Toni Michel Caubet

Reputation: 20183

Get final graph picture url on facebook

Facebook image can be reached by

https://graph.facebook.com/{uid}/picture

The thing is that this is then redirected to a url like

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

So to avoid redirects (basically to get better google pagespeed insights) I started caching the "final" url, like this:

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

Where

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;
    }

}

And it was working great for a while, the thing is now facebook is forbiding access to this urls:

"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_"

And worst part is that the "fallback" i setted is never triggered even of the 403 status code

So the question is,

Do you know any workaround?

-EDIT-

A bit more of code:

$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) {
  }
}

Upvotes: 0

Views: 889

Answers (1)

EugeneMi
EugeneMi

Reputation: 3585

You can make an FQL query instead with the picture as one of the parameters - it will give you the most recent "final" link.

It will still require 2 calls, but you will avoid the redirect. Might be a bit faster.

Upvotes: 1

Related Questions