vendredi 29 mai 2015

Php upload to Facebook without Facebook Login

I need to do a form to upload photos with name and description, but I want to upload the photo at the same time to a Facebook fanpage, with the same form. I don't want to login to facebook before uploading the image, I just want to write the credentials in the php and work with this. I have tried it but I can't get the facebook session (and I don't even know that if is possible to do this without login).

Thanks in advance

            //PAGE ID: ********


            define('FACEBOOK_SDK_V4_SRC_DIR', '/complementos/facebook-php-sdk-v4-4.0-dev/src/Facebook/');
            require __DIR__ . '/complementos/facebook-php-sdk-v4-4.0-dev/autoload.php';


            require_once( 'complementos/facebook-php-sdk-v4-4.0-dev/src/Facebook/FacebookSession.php' );

            // Make sure to load the Facebook SDK for PHP via composer or manually
            require_once( 'complementos/facebook-php-sdk-v4-4.0-dev/src/Facebook/FacebookRequest.php' );
            require_once( 'complementos/facebook-php-sdk-v4-4.0-dev/src/Facebook/GraphObject.php' );

            use Facebook\FacebookSession;
            use Facebook\FacebookRequest;
            use Facebook\GraphObject;

            session_start();
            FacebookSession::setDefaultApplication('********', '******');

            if($session) {

              try {
                    // uploading image to user timeline using facebook php sdk v4
                    $response = (new FacebookRequest(
                        $session, 'POST', '/me/photos', array(
                            'source' => new CURLFile('img/h_index.jpg', 'image/jpg'), // photo must be uploaded on your web hosting
                            'message' => 'User provided message'
                            )
                        )
                    )->execute()->getGraphObject();
                    if($response) {
                        echo "Photo is uploaded...";
                    }

                } catch(FacebookRequestException $e) {
                    echo $e->getMessage();
                }

            }

            else
            {
                echo "no entra en session;";
            }

Aucun commentaire:

Enregistrer un commentaire