ccc

API Facebook: Obtener datos al loguearte y obtener la fotografía grande del usuario

<html>
<body>
<button class='facebook-btn' onclick="ingresar();"><i class="fa fa-facebook"></i>Ingresar con Facebook</button>
</body>
<script src="//code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
    window.fbAsyncInit = function () {
        FB.init({
            appId: 'xxxx',
            xfbml: true,
            cookie : true,
            version: 'v2.7'
        });
    };
    (function (d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) {
            return;
        }
        js = d.createElement(s);
        js.id = id;
        js.src = "//connect.facebook.net/en_US/sdk.js";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));

    var idFB = 0;
    function ingresar() {
        FB.login(function (response) {
            FB.getLoginStatus(function (response) {
                if (response.status == 'connected') {
                    FB.api('/me',{
                        fields: 'first_name,last_name,age_range,link,gender,locale,picture,timezone,updated_time,verified,email,cover'
                    }, function (response) {
                        idFB = response.id;
                        FB.api(
                            "/"+idFB+"/picture?height=960",
                            function (response) {
                                console.log(idFB);
                                console.log(response);
                                $.ajax({
                                    type: 'POST',
                                    url: './guardarFoto.php',
                                    data: {
                                        fotoFB: response.data.url
                                    },
                                    dataType: 'jsonp',
                                    complete: function () {
                                    }
                               })
                            }
                        );
                    });

                } else if (response.status == 'not_authorized') {
                    alert('Debes autorizar la app!');
                } else {
                    alert('Debes ingresar a tu cuenta de Facebook!');
                }
            });
        }, {scope: 'public_profile, email',return_scopes: true});
    }
</script>

En guardarFoto.php:
<?php
$img = './la_foto.jpg';
file_put_contents($img, file_get_contents($_POST["fotoFB"]));
?>

No hay comentarios:

Publicar un comentario