facebook-php-sdk如何将照片上传到墙上或相册?

时间:2013-02-09 15:28:25

标签: facebook facebook-graph-api facebook-php-sdk

嗨,大家好我是php programin和facebook应用程序的新手,我发现这个代码并且它对于TEXT帖子效果很好,但是我的要求是我必须上传一张图片,我试过addin ['source '=> '$ image_source']在arrey上但不起作用...... 如何上传照片?

<?php
include_once("config.php");


if($_POST)
{
    //Post variables we received from user
    $userPageId     = $_POST["userpages"];
    $userMessage    = $_POST["message"];
    $image_source = "http://www.example.com/image.jpg"
    if(strlen($userMessage)<1) 
    {
        //message is empty
        $userMessage = 'No message was entered!';
    }

        //HTTP POST request to PAGE_ID/feed with the publish_stream
        $post_url = '/'.$userPageId.'/feed';

        //posts message on page statues 
        $msg_body = array(
        'message' => $userMessage,
        'source' => '$image_source'
        );

    if ($fbuser) {
      try {
            $postResult = $facebook->api($post_url, 'post', $msg_body );
        } catch (FacebookApiException $e) {
        echo $e->getMessage();
      }
    }else{
     $loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions));
     header('Location: ' . $loginUrl);
    }

    //Show sucess message
    if($postResult)
     {
         echo '<html><head><title>Message Posted</title><link href="style.css" rel="stylesheet" type="text/css" /></head><body>';
         echo '<div id="fbpageform" class="pageform" align="center">';
         echo '<h1>Your message is posted on your facebook wall.</h1>';
         echo '<a class="button" href="'.$homeurl.'">Back to Main Page</a> <a target="_blank" class="button" href="http://www.facebook.com/'.$userPageId.'">Visit Your Page</a>';
         echo '</div>';
         echo '</body></html>';
     }
}

?>

1 个答案:

答案 0 :(得分:3)

您无法使用feed端点发布照片。您应该/me/photos使用publish_stream权限。要发布到相册,您需要/ALBUM_ID/photos端点

   $msg_body = array(
    'message' => $userMessage,
    'source' => '@'.'$image_source'
   );

   $postResult = $facebook->api('/me/photos/','post', $msg_body);

要使用与您的网站无关的网址,您需要使用url

  $msg_body = array(
    'message' => $userMessage,
    'url' => 'http://somepage.com/img.png'
   );