2010-10-29 58 views
13

这是PHP的最后一次尝试,如果失败,我会尝试使用JS。所以我的目标是张贴FB页面上的“网页名称”通过PHP:这是我想要得到Facebook Graph API PHP SDK在页面上张贴

View 1

但我得到如下所示照片。此外,它仅可用于此配置文件(不适用于喜欢/ etc的朋友/ ppl)。

View 2

这是我当前的代码

function post_facebook($data=null, $redir = null){ 
     $result = ""; 
     require_once (ROOT. "/apps/configuration/models/ConfigurationItem.php"); 
     require_once (ROOT . "/components/facebook/facebook.php"); 

     $this->ConfigurationItem = new ConfigurationItem($this->getContext()); 

     $row=$this->ConfigurationItem->findByCatKeyItemKey('system','facebook_login'); 
     $apiid=$row['value']; <= Correct apiid 

     $row=$this->ConfigurationItem->findByCatKeyItemKey('system','facebook_pass'); 
     $secret=$row['value']; <= Correct secret key 

     $facebook = new Facebook(array(
      'appId' => $apiid, 
      'secret' => $secret, 
      'cookie' => true, 
     )); 

     $session = $facebook->getSession(); 
     $me = null; 
     if ($session) { 
      try { 
       $uid = $facebook->getUser(); 
       $me = $facebook->api('/me'); 
      } catch (FacebookApiException $e) { 
       error_log($e); 
      } 
      $message=$data['facebook_text']; 
      $attachment = array(
       'message' => $data['facebook_text'], 
       'name' => $data['name'], 
       'link' => $this->getLinkToLatestNews(), 
       'description' => '', 
      ); 

      try { 
       $facebook->api('/PAGE ID/feed/', 'post', $attachment); 
       $result = "Facebook: Sent"; 
      } catch (FacebookApiException $e) { 
       $result = "Facebook: Failed"; 
       error_log($e); 
      } 
     } else { 
      $login_url = $facebook->getLoginUrl(); 
      header("Location: ".$login_url); 
      exit; 
     } 

     echo $result; 
     exit; 
     //return $result; 

    } 

我做错了什么?我在API文档/顶级谷歌搜索结果中找不到任何东西,只能用于JS。感谢帮助!

回答

20

您需要确保您要求用户的'manage_pages'权限。一旦你得到了,你可以做$facebook->api('/me/accounts'),你会收到一个令牌回(连同页面信息),你可以用它在页面上发布为页面。

+0

的关键,解决这个问题是越来越访问令牌 - 我错过了前面的部分。我欠你! – Misiur 2010-10-29 20:28:12

+0

嗨,你是否能够发布一个链接与图像(如页作为海报)? – user3329793 2014-02-23 01:28:08

0

我在这一天的大部分时间里挣扎着,然后发现不使用setAccessToken(page_access_token)是阻止它为我工作的唯一东西。我在18个月前的一篇文章中发现了这个帖子。我在这里把我的解决方案,任何人谁在未来有这样一个问题:

protected $scope = "email,publish_stream,manage_pages"; 

    $url = "{$api_url}/{$fbusername}/accounts?access_token=".$access_token; 
    $response = json_decode(file_get_contents($url)); 
    foreach($response->data as $data) { 
     try 
     { 
      $res = $this->SDK->setAccessToken($data->access_token); 
      $res = $this->SDK->api(
       "{$data->id}/feed", 
       "POST", 
       array('link' => 'www.example.com', 
         'message' => 'This is a test message from php',) 
      ); 
      log::debug(__FUNCTION__, print_r($res,true)); 
     } 
     catch (Exception $e) 
     { 
      log::debug(__FUNCTION__, $e->getType().": ".$e->getMessage()); 
     } 

    }