PHP下载Facebook用户图像配置文件错误


PHP Download Facebook User Image Profile Error

帮助我解决此问题我想使用PHP 5.3.27下载Facebook用户图像配置文件我使用此代码,图像已下载,但大小为0字节,无法打开。

第一个代码:

function DownloadImage($url, $dest){
    $curl = curl_init($url);
    $fp = fopen($dest, 'wb');
    curl_setopt($curl, CURLOPT_FILE, $fp);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_exec($curl);   
    curl_close($curl);
    fclose($fp);    
}
try{   
    $social_id='1234';
    DownloadImage('http://graph.facebook.com/'.$social_id.'/picture', '../user_image/normal/'.$social_id.'.jpg');
    DownloadImage('http://graph.facebook.com/'.$social_id.'/picture?width=141&height=141', '../user_image/bigger/'.$social_id.'.jpg');  
}
catch(Exception $errr){
    echo $errr;
}

这是第二个代码。。具有相同的结果(0字节大小,无法打开)

$url = 'http://graph.facebook.com/1234/picture';
$img = '../user_image/bigger/1234.jpg';
file_put_contents($img, file_get_contents($url));

如我所见

http://graph.facebook.com/1234/picture

重定向到:

https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash2/t1.0-1/c47.7.85.85/s50x50/417599_10100236041078581_1583446385_a.jpg

尝试添加:curl_setopt($curl,CURLOPT_FOLLOWLOCATION,true);

TRUE表示在服务器作为HTTP标头的一部分发送的任何"Location:"标头后面(注意这是递归的,PHP将在发送的"Location:"标头后面,除非设置了CURLOPT_MAXREDIRS)。

所以你的代码可能看起来像:

function DownloadImage($url, $dest){
    $curl = curl_init($url);
    $fp = fopen($dest, 'wb');
    curl_setopt($curl, CURLOPT_FILE, $fp);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($curl);   
    curl_close($curl);
    fclose($fp);    
}
try{   
    $social_id='1234';
    DownloadImage('http://graph.facebook.com/'.$social_id.'/picture', '../user_image/normal/'.$social_id.'.jpg');
    DownloadImage('http://graph.facebook.com/'.$social_id.'/picture?width=141&height=141', '../user_image/bigger/'.$social_id.'.jpg');  
}
catch(Exception $errr){
    echo $errr;
}