4

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?

Community
  • 1
  • 1
dr_rk
  • 4,395
  • 13
  • 48
  • 74

2 Answers2

7

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("[\n\r]", "")}"

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);
trincot
  • 317,000
  • 35
  • 244
  • 286
-2

Please use php function

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

thanks...

iLaxo Mit
  • 77
  • 6