0
Facebook的圖像可以通過獲得在Facebook上最後的圖形畫面網址
https://graph.facebook.com/{uid}/picture
事情達到的是,這然後被重定向到URL像
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
因此,爲了避免重定向(基本上得到我開始緩存「最終」網址,如下所示:
$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個狀態碼
所以現在的問題就是,
你知道什麼解決辦法?
CNC中
多一點的代碼:
$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) {
}
}
嗨,謝謝你的回答!我用更多的代碼編輯了問題,以便知道我在什麼階段請求「最終」url。所以你的意思是在緩存時做fql查詢,或者說allways? – 2014-09-02 20:58:05
每一次。我會想象最終的url會隨着時間的推移而變化,所以很難在很長一段時間內緩存它。 – EugeneMi 2014-09-02 21:09:24
看看這個,看看你會從fql調用中得到什麼。 https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=me%3Ffields%3Did%2Cname%2Cpicture&version=v2.1 – EugeneMi 2014-09-02 21:09:51