PHP Base64 encoding for JSON slash issue -


i using elastic-php api 2.0 create index of word , pdf documents. requires send base64 encoding of document json mapper attachment plugin.

however, php's base64 generates slashes \ in encoded string. json trying construct encoding, cannot parsed elastic:

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

i not want remove/replace slashes, suggested in stackoverflow posts, since may cause problems later decoding.

how slashes in base64 encoding dealt in such scenarios?

it better not build json string yourself, let json_encode job, takes care of slashes. don't need addslashes then:

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

note new line characters insert chunk_split escaped during encoding, json not allow non-escaped line breaks in strings. if receiving end decodes json string in right way, result in value $obj has in above code, content having line breaks.

in elastic blog post, author removes line breaks in base64 encoded string. scala code presented there has this:

"image" :"${new base64encoder().encode(data).replaceall("[\n\r]", "")}" 

this seems suggest shouldn't using chunk_split either, suggested php code becomes:

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

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -