4

我剛剛嘗試使用Facebook PHP SDK開發Facebook應用程序。在Facebook的開發者網站給出的示例代碼如下..'致命錯誤:'CURLFile'未找到'類錯誤Facebook PHP應用程序'上傳照片到用戶的時間軸'

 <?php 
    // Remember to copy files from the SDK's src/ directory to a 
    // directory in your application on the server, such as php-sdk/ 
    require_once('php-sdk/facebook.php'); 

    $config = array(
    'appId' => 'YOUR_APP_ID', 
    'secret' => 'YOUR_APP_SECRET', 
    'fileUpload' => true, 
    'allowSignedRequest' => false // optional but should be set to false for non-canvas apps 
); 

    $facebook = new Facebook($config); 
    $user_id = $facebook->getUser(); 

    $photo = './mypic.png'; // Path to the photo on the local filesystem 
    $message = 'Photo upload via the PHP SDK!'; 
?> 
<html> 
    <head></head> 
    <body> 

    <?php 
    if($user_id) { 

     // We have a user ID, so probably a logged in user. 
     // If not, we'll get an exception, which we handle below. 
     try { 

     // Upload to a user's profile. The photo will be in the 
     // first album in the profile. You can also upload to 
     // a specific album by using /ALBUM_ID as the path 
     $ret_obj = $facebook->api('/me/photos', 'POST', array(
             'source' => new CURLFile($photo, 'image/png'), 
             'message' => $message, 
             ) 
            ); 
     echo '<pre>Photo ID: ' . $ret_obj['id'] . '</pre>'; 
     echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>'; 
     } catch(FacebookApiException $e) { 
     // If the user is logged out, you can have a 
     // user ID even though the access token is invalid. 
     // In this case, we'll get an exception, so we'll 
     // just ask the user to login again here. 
     $login_url = $facebook->getLoginUrl(array(
         'scope' => 'photo_upload' 
         )); 
     echo 'Please <a href="' . $login_url . '">login.</a>'; 
     error_log($e->getType()); 
     error_log($e->getMessage()); 
     } 
    } else { 

     // No user, print a link for the user to login 
     // To upload a photo to a user's wall, we need photo_upload permission 
     // We'll use the current URL as the redirect_uri, so we don't 
     // need to specify it here. 
     $login_url = $facebook->getLoginUrl(array('scope' => 'photo_upload')); 
     echo 'Please <a href="' . $login_url . '">login.</a>'; 

    } 

    ?> 

    </body> 
</html> 

但是當我運行它,它顯示了一個錯誤'Fatal error: Class 'CURLFile' not found'。當搜索解決方案時,我發現新的PHP Facebook SDK沒有'CURLFile'類。任何人都可以使用新的SDK代碼來幫助我將照片發佈到用戶時間軸上嗎?

在此先感謝..

+0

什麼是你的操作系統? – HMagdy

+0

@HassanMagdy Windows 7 64bit。 –

+0

也許你的php版本還不支持php5.5。 – Jhn

回答

1

你嘗試在XAMPP \ apache的\斌\ php.ini中能夠在PHP捲曲通過取消對該行

;extension=php_curl.dll 

,然後重新啓動Apache服務。

您可以檢查通過phpinfo()函數

還要檢查根據你的php版本上The CURLFile class doc

+0

是的..我啓用cURL –

9

也許你需要添加一個反斜槓:

new \CurlFile($photo) 

如果它不也許工作檢查如果CurlFile存在。如果它不存在,那麼你需要直接通過tmp filename.fileExtension

'source' => class_exists('CurlFile', false) ? new CURLFile($photo, 'image/png') : "@{$photo}" 

在您在其他條件$photo是在filename.fileExtension形式。

+2

謝謝你第二個爲我工作 –

+1

在'CurlFile'前面添加\爲我工作。謝謝! – Keith

3

CURLFile僅適用於PHP 5> = 5.5.0

由於Facebook舉例說:

// If you're not using PHP 5.5 or later, change the file reference to: 
// 'source' => '@/path/to/file.name' 
相關問題