2016-07-18 22 views
-3
try { 
    $requestCover = $fb->get('me?fields=cover'); //getting user Cover 
    $requestProfileCover=$fb->get('/me'); 
// $cover = $requestCover->getGraphUser();   
// $profile = $requestProfileCover->getGraphUser();  
    } 
    catch(Facebook\Exceptions\FacebookResponseException $e) { 
    echo ' error: ' . $e->getMessage();   
    exit; 
    } 
    catch(Facebook\Exceptions\FacebookSDKException $e) {    
    echo 'Facebook SDK error: ' . $e->getMessage();   
    exit; 
    } 
    print_r($requestCover);  // showing cover details on the screen 
    echo "<img src='".$requestCover['source']."'/>"; 

錯誤:我試圖通過圖API獲取FACEBOOK封面,但我無法使用。

Fatal error: Uncaught Error: Cannot use object of type Facebook\FacebookResponse as array in C:\xampp\htdocs\fb_app\twibook\index.php:165 Stack trace: #0 {main} thrown in C:\xampp\htdocs\fb_app\twibook\index.php on line 165

+0

究竟是如何不工作? –

+0

錯誤:致命錯誤:未捕獲錯誤:無法使用Facebook \ FacebookResponse類型的對象作爲C:\ xampp \ htdocs \ fb_app \ twibook \ index.php中的數組:165堆棧跟蹤:#0 {main}拋出C:\ xampp \ htdocs \ fb_app \ twibook \ index.php 165行 –

+0

我想165行是你的代碼片段的最後一行?嘗試訪問它通過'$ requestCover->源' – Philipp

回答

0

CBroe指向你到正確的功能使用,讓您的數據。我正在使用這個答案,試圖解釋你應該嘗試的內容不適合評論。 (有沒有這個API的工作還沒有,所以我不知道實際的解決您的問題,但處理你的問題的描述應該是準確的)


通過調用$requestCover = $fb->get('me?fields=cover');要設置$requestCover到對象類FacebookResponse

因爲它是一個對象,所以無法像處理數組元素時那樣訪問它的屬性。您必須使用->操作員才能訪問它們(如果它們是公開的),或者您必須使用特定的方法。

在這種情況下,對於FacebookResponse類,有些方法需要使用。 The one CBroe pointed you to,將返回一個數組,其中包含響應正文的不同數據,因此首先將調用的返回值寫入變量,然後您(應該)使用您的值包含一個數組。

try { 
    $requestCover = $fb->get('me?fields=cover'); //getting user Cover 
    $requestProfileCover=$fb->get('/me'); 
} 
catch(Facebook\Exceptions\FacebookResponseException $e) { 
    echo ' error: ' . $e->getMessage();   
    exit; 
} 
catch(Facebook\Exceptions\FacebookSDKException $e) {    
    echo 'Facebook SDK error: ' . $e->getMessage();   
    exit; 
} 
$responseBody = $requestCover->getDecodedBody(); 
echo "<img src='" . $responseBody ['source'] . "'/>"; 
print_r($requestCover); // should print an array 
相關問題