douyouming9180 2012-12-17 03:55
浏览 38
已采纳

如何编码“'”字符

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!

  • 写回答

2条回答 默认 最新

  • duanbipu1720 2012-12-17 03:57
    关注

    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'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能