简体   繁体   中英

Facebook Graph API - How to post to wall, with new lines?

I'm playing with Facebook Graph API and I am facing one problem - I can't find any way how to post to wall with some HTML code or new lines. How it could be done? Here's my code

<?php
include_once 'lib/facebook.php';
define("FACEBOOK_APP_ID", '10126');
define("FACEBOOK_API_KEY", '064ca1988b');
define("FACEBOOK_SECRET_KEY", '9afdf92114');
define("FACEBOOK_CANVAS_URL", 'http://apps.facebook.com/my_canv_app/');
if (isset($_GET['code'])){
    header("Location: " . FACEBOOK_CANVAS_URL);
    exit;
}

$facebook = new Facebook(array('appId' => FACEBOOK_APP_ID, 'secret' => FACEBOOK_SECRET_KEY));
$user = $facebook->getUser();
$loginUrl   = $facebook->getLoginUrl(
        array(
                'scope'  => 'email,publish_stream,user_birthday,user_location,user_about_me,user_hometown'
        )
);

if (!$user) {
    echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
    exit;
}


try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
    $statusUpdate = $facebook->api('/me/feed', 'post', array('message'=> 'Trying to make new line here \n <br /> Neither works', 'cb' => ''));
} catch (FacebookApiException $e) {
    echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
}
?>

how could I do it?

This works.

try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$statusUpdate = $facebook->api('/me/feed', 'post', array('message'=> 'Line 1
                                                             Line 2
                                                             Line 3
                                                             Line 4',
                                                         'cb' => ''));

You can't include any markup or new lines in wall posts. You used to be able to provide some FBML markup to get some basic formatting, but that got abused. If you could include html and new lines, Facebook profiles would start looking like MySpace profiles.

Facebook pretty much sanitizes all wall/profile content, for security reasons.

Despite all the stuff that's been said in answers to previous questions regarding newlines in wall posts – I never had any problem with just using the escape character \\n when making posts using PHP.

 array('message'=> 'Trying to make new line here \\n <br /> Neither works', … 

Of course, if you are filling you message parameter in PHP using single quotes, \\n means only the two characters \\ and n – that these escape sequences are only interpreted when using double quotes, is absolutely basic PHP syntax knowledge …

D'oh!

这是你的解决方案%0A将它插入你的文本中它将作为换行符:)

put this

< center >< /center >

to where you want to break the line

ex:

line1< center >< / center >line2< center >< / center >line3

NOTE: no spaces before and after < >

For those who would like an update on this.
I am using the PHP SDK and \\n works as long as it is enclosed in double quotes (""). Single quotes won't work.

So, I fetch my data from a tiny mce box and I do this:

$message = preg_replace("/<br \/>/","\n",$tiny_mce_message); // the \n must be in ""!
$message = strip_tags($message);
$this->message = utf8_encode($message);

换行符(ASCII码10)可以解决这个问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM