file_get_contents的替代方案,用于从图形API中检索信息

I am developing my first facebook which has now being completed, in my app ive used file_get_contents function to retrieve content from graph api:

$userName =  json_decode(file_get_contents('http://graph.facebook.com/' . $userId)) -> name;

But problem is that the host that I am now using doesn’t allow the use of file_get_contents function.

  • So I want to know what other alternatives I can use to retrieve information from graph api beside this?
  • From googling i found out the facebook's php sdk provides different alternatives for it. Ive include ('src/facebook.php'); included in my code but i am not sure how to use that or is there a way i can user the sdk to overcome my problem?

Kindly help with this.

Thankyou.

The best solution is to use Facebook PHP SDK.

The code will be similar to:

$app_id     = "Your App ID/API Key goes here";
$app_secret = "Your App secret goes here";
$site_url       = "Your Site URL goes here";

include_once "src/facebook.php";

$facebook = new Facebook(array(
    'appId'     => $app_id,
    'secret'    => $app_secret,
    ));

$user = $facebook->getUser();

if($user){
    try{
        $user_profile = $facebook->api('/me');
    }catch(FacebookApiException $e){
        $user = NULL;
    }
}

if($user){
    $logoutUrl = $facebook->getLogoutUrl();
}else{
    $loginUrl = $facebook->getLoginUrl(array(
        'scope'     => 'read_stream publish_stream email',
        'redirect_uri'  => $site_url,
        ));
}

You can find a detailed tutorial at http://25labs.com/tutorial-integrate-facebook-connect-to-your-website-using-php-sdk-v-3-x-x-which-uses-graph-api/