2012-08-02 76 views
0

我想上傳一張圖片到Facebook上使用PHP的按鈕點擊。它似乎不工作。將圖像上傳到Facebook的按鈕點擊

HTML按鈕的代碼

<a class="btn" href="test.php?upload=true">Click here to upload this image on your Facebook wall</a> 

PHP代碼

<?php 
     $act=isset($_GET['upload'])?$_GET['upload']:""; 

     if($upload=='true'){ 

     set_time_limit(50); 
     require 'facebook.php'; 

     // Create our Application instance (replace this with your appId and secret). 
     $facebook = new Facebook(array(
        'appId' => 'FB_APP_ID', 
        'secret' => 'FB_SECRET_KEY', 
       )); 

     // Get User ID 
     $user = $facebook->getUser(); 
     if ($user) { 

     } else { 
      $loginUrl = $facebook->getLoginUrl(); 
      header('Location:' . $loginUrl . '&scope=user_photos,publish_stream'); 
     } 
     ?> 

     <?php 
     // Login or logout url will be needed depending on current user state. 
       if ($user) { 
        if (isset($_GET['upload'])) { 
       ?> 

       <?php 
         $facebook->setFileUploadSupport(true); 
         $args = array('message' => 'test'); 
         copy('http://mysite.com/test.png', 'tmp/file.jpeg'); 
         $args['image'] = '@' . realpath('tmp/file.jpeg'); 
         $data = $facebook->api('/me/photos', 'post', $args); 
         unlink('tmp/file.jpeg'); 
         //assigning users to tag and cordinates 
         $argstag = array('to' => $user); 
         $argstag['x'] = 40; 
         $argstag['y'] = 40; 
         $datatag = $facebook->api('/' . $data['id'] . '/tags', 'post', $argstag); 
         echo 'Success! Check your facebook wall now'; 
        } else { 
       ?> 
         <a href="test.php?upload=true">Click here to upload this image on your facebook wall</a><br/><br/> 

     <?php 
        } 
       } 


     } 
?> 

注:facebook.php是來與Facebook PHP SDK中的一個

回答

0

放在一個嘗試& catch塊看看會發生什麼:

// ... 
try { 
    $facebook->setFileUploadSupport(true); 
    $response = $facebook->api(
     '/me/photos/', 
     'post', 
     array(
     'message' => 'This is my image caption', 
     'source' => '@/absolute/path/to/image' // @-sign must be the first character 
    ) 
    ); 
    } 
    catch (FacebookApiException $e) { 
    echo "Error: ".$e->getMessage(); 
    } 
    // ... 

確保在「tmp /」(是否指「/ tmp /」?)調用拷貝之後存在有效的圖像,並確保您具有photo_upload權限。

https://developers.facebook.com/docs/reference/php/facebook-setFileUploadSupport/