我已经建立了一个网站,我可以使用Facebook SDK 4从Facebook页面获取信息。该网站在使用WampServer 2.5的localhost上完美运行,但是因为我将它上传到免费托管网站(它运行PHP 5.4) ,Facebook URL没有返回任何信息,因为没有会话。
有什么想法吗?
<?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";
}
}
}
?>
我已编辑过该文件。
答案 0 :(得分:0)
将代码更改为:
<?php
define('FACEBOOK_SDK_V4_SRC_DIR', '../facebook-php-sdk-v4-4.0-dev/');
require_once(FACEBOOK_SDK_V4_SRC_DIR."autoload.php");
session_start();
// all use statements afterwards
答案 1 :(得分:0)
问题已解决,因为我已将代码上传到非免费托管网站。允许与Facebook SDK 4建立连接可能是主持人的问题,例如&#34; FacebookSession&#34;和&#34; FacebookRequest&#34;。