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.