dtcd27183
dtcd27183
2016-02-06 10:14

用于JSON斜杠问题的PHP Base64编码

已采纳

I am using Elastic-PHP API 2.0 to create an index of Word and PDF documents. This normally requires to send a Base64 encoding of the document as JSON to its Mapper attachment plugin.

However, PHP's Base64 generates slashes \ in the encoded string. The JSON which I am trying to construct with this encoding, cannot be parsed by Elastic:

$json = 
    '{"content" : "'.addslashes(chunk_split(base64_encode($file_contents))).'"}'

I do not want to remove/replace the slashes, as suggested in some Stackoverflow posts, since it may cause problems later with decoding.

How are slashes in Base64 encoding dealt with in such scenarios?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dtjpnd7517 dtjpnd7517 5年前

    It is better not to build the JSON string yourself, but to let json_encode do the job, which takes care of slashes. You don't need addslashes then:

    // create the object
    $obj = array(
        "content" => chunk_split(base64_encode($file_contents))
    );
    // encode the object in JSON format
    $json = json_encode($obj);
    

    Note that the new line characters that you insert with chunk_split will be escaped during the encoding, as JSON does not allow non-escaped line breaks in strings. If the receiving end decodes the JSON string in the right way, it will result in the value $obj has in the above code, with content having line breaks.

    In an Elastic blog post, the author even removes any line breaks in the base64 encoded string. The Scala code presented there has this:

    "image" :"${new BASE64Encoder().encode(data).replaceAll("[
    ]", "")}"
    

    This really seems to suggest you shouldn't be using chunk_split either, so then the suggested PHP code becomes:

    // create the object
    $obj = array(
        "content" => base64_encode($file_contents)
    );
    // encode the object in JSON format
    $json = json_encode($obj);
    
    点赞 评论 复制链接分享
  • duanjianlu0506 duanjianlu0506 5年前

    Please use php function

    addslashes() to add slash or any special character , to retrive slashes use stripslashes().

    thanks...

    点赞 评论 复制链接分享