Curl PHP PUT request changes file content -
i use put request curl php in 1 of scripts upload file external file repository using rest api. however, uploaded files contain information content-disposition, content-type , alpha-numeric code @ both beginning , end of file, rendering useless unless manually enditing again.
the unnecessary overhead looks this:
--------------------------e876c4b8eee91562 content-disposition: form-data; name="test_file"; filename="test_3.xlsx" content-type: application/octet-stream
the code use request is:
$ch = curl_init($upload_url); $cfile = new curlfile($file_realpath, 'application/octet-stream', $base_file_name); $data = array ('test_file' => $cfile); curl_setopt($ch, curlopt_customrequest, "put"); curl_setopt($ch, curlopt_postfields, $data); curl_setopt($ch, curlopt_httpheader, array( 'accept:application/json', 'content-type:application/octet-stream', )); b2share_ignore_certificates($ch); curl_setopt($ch, curlinfo_header_out, true); curl_setopt($ch, curlopt_returntransfer, true); $output = curl_exec($ch); $http_status = curl_getinfo($ch, curlinfo_http_code); $headersent = curl_getinfo($ch, curlinfo_header_out); curl_close($ch);
i can't change request post, rest api on other side needs put request. ideas on how rid of overhead?
ok. found answer question here: php curl post file without header
thus changing line
curl_setopt($ch, curlopt_postfields, $data);
to
curl_setopt($ch, curlopt_postfields, file_get_contents($file_realpath));
fixes issue. not "clean" solution, works. i'd glad hear better solutions though.
Comments
Post a Comment