How to save facebook profile photo in database?

Asked

Viewed 1,388 times

4

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

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

  • 1

    I do not know if I am correct, but I believe that could save the image in a folder and in the database save the path of that folder and click on the form the image called the path recorded in the database.

  • Anderson, thanks for the comment! Imagine this: I get the url: http://graph.facebook.com/'. $fb_id. '/picture? width=300 i would like to know how to save this image in the database!

  • Take a look at this topic who knows how to help you http://answall.com/questions/16396/inser-imagem-no-banco-de-data

  • file_put_contents? What error appears? would be the http:// wrapper is disabled in the server configuration by allow_url_fopen=0?

  • This ae William. I manage to solve my problem by using a class.

2 answers

1

You can directly access the image json:

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');

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;
            }
        }
  • if this is the correct answer, then mark as the answer accepted.

  • @Eduardoseixas, look my, answer, I believe you will have a better use than using php’s Curl function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.