PHP脚本问题发布在我的Facebook粉丝页面的墙上,并且$ access_token的永久价值

问题描述:


可能重复:

的PHP SCRIPT问题

Possible Duplicate:
PHP SCRIPT issue with passing the uploaded image to facebook->api function

我有这个代码,它的工作非常好,只有一个问题,我无法通过我的上传的图像到Facebook-> api(无论它的有效和否)和以下总是echo
echo'只有jpg,png和gif图像类型被支持!
我甚至删除对图像类型的检查,并尝试从
$ img = realpath($ _ FILES [pic] [tmp_name])获取图像;
但$ img没有任何内容,并在Facebook上默认上传一个空的图像作为我的页面
请仔细检查我的以下代码,让我知道我的代码应该是什么错误,所以我将能够上传图片

I have this code and its working awsomely fine just and just only one problem i am not able to pass my uploaded image to facebook->api (whether its valid or not) and the following always echo echo 'Only jpg, png and gif image types are supported!'; i even remove the check on image type and try to get the image from $img = realpath($_FILES["pic"]["tmp_name"]); but $img gets nothing in it and uploads a by default empty image on facebook as my page Kindly check my following code and let me know what should is wrong with my code so i'll become able to upload images

 <?


require 'src/facebook.php';

$app_id = "364900470214655";
$app_secret = "xxxxxxxx";

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

$user = $facebook->getUser();
//echo $user;

if(($facebook->getUser())==0)
{
 header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access,manage_pages'))}");
 exit;
}
else {
$accounts_list = $facebook->api('/me/accounts');
echo "i am connected";
}
  $valid_files = array('image/jpeg', 'image/png', 'image/gif');


//to get the page access token to post as a page
foreach($accounts_list['data'] as $account){
      if($account['id'] == 194458563914948){      // my page id =123456789
        $access_token = $account['access_token'];
        echo "<p>Page Access Token: $access_token</p>";
        }
    }

//posting to the page wall

if (isset($_FILES) && !empty($_FILES))
{  
if( !in_array($_FILES['pic']['type'], $valid_files ) )
{
  echo 'Only jpg, png and gif image types are supported!';
 }
 else{
  #Upload photo here
  $img = realpath($_FILES["pic"]["tmp_name"]);
$attachment = array('message' => 'this is my message',
                'access_token'  => $access_token,
                'name' => 'This is my demo Facebook application!',
                'caption' => "Caption of the Post",
                'link' => 'example.org',
                'description' => 'this is a description',
                'picture' => '@' . $img,
                'actions' => array(array('name' => 'Get Search',
                                  'link' => 'http://www.google.com'))
                );
$status = $facebook->api('/194458563914948/feed', 'POST', $attachment);   // my page id =123456789
var_dump($status);
}
}
?>
<body>
 <!-- Form for uploading the photo -->
 <div class="main">
  <p>Select a photo to upload on Facebook Fan Page</p>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
  <p>Select the image: <input type="file" name="pic" /></p>
  <p><input class="post_but" type="submit" value="Upload to my album" /></p>
  </form>
 </div>
</body>


创建新相册并添加照片

这是您将照片上传到ALBUM_ID / photos Graph API端点的场景。此示例的用户界面允许用户在提交新照片之前选择照片并添加标题。

This is the scenario where you upload a photo to the ALBUM_ID/photos Graph API endpoint. The user interface for this example allows the user to select a photo and add a caption before submitting the new photo.

使用PHP:

<?php

       $app_id = "YOUR_APP_ID";
       $app_secret = "YOUR_APP_SECRET";
       $post_login_url = "YOUR_POST-LOGIN_URL";
       $album_name = 'YOUR_ALBUM_NAME';
       $album_description = 'YOUR_ALBUM_DESCRIPTION';

       $code = $_REQUEST["code"];

       //Obtain the access_token with publish_stream permission 
       if(empty($code))
         {
           $dialog_url= "http://www.facebook.com/dialog/oauth?"
           . "client_id=" . $app_id 
           . "&redirect_uri=" . urlencode($post_login_url)
           . "&scope=publish_stream";
           echo("<script>top.location.href='" . $dialog_url . 
           "'</script>");
       } 
       else {
         $token_url= "https://graph.facebook.com/oauth/"
         . "access_token?"
         . "client_id=" .  $app_id 
         . "&redirect_uri=" . urlencode( $post_login_url)
         . "&client_secret=" . $app_secret
         . "&code=" . $code;
         $response = file_get_contents($token_url);
         $params = null;
         parse_str($response, $params);
         $access_token = $params['access_token'];

         // Create a new album
         $graph_url = "https://graph.facebook.com/me/albums?"
         . "access_token=". $access_token;

         $postdata = http_build_query(
         array(
          'name' => $album_name,
          'message' => $album_description
            )
          );
         $opts = array('http' =>
         array(
          'method'=> 'POST',
          'header'=>
            'Content-type: application/x-www-form-urlencoded',
          'content' => $postdata
          )
         );
         $context  = stream_context_create($opts);
         $result = json_decode(file_get_contents($graph_url, false, 
           $context));

         // Get the new album ID
         $album_id = $result->id;

         //Show photo upload form and post to the Graph URL
         $graph_url = "https://graph.facebook.com/". $album_id
           . "/photos?access_token=" . $access_token;
         echo '<html><body>';
         echo '<form enctype="multipart/form-data" action="'
         .$graph_url. ' "method="POST">';
         echo 'Adding photo to album: ' . $album_name .'<br/><br/>';
         echo 'Please choose a photo: ';
         echo '<input name="source" type="file"><br/><br/>';
         echo 'Say something about this photo: ';
         echo '<input name="message" type="text"
            value=""><br/><br/>';
         echo '<input type="submit" value="Upload" /><br/>';
         echo '</form>';
         echo '</body></html>';
      }
 ?>