Facebook v5 PHP API上传图片

时间:2015-08-07 12:22:30

标签: php facebook

我有这段代码:

<?php
session_start();
date_default_timezone_set('Asia/Manila');
$base_path = __DIR__ . '/';
require_once($base_path . 'facebook-sdk-v5/autoload.php');
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
$fb = new Facebook\Facebook([
  'app_id' => '{appId}',
  'app_secret' => '{secret}',
  'default_graph_version' => 'v2.3',
]);

$helper = $fb->getRedirectLoginHelper();
try {
  $accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  // When validation fails or other local issues
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
} catch(Exception $e){
    echo $e->getMessage();
    exit;
}
if (! isset($accessToken)) {
  if ($helper->getError()) {
    header('HTTP/1.0 401 Unauthorized');
    echo "Error: " . $helper->getError() . "\n";
    echo "Error Code: " . $helper->getErrorCode() . "\n";
    echo "Error Reason: " . $helper->getErrorReason() . "\n";
    echo "Error Description: " . $helper->getErrorDescription() . "\n";
  } else {
    header('HTTP/1.0 400 Bad Request');
    echo 'Bad request';
}
exit;
}

// Logged in
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());

// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();

// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
echo '<h3>Metadata</h3>';
var_dump($tokenMetadata);

// Validation (these will throw FacebookSDKException's when they fail)
/*try{
    $tokenMetadata->validateAppId($config['app_id']);
} catch(Facebook\Exceptions\FacebookSDKException $e){
    echo '<br>Facebook SDK returned an error: ' . $e->getMessage();
    exit;
} catch(Exception $e) {
    echo $e->getMessage();
    exit;
}
echo '1<br>';
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();
echo '2<br>';*/
if (! $accessToken->isLongLived()) {
  // Exchanges a short-lived access token for a long-lived one
  try {
    $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
  } catch (Facebook\Exceptions\FacebookSDKException $e) {
    echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
    exit;
  }
  echo '<h3>Long-lived</h3>';
  var_dump($accessToken->getValue());
} else {
    echo '<h3>the King!</h3>';
}

$_SESSION['fb_access_token'] = (string) $accessToken;
echo $_SESSION['fb_access_token'] . '<br>';
//$fb->setDefaultAccessToken($accessToken);
$picUrl = "http://images2.fanpop.com/images/photos/5100000/Cute-pups-dogs-5114450-500-309.jpg";

$linkData = [
    'url' => $picUrl,
    'message' => 'Test-test',
];
try{
    $response = $fb->post('/me/photos',$linkData,$accessToken);
} catch (Facebook\Exceptions\FacebookResponseException $e){
    echo 'Response Exception: ' . $e->getMessage();
    exit;
} catch (Facebook\Exceptions\FacebookSDKException $e){
    echo 'SDK Exception: ' . $e->getMessage();
    exit;
} catch (Exception $e){
    echo 'Unknown Exception: ' . $e->getMessage();
    exit;
}
$graphNode = $response->getGraphNode();
echo 'Posted with id: ' . $graphNode['id'];
?>

它成功地将图片发布到我的时间线,但问题是我希望它可以单独发布,但可以在一张专辑中找到。如何将其作为单独的图片。有人可以帮帮我吗?提前致谢。 :d

0 个答案:

没有答案