0

I am trying to save the picture which you get from the $FB->get on my server. So I can get that pic and make it a profile picture when login in with facebook. The only thing now is that I don't know how to save that pic on my server. I think I need to use copy or rename function from php or something like that. As you can see I tried using it but I have no clue how I need to fix this.

$oAuth2Client = $FB->getOAuth2Client();
   if (!$accessToken->isLongLived())
    $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);

$response = $FB->get("/me?fields=id, first_name, last_name, email,gender,link, birthday,picture.type(large)", $accessToken);
$userData = $response->getDecodedBody();


$fb_foto_url = $userData['picture']['data']['url']; 
copy($fb_foto_url ,'assets/images/profielfotos');

$voornaam= $app->get_klant_fb($userData['first_name']);
$_SESSION['voornaam'] = $userData['first_name'];
$_SESSION['achternaam'] = $userData['last_name'];

$fb_fotoUrl = $app->saving_fb_foto($userData['picture']['data']['url']);

How I am making a user based on facebook login.

//data ophalen
$klant= $app->get_klant_fb($userData['id']);
if($klant['id'] > 0) {
    //sessie klant zetten
    $_SESSION['klant_id'] = $klant['id'];
} else {
    //klant aanmaken
    unset($query);
    $query['oauth_uid']     = $userData['id'];
    $query['ledenpagina_id']= $_SESSION['ledenpagina_id'];
    $query['voornaam']      = $userData['first_name'];
    $query['achternaam']    = $userData['last_name'];
    $query['emailadres']    = $userData['email'];
    $query['geboortedatum'] = $userData['birthday'];
    $query['gender']        = $userData['gender']; 
    $query['link']          = $userData['link'];
    $app->insert_query('klanten', $query);

    $klant= $app->get_klant_fb($userData['id']);
    if($klant['id'] > 0) {
        $_SESSION['klant_id'] = $klant['id'];  
    }
}

This is what the develeper.facebook looks like

2 Answers2

0

If you have the URL which it looks like..

You can do a save..

file_put_contents("your/local/server/file.png", fopen($fb_foto_url, 'r'));

However it would make more sense to me, to store the Facebook URL in your database, and simply use that.. .

Ref : http://php.net/manual/en/function.file-put-contents.php

Pogrindis
  • 7,755
  • 5
  • 31
  • 44
  • well the link Ill get is a link which when I press it will download the image. So it will not show me the url in a tab or something like that. If I look at your answer it does `r` what is that? – helpmeifyoucann May 29 '18 at 09:34
  • @helpmeifyoucann the `r` simply means read only when open.. http://php.net/manual/en/function.fopen.php you're going to READ the file from the URL and save locally. Don't be afraid to test the code. – Pogrindis May 29 '18 at 09:52
0

Please try using the curl option to save the image to the server.

$fb_foto_url = $userData['picture']['data']['url']; 

$ch = curl_init($fb_foto_url);
$fp = fopen('assets/images/profielfotos/photo.ext', 'wb'); //Please print out the url and replace the .ext with extension returned such as jpg/png
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

//The image will be stored by now //Remove the copy function

But if you really don't want to edit the picture or something try getting it directly from the url so it will be automatically updated as well.

  • Can you make it so the code from you works for me? because I am not really getting what all of this does. (or maybe use comment so I know what to change in my case) – helpmeifyoucann May 29 '18 at 09:39