Lajos Arpad
Lajos Arpad

Reputation: 76817

Posting emojis to Facebook

I am using Facebook API with their official PHP library. I have a code which posts some content to Facebook, however, emojis are not successfully posted. This is the code:

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);
}

App::unclean:

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));
}

And finally, the code which posts to 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;
}

Everything works fine, except the fact that emojis are not shown. What should be modified if the question and/or answer contains an emoji to show everything, including emojis well on Facebook?

Upvotes: 1

Views: 848

Answers (1)

Sergey Krivov
Sergey Krivov

Reputation: 372

These steps solved this problem for me:

  1. Create a mysql backup :)
  2. Switched database character to utf8mb4
  3. Switched table default charset to utf8mb4
  4. Switched text column character to utf8mb4
  5. Check backend to always work in utf8

Example:

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;

And pay attention, when converting from utf8 to utf8mb4, the maximum length of a column or index key is unchanged in terms of bytes. Therefore, it is smaller in terms of characters, because the maximum length of a character is now four bytes instead of three.

Upvotes: 1

Related Questions