2016-07-18 23 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 
相关问题