【问题标题】:Facebook API - cover photo image not showingFacebook API - 封面照片图像未显示
【发布时间】:2014-05-06 09:44:27
【问题描述】:

我有一个简单的 PHP 来检索封面照片。

function get_fb_image($id) {
  global $facebook;
  $img = $facebook->api('/'.$id.'?fields=cover');
  return $img["cover"]["source"];
}

它返回的字符串是(例如)https://scontent-a.xx.fbcdn.net/hphotos-ash3/t1.0-9/s720x720/10292482_276536685841029_1024651999315765938_n.jpg,这是正确的图像。当我尝试将它放在一个简单的标签中但它不起作用时,没有图像。我试过换成http而不是https,我什至尝试使用访问令牌无济于事,有什么想法吗?谢谢!

你可以在这里看到渲染的页面http://comedyinenglish.de/performers/caroline-clifford/ - 图片是在'xxx'附近渲染的

更新 我突然想到图像只是没有在 Chrome 中显示,有什么想法吗?安全问题?试过http://和https

【问题讨论】:

  • 已解决天哪,这是插件“断开连接”,很抱歉浪费大家的时间 - 希望这对某人有所帮助!

标签: php facebook facebook-graph-api


【解决方案1】:

我没有 Facebook 的 PHP SDK 来尝试这个,但是你可以使用 graphs api 来获取封面图片,如下所示:

test.php

<?php

function getCoverImage($user_or_page_id) {
    $jsonResult = json_decode(
        file_get_contents(
            'https://graph.facebook.com/'. $user_or_page_id .'?fields=cover'));
    return $jsonResult->cover->source;
}

?>

<img src="<?php echo getCoverImage('carolinecliffordcomedy') ?>" >

我已经确认这对我有用:

【讨论】:

  • img 标签呈现为&lt;img src="http://scontent-a.xx.fbcdn.net/hphotos-ash3/t1.0-9/s720x720/10292482_276536685841029_1024651999315765938_n.jpg"&gt;
  • 如果您尝试访问该 URL http://scontent-a.xx.fbcdn.net/hphotos-ash3/t1.0-9/s720x720/10292482_276536‌​685841029_1024651999315765938_n.jpg,您将收到错误:Invalid Request - 这就是图像无法呈现的原因。
  • 是的,URL 不会长时间保持有效,您需要使用 API,新图像现在是 scontent-a.xx.fbcdn.net/hphotos-ash3/t1.0-9/s720x720/…
  • 你可以在这里看到渲染的页面comedyinenglish.de/performers/caroline-clifford - 图片是在'xxx'附近渲染的
  • 是的,根据我的 cmets,问题是由于我安装了 Chrome 扩展程序,感谢您的帮助!
【解决方案2】:

您可以使用 curl 函数来获取结果。我在我的应用程序中使用了此代码并得到了结果。请尝试此代码:

function curl_get_file_contents($URL) {
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $URL);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0); 
     curl_setopt($ch, CURLOPT_TIMEOUT, 400);
     $contents = curl_exec($ch);
     $err  = curl_getinfo($ch,CURLINFO_HTTP_CODE);
     curl_close($ch);
     $contents=json_decode($contents,true);
     if ($contents)
         return $contents;
     else
         return FALSE;
}
    $page_user_name = 'carolinecliffordcomedy';  
    $url = "https://graph.facebook.com/$page_user_name?fields=cover";  
    $details =curl_get_file_contents($url);
    <img src="<?php echo $details['cover']['source']; ?>" height="250" width="250" />  

【讨论】:

  • 嗨,我在我的页面上试过了,但图像仍然没有在 Chrome 中显示,请参阅 xxx 旁边的comedyinenglish.de/performers/caroline-clifford
  • 当我点击链接时,即使我是 chrome,我也能看到图片。可以给我截图吗?
  • 根据我对原始问题的 cmets,问题是由于我安装了 Chrome 扩展程序。谢谢你的帮助,卡罗琳
猜你喜欢
  • 1970-01-01
  • 2014-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多