druzuz321103 2015-10-27 20:22 采纳率: 100%
浏览 61
已采纳

没有会话(Facebook SDK 4)

I have made a web site where I get and show information from Facebook pages using Facebook SDK 4. The site was working perfectly on localhost with WampServer 2.5, but since I uploaded it on a free hosting site (it runs PHP 5.4), the Facebook URLs are not returning any information because there is no session.

Any ideas?

<?php
    session_start();// Starting Session
    // storing session
    $user_name = $_SESSION['admin_profile']['username'];
    $user_pass = $_SESSION['admin_profile']['password'];

    // Establishing connection with server by passing server_name, user_id and password as a parameter
    $connection = mysqli_connect('xxx', 'xxx', 'xxx', 'xxx');

    // SQL query to fetch complete information of user
    $ses_sql = mysqli_query($connection, "SELECT * FROM admin WHERE username='$user_name' AND password='$user_pass' ");
    $rows = mysqli_fetch_assoc($ses_sql);

    $login_session = $rows['username'];

    // mysqli_close($connection); // Closing connection
    if(!isset($_SESSION['login_user']))
    {
        mysqli_close($connection); // Closing connection
        header('Location: ../control_panel.php'); // Redirecting to home page
    }

    // Include required files form Facebook SDK
    require_once( '../facebook-php-sdk-v4-4.0-dev/autoload.php' );
    require_once( '../facebook-php-sdk-v4-4.0-dev/src/facebook/HttpClients/FacebookHttpable.php' );
    require_once( '../facebook-php-sdk-v4-4.0-dev/src/facebook/HttpClients/FacebookCurl.php' );
    require_once( '../facebook-php-sdk-v4-4.0-dev/src/facebook/HttpClients/FacebookCurlHttpClient.php' );
    require_once( '../facebook-php-sdk-v4-4.0-dev/src/facebook/Entities/AccessToken.php' );
    require_once( '../facebook-php-sdk-v4-4.0-dev/src/facebook/Entities/SignedRequest.php' );
    require_once( '../facebook-php-sdk-v4-4.0-dev/src/facebook/FacebookSession.php' );
    require_once( '../facebook-php-sdk-v4-4.0-dev/src/facebook/FacebookRedirectLoginHelper.php' );
    require_once( '../facebook-php-sdk-v4-4.0-dev/src/facebook/FacebookRequest.php' );
    require_once( '../facebook-php-sdk-v4-4.0-dev/src/facebook/FacebookResponse.php' );
    require_once( '../facebook-php-sdk-v4-4.0-dev/src/facebook/FacebookSDKException.php' );
    require_once( '../facebook-php-sdk-v4-4.0-dev/src/facebook/FacebookRequestException.php' );
    require_once( '../facebook-php-sdk-v4-4.0-dev/src/facebook/FacebookOtherException.php' );
    require_once( '../facebook-php-sdk-v4-4.0-dev/src/facebook/FacebookAuthorizationException.php' );
    require_once( '../facebook-php-sdk-v4-4.0-dev/src/facebook/GraphObject.php' );
    require_once( '../facebook-php-sdk-v4-4.0-dev/src/facebook/GraphSessionInfo.php' );

    use Facebook\HttpClients\FacebookHttpable;
    use Facebook\HttpClients\FacebookCurl;
    use Facebook\HttpClients\FacebookCurlHttpClient;
    use Facebook\Entities\AccessToken;
    use Facebook\Entities\SignedRequest;
    use Facebook\FacebookSession;
    use Facebook\FacebookRedirectLoginHelper;
    use Facebook\FacebookRequest;
    use Facebook\FacebookResponse;
    use Facebook\FacebookSDKException;
    use Facebook\FacebookRequestException;
    use Facebook\FacebookOtherException;
    use Facebook\FacebookAuthorizationException;
    use Facebook\GraphObject;
    use Facebook\GraphSessionInfo;

    $db = new mysqli('xxx', 'xxx', 'xxx', 'xxx');
    if (mysqli_connect_errno()) {
         echo "Error: Could not connect to database.  Please try again later.";
         exit;
    }

    $print_result = "";
    $urlErr = "";
    $fb_url = "";

    $url_name = "";
    $url_picture = "";
    $url_link = "";



    /*---------------------Handle submit button----------------------*/
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["fb_url"])) {
            $urlErr = "S?µp????ste Url";
        }
        else {
            $fb_url = test_input($_POST["fb_url"]);
            $urlErr = "";
        }
    }

    if ($fb_url != ""){
        //INSERT Facebook link into database (Table: facebook_url)

        // Init app with app id and secret
        FacebookSession::setDefaultApplication( 'xxxxx','xxxxx' );


        $session = FacebookSession::newAppSession();
        $accessToken = $session->getAccessToken();
        $session = new FacebookSession($accessToken);

        if ( isset( $_SESSION ) && isset( $_SESSION['$accessToken'] ) ) {

            // Create new session from saved access_token
            $session = new FacebookSession( $_SESSION['$accessToken'] );

            // Validate the access_token to make sure it's still valid
            try {
                if ( !$session->validate() ) {
                    $session = null;
                }
            }
            catch ( Exception $e ) {

                // Catch any exceptions
                $session = null;
            }
        }
        $helper = new FacebookRedirectLoginHelper('http://mevent.gr/control_panel.php');



        if ( !isset( $session ) || $session === null ) {
            // No session exists
            try {
                $session = $helper->getSessionFromRedirect();
            }
            catch( FacebookRequestException $ex ) {
                // When Facebook returns an error
                // handle this better in production code
                print_r( $ex );
            }
            catch( Exception $ex ) {
                // When validation fails or other local issues
                // handle this better in production code
                print_r( $ex );
            }
        }


        if ( isset( $session ) ) {

            // Save the session
            $_SESSION['$accessToken'] = $session->getToken();

            // Create a session using saved token or the new one we generated at login
            $session = new FacebookSession( $session->getToken() );

            try {
                $response = (new FacebookRequest($session, 'GET', "/$fb_url"))->execute();
                $object = $response->getGraphObject();
                $url_id = $object->getProperty('id');

                //Retrieve id of URL
                $request = new FacebookRequest($session, 'GET', "/$url_id/?fields=name,picture,link");
                $response = $request->execute();
                $graphObject = $response->getGraphObject()->asArray();

                //Retrieve name, picture, link
                $url_name = $graphObject['name'];
                $url_picture = $graphObject['picture']->data->url;
                $url_link = $graphObject['link'];


                //INSERT 'url_id', 'fb_url' into table 'facebook_url'
                $url_id = mysqli_real_escape_string($db, $url_id);
                $fb_url = mysqli_real_escape_string($db, $fb_url);

                $query = "INSERT INTO facebook_url (url, id_url) VALUES
                ('$fb_url', '$url_id')";
                $result = $db->query($query);
                if ($result){
                    $print_result = "The facebook url inserted into database";
                }
                else{
                    $print_result = "The facebook url have already inserted into database.";
                }
            }
            catch (FacebookRequestException $ex) {
                //echo $ex->getMessage();
                //echo "Yes_1";
                $urlErr = "Invalid URL";
            }
            catch (\Exception $ex) {
                //echo $ex->getMessage();
                //echo "Yes_2";
                $urlErr = "Invalid URL";
            }
        }

    }

?>

I have edited the file.

  • 写回答

2条回答 默认 最新

  • dpli36193 2016-01-16 12:50
    关注

    The problem solved since I had uploaded the code on a non-free hosting site. Probably it was host's issue to allow connections with Facebook SDK 4, like "FacebookSession" and "FacebookRequest".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器