How to save facebook profile photo in the database?

4

I have a button that when I click, connects to facebook and displays the person's data in a registration form, in that form to a text field that returns the following: http://graph.facebook.com/'.$fb_id.'/picture?width=300

I would like to save the photo that the link returns me to the database. I was using file_put_contents('...',file_get_contents()) , but it is no longer working.

    
asked by anonymous 06.04.2015 / 18:07

2 answers

1

I used this class to solve my problem.

        class cUrl{
            public function file_get_contents_curl($url) 
            {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_HEADER, 0); 
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

                $data = curl_exec($ch);
                curl_close($ch);

                return $data;
            }
        }
    
07.04.2015 / 07:04
1

You can directly access the json of the image:

https://graph.facebook.com/4?fields=picture.width(750).height(750)

Example:

function getImageFacebook($id) {
 $url = 'https://graph.facebook.com/'.$id.'?fields=picture.width(300).height(300)';
          // faz a requisição a API passando a URL como parametro
          $json_string = file_get_contents($url);
          // usando a função json_decode e transformando em um array
          $json = json_decode($json_string, true);
          // retorna o número de likes
          echo '<pre>';
          print_r($json);
          echo '</pre>';

          $largura = $json['picture']['data']['width'];
          $altura = $json['picture']['data']['height'];
          $imagem = $json['picture']['data']['url'];
          echo '<img src="' . $imagem . '" width="' . $largura . '" height="' . $altura . '" border="0">';
        }
 getImageFacebook('67563683055');
    
17.09.2015 / 15:02