发布到Facebook作为品牌页面 - PHP

时间:2013-06-25 17:56:40

标签: php facebook

我在Facebook上有粉丝页面(或品牌页面)。我已经创建了一个PHP应用程序,我想在这个品牌推广页面上发布消息,但不是在我自己的用户名下,而是作为这个品牌名称。所以基本上我想通过PHP来写这个品牌的名字。

我有以下代码:

    //This is the Page ID of the branding page
    $post_url = '/'.$userPageId.'/feed';

    // posts message on page feed
    $msg_body['message'] = $userMessage;

    if (!empty($userLink)) {
        $msg_body['link'] = $userLink;
    }
    if (!empty($userDescr)) {
        $msg_body['description'] = $userDescr;
    }
    if (!empty($userPic)) {
        $msg_body['picture'] = $userPic;
    }

  try {
        $postResult = $facebook->api($post_url, 'post', $msg_body );
    } catch (FacebookApiException $e) {
    echo $e->getMessage();
  }

代码会在品牌宣传页上留言,但是来自我自己的帐户。知道我怎么写,好像消息来自品牌本身?

提前致谢!

1 个答案:

答案 0 :(得分:1)

我最终设法解决了这个问题。问题是我没有传递页面访问令牌。以下代码运行良好,并修复了发布扩展ASCII符号时的字符编码问题:

$userPageId     = $_POST["userpages"];
$userMessage    = $_POST["message"];
$userLink   = $_POST["link"];
$userDescr  = $_POST["description"];
$userPic    = $_POST["picture"];
$access_token   = $_POST["page_access_token"];
$userCaption    = $_POST["caption"];

//HTTP POST request to PAGE_ID/feed with the publish_stream
$post_url = '/'.$userPageId.'/feed';

// posts message on page feed
$userMessage = utf8_encode (stripslashes($userMessage)); 
$userMessage = iconv("UTF-8", "UTF-8//TRANSLIT", $userMessage);

$userCaption = utf8_encode (stripslashes($userCaption)); 
$userCaption = iconv("UTF-8", "UTF-8//TRANSLIT", $userCaption);

$userDescr = utf8_encode (stripslashes($userDescr)); 
$userDescr = iconv("UTF-8", "UTF-8//TRANSLIT", $userDescr);     

$msg_body['message'] = $userMessage;
$msg_body['access_token'] = $access_token;

if (!empty($userLink)) {
    $msg_body['link'] = $userLink;
}
if (!empty($userDescr)) {
    $msg_body['description'] = $userDescr;
}
if (!empty($userPic)) {
    $msg_body['picture'] = $userPic;
}
if (!empty($userCaption)) {
    $msg_body['caption'] = $userCaption;
}

if ($fbuser) {
  try {
    $postResult = $facebook->api($post_url, 'post', $msg_body );
  } 
      catch (FacebookApiException $e) {
    echo $e->getMessage();
  }
}