2016-03-08 66 views
1

場景:我正在通過一個應用程序,我需要從Facebook下載用戶的個人資料圖片,應用特定的過濾器,然後重新上傳和設置它作爲個人資料圖片,這是可能使用這個技巧。 'makeprofile = 1'保存用戶的個人資料圖片從Facebook的API - PHP SDK V.5

http://www.facebook.com/photo.php?pid=xyz&id=abc&makeprofile=1 

問題: 所以我面臨的問題是,同時通過API下載從接收到的URL的圖像。我獲得的圖片URL是這樣的:

$request = $this->fb->get('/me/picture?redirect=false&width=9999',$accessToken); // 9999 width for the desired size image 

// return object as in array form 
$pic = $request->getGraphObject()->asArray(); 

// Get the exact url 
$pic = $pic['url']; 

現在我想從獲得的URL將圖像保存到一個目錄我的服務器上,這樣我可以應用過濾器,並重新上傳。 當我使用的file_get_contents ($ PIC)它拋出以下錯誤

file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed 

我已經嘗試了一些其他的方法很好,但不能此問題得到解決。任何幫助將不勝感激:)

NOTE:我通過Codeigniter和本地主機現在這樣做。

+0

你可以上傳圖片成功回到Facebook的? – Kyslik

回答

0

所以我自己找到了解決方案,並決定回答,以便如果這可以幫助其他人面對同樣的問題。

我們需要一些參數傳遞給的file_get_contents()函數

$arrContextOptions=array(
       "ssl"=>array(
        "verify_peer"=>false, 
        "verify_peer_name"=>false, 
       ), 
); 
$profile_picture = @file_get_contents($profile_picture, false, stream_context_create($arrContextOptions)); 
// Use @ to silent the error if user doesn't have any profile picture uploaded 

現在$ profile_picture有圖片,我們可以在任何地方通過以下方式保存。

$path = 'path/to/img'; //E.g assets/images/mypic.jpg 

file_put_contents($path, $profile_picture); 

這一切:-)

0

可以使用file_get_connects()

$json = file_get_contents('https://graph.facebook.com/v2.5/'.$profileId.'/picture?type=large&redirect=false'); 
$picture = json_decode($json, true); 
$img = $picture['data']['url']; 

其中$簡檔變量包含用戶個人資料ID和 型paramete可以是方形,大,小,正常根據你的要求你想要的尺寸

現在$img變量包含您的圖像數據。使用file_put_contents()

$imagePath = 'path/imgfolder'; //E.g assets/images/mypic.jpg 
file_put_contents($imagePath, $img); 
+0

Pankaj,我在使用file_put_contents()函數時出錯,所以我的答案中提到的參數對我有用。謝謝你的回答,以及:-) –

2

你的問題節省服務器映像文件是this question一個潛在的重複。

我只是在這裏重複elitechief21's answer:您不應該禁用SSL證書,因爲這會在您的應用程序中創建一個安全漏洞。

但相反,你應該下載受信任的證書頒發機構(CA)(例如curl.pem)的列表,並與您的file_get_contents一起使用它,像這樣:

$arrContextOptions=array(
    "ssl"=>array(
     "cafile" => "/path/to/bundle/cacert.pem", 
     "verify_peer"=> true, 
     "verify_peer_name"=> true, 
    ), 
); 
$profile_picture = @file_get_contents($picture_url, false, stream_context_create($arrContextOptions)); 
相關問題