开发者

How to use the graph API to upload photos to albums

My code(not working):

<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
  'appId'  => '...',
  'secret' => '...'
));

Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false; 
$user = $facebook->getUser();
if ($user) {
  try {
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

if ($user) {
  $album_id = '246592692052269';    
  $FILE_PATH = 'Language.jpg';
  $access_token = $_SESSION['fb_appId_access_token'];
  $logoutUrl = $facebook->getLogoutUrl();
   $args = array('image'=> '@' . __DIR__.$FILE_PATH, 
           'name' => 'this photo was taken in acre',
          'message'=> 'Photo Caption'); 
    $ch = curl_init();
    $url = 'http://graph.facebook.com/'.$album_id.'/photos?access_token='.$access_token;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
    $data = curl_exec($ch);
    //returns the photo id
    if(empty($data)) {
    echo 'Responde: '. $data . '<pre>';
    print_r(curl_getinfo($ch));
    echo '</pre>';
    } else {
    echo 'Upload failed! erro='. curl_error($ch);
    }

} else {
  $loginUrl = $facebook->getLoginUrl();
}

?>

Output:

Array
(
    [url] => http://graph.facebook.com/246592692052269/photos?access_token=...
    [content_type] => 
    [http_code] => 0
    [header_size] => 0
    [request_size] => 0
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.203
    [namelookup_time] => 0
    [开发者_开发百科connect_time] => 0.203
    [pretransfer_time] => 0.203
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => -1
    [starttransfer_time] => 0
    [redirect_time] => 0
)

Can someone point out my error? Thanks in advance.


Make sure file uploads are enabled on the server side, and initialize the SDK with fileUpload set to true as described on the PHP SDK documentation page, or call setFileUploadSupport. You don't need to manually call the graph API with the access token and curl commands as you are doing above, the PHP SDK will take care of that for you. See the api method documentation page for a complete example.


Check out facebook's howto: http://developers.facebook.com/blog/post/498/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜