如何使用 Facebook Graph API 使用 PHP 获取用户的 location/City?

How to use Facebook Graph API to Get user's location/City using PHP?

我对使用 php 和 Facebook Graph api 还很陌生。目前,我正在测试一些代码以从 facebook 中提取用户的城市位置,但我收到 Class 'graphLocation' not found fatal 错误。我提取了用户名、电子邮件和图像,但该位置不起作用。这是代码..

<?php

/* INCLUSION OF LIBRARY FILEs*/
require_once( 'Facebook/FacebookSession.php');
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php');
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphUser.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
require_once( 'Facebook/Entities/AccessToken.php');
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookHttpable.php');
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php');

/* USE NAMESPACES */

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;
use Facebook\FacebookHttpable;
use Facebook\FacebookCurlHttpClient;
use Facebook\FacebookCurl;

/*PROCESS*/

//1.Stat Session
 session_start();

//check if users wants to logout
 if(isset($_REQUEST['logout'])){
    unset($_SESSION['fb_token']);
 }

//2.Use app id,secret and redirect url
$app_id = '**********';
$app_secret = '**************';
$redirect_url='http://www.citywits.com/facebookTest/';

//3.Initialize application, create helper object and get fb sess
 FacebookSession::setDefaultApplication($app_id,$app_secret);
 $helper = new FacebookRedirectLoginHelper($redirect_url);
 $sess = $helper->getSessionFromRedirect();

//check if facebook session exists
if(isset($_SESSION['fb_token'])){
    $sess = new FacebookSession($_SESSION['fb_token']);
}

//logout
$logout = 'http://www.citywits.com/facebookTest.php?logout=true';

//4. if fb sess exists echo name
    if(isset($sess)){
        //store the token in the php session
        $_SESSION['fb_token']=$sess->getToken();
        //create request object,execute and capture response
        $request = new FacebookRequest($sess,'GET','/me');
        // from response get graph object
        $response = $request->execute();
        $user = $response->getGraphObject(GraphUser::classname());
  $location = $response->getGraphObject(GraphLocation::classname());
        // use graph object methods to get user details
        $name = $user->getName();
        $id = $user->getId();
        $image = 'https://graph.facebook.com/'.$id.'/picture?width=300';
        $email = $user->getProperty('email');
  $address = $location->getCity();
        echo "hi $name <br>";
        echo "your email is $email <br>";
  echo "address is $address <br>";
        echo "<img src='$image' /><br><br>";
        echo "<a href='".$logout."'><button>Logout</button></a>";
    }else{
        //else echo login
        echo '<a href="'.$helper->getLoginUrl(array('email')).'" >Login with                            facebook</a>';
    }
?>

错误说找不到 'GraphLocation' class,但我这里有:

<?php
/**
 * Copyright 2014 Facebook, Inc.
 *
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
 * use, copy, modify, and distribute this software in source code or binary
 * form for use in connection with the web services and APIs provided by
 * Facebook.
 *
 * As with any software that integrates with the Facebook platform, your use
 * of this software is subject to the Facebook Developer Principles and
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
 * shall be included in all copies or substantial portions of the software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 */
namespace Facebook;

/**
 * Class GraphLocation
 * @package Facebook
 * @author Fosco Marotto <fjm@fb.com>
 * @author David Poll <depoll@fb.com>
 */
class GraphLocation extends GraphObject
{

  /**
   * Returns the street component of the location
   *
   * @return string|null
   */
  public function getStreet()
  {
    return $this->getProperty('street');
  }

  /**
   * Returns the city component of the location
   *
   * @return string|null
   */
  public function getCity()
  {
    return $this->getProperty('city');
  }

  /**
   * Returns the state component of the location
   *
   * @return string|null
   */
  public function getState()
  {
    return $this->getProperty('state');
  }

  /**
   * Returns the country component of the location
   *
   * @return string|null
   */
  public function getCountry()
  {
    return $this->getProperty('country');
  }

  /**
   * Returns the zipcode component of the location
   *
   * @return string|null
   */
  public function getZip()
  {
    return $this->getProperty('zip');
  }

  /**
   * Returns the latitude component of the location
   *
   * @return float|null
   */
  public function getLatitude()
  {
    return $this->getProperty('latitude');
  }

  /**
   * Returns the street component of the location
   *
   * @return float|null
   */
  public function getLongitude()
  {
    return $this->getProperty('longitude');
  }

}

非常感谢您的帮助!提前致谢!

您没有将其包含在您的文件中...

试试这个:

/* INCLUSION OF LIBRARY FILEs*/
require_once( 'Facebook/FacebookSession.php');
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php');
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphLocation.php' );
require_once( 'Facebook/GraphUser.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
require_once( 'Facebook/Entities/AccessToken.php');
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookHttpable.php');
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php');

/* USE NAMESPACES */

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphLocation;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;
use Facebook\FacebookHttpable;
use Facebook\FacebookCurlHttpClient;
use Facebook\FacebookCurl;