如何编码“'”字符

I'm writing a facebook application that post messages into the feed. When i'm trying to post the message "I'm Roy" to my feed using the graph API with HTTP POST, I keep getting the following result "I\'m Roy". Why is that? I'm using PHP to call the graph API, and wrap my message text with urlencode().

What do I miss?

This is my PHP code:

    function POST_feeds($parameters)
    {           
        //extract data from the post
        extract($_POST);

        //set POST variables
        $url = 'https://graph.facebook.com/' . $parameters["facebookUserId"] . "/feed";
        $fields = array(
                    'access_token' => $this->accessToken,
                    'message' => $parameters["message"]
                );

        //url-ify the data for the POST
        foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
        rtrim($fields_string, '&');

        //open connection
        $ch = curl_init();

        //set the url, number of POST vars, POST data
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_POST, count($fields));
        curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

        //execute post
        $facebookResponse = curl_exec($ch);

        //close connection
        curl_close($ch);

        $response = new Response();
        $response->addItem($facebookResponse);
        return response;
    }

UPDATE

the PHP function above is getting its parameters from the POST parameters of the following XMLHttpRequest request:

    var xhr = new XMLHttpRequest();
    xhr.open("POST", webServiceUrl + "feeds", true);
    var params = "facebookUserId=" + facebookUserId + "&message=" + message;
    print(params);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
    xhr.setRequestHeader("Content-length", params.length);

    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                print(xhr.responseText);
                var responseObject = JSON.parse(xhr.responseText);
                successCallback(responseObject);
            } else {
                failureCallback();              
            }
        }
    };
    xhr.send(params);

It seems that the problem starts from the javascript code (when i try to hardcode some message with the " ' " character in my PHP code its all works fine).

Thanks!

You shouldn't be urlencoding your data, the php facebook api takes care of that for you.

--- original answer

You have to use double quotes " on the outside in order to parse escaped characters. Also if you are using double-outer-quotes you don't need to escape '

"I\'m Roy"

is not the same as

'I\'m Roy'

Give this one a try: http://php.net/manual/en/function.urlencode.php ?

You might want to use it in conjunction with htmlentities() as noted in the docs if you have html tags in the string as well...