将表情符号发布到Facebook

时间:2015-09-16 17:56:22

标签: php facebook emoji facebook-graph-api-v2.0

我正在使用Facebook API和他们的官方PHP库。我有一个代码将一些内容发布到Facebook,但是,emojis没有成功发布。这是代码:

public function postToFacebook() {
    $stripped_question = App::unclean($this->getQuestion());

    $question_id = $this->getId();

    $answer = $this->getAnswer();
    if ((!!$answer) && (strlen($answer) > 10)) {
        $answer = substr($answer, 0, strlen($answer) / 2)."...";
    }
    $uri = QM::$mainSiteName."question/question/qid/$question_id/";
    $desc = "";
    $caption = App::unclean($answer);
    $title = $stripped_question;
    $pic = QM::$mainSiteName . "images/facebook-attachment.png";
    $action_name = "Ask " . $_SESSION["username"] . " anything";
    $action_link = QM::$mainSiteName . $_SESSION["username"];
    $msg = "";

    App::CurrentUser()->postToFacebookWall($title, $uri, $desc, $msg, $pic, $caption, $action_name, $action_link);
}

应用::不洁:

public static function unclean($var) {
    return QM::filterText(stripslashes(trim($var)));
}

QM :: filterText:

public static function filterText($text) {
    return str_replace(array('&lt;br /&gt;', '&lt;br/&gt;', '&lt;br&gt;'), '<br>', htmlspecialchars($text));
}

最后,发布到Facebook的代码:

public function postToFacebookWall($title, $uri, $desc, $msg = null, $pic = null, $caption = null, $action_name = null, $action_link = null, $uid = 'me') {
    try {
        $data = array(
            'name' => $title,
            'link' => $uri,
            'description' => $desc            
        );

        if ($pic) {
            $data['picture'] = $pic;
        }

        if ($caption) {
            $data['caption'] = $caption;
        }

        if ($action_name) {
            $data['actions'] = json_encode(array('name' => $action_name, 'link' => $action_link));
        }

        Facebook\FacebookAccessor\FacebookAction::facebookActionFactory("post", array("url" => QM::$mainSiteName, "params" => $data));
    } catch (Exception $e) {
        return false;
    }

    return true;
}

一切都很好,除了没有显示表情符号的事实。如果问题和/或答案中包含表情符号以显示所有内容,包括表情符号上的表情符号,应该修改哪些内容?

1 个答案:

答案 0 :(得分:1)

这些步骤为我解决了这个问题:

  1. 创建一个mysql备份:)
  2. 将数据库字符切换为utf8mb4
  3. 将表格默认字符集转换为utf8mb4
  4. 将文本列字符切换为utf8mb4
  5. 检查后端以始终使用utf8
  6. 示例:

    ALTER DATABASE {database_name} CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
    # For each table:
    ALTER TABLE {table_name} CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    # For each column:
    ALTER TABLE {table_name} CHANGE {column_name} VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    

    注意,当从utf8转换为utf8mb4时,列或索引键的最大长度以字节为单位不变。因此,它在字符方面较小,因为字符的最大长度现在是四个字节而不是三个。