php - How to POST PDF content as application/pdf using cURL -


i'm new curl , use .net or jquery posting web apis, have requirement use existing php implementation post pdf content-type: application/pdf web api.

the existing implementation posts , receives json data, it's pretty straight forward. when try change code post application/pdf content endpoint, keep getting following error:

file of wrong type. acceptable content types application/pdf, text/richtext, text/plain, image/jpeg.

here code using:

$curl = curl_init($url);   curl_setopt($curl, curlopt_returntransfer, true);   curl_setopt($curl, curlopt_failonerror, false);   curl_setopt($curl, curlopt_ssl_verifypeer, false);   curl_setopt($curl, curlopt_header, true); curl_setopt($curl, curlopt_httpheader, array('content-type: application/pdf'),                                               $auth_header));    curl_setopt($curl, curlopt_post, 1);                                          curl_setopt($curl, curlopt_postfields, file_get_contents('c:\myfile.pdf'));   $response = curl_exec($curl);   if (!$response) {     $response = curl_error($curl);   }   curl_close($curl);   

i have checked pdf valid pdf , tried several other pdfs. have set curl options write error log, shows me curl setting content-type application/pdf in header.

am using wrong function pdf content file? or setting body content wrong? api calling has documentation states pdf content should posted in body, no mention of posting parameter. says set content-type: application/pdf in header, have done. doesn't else, assume should easy call, maybe don't know enough curl...

assuming you're using php 5.5+, need use curlfile uploading file:

$file = new curlfile('c:\myfile.pdf','application/pdf','myfile'); // curl here curl_setopt($curl, curlopt_postfields, ['pdf' => $file]); ... 

in case you're using older version, perhaps should try adding "@" in front of file name suggested here:

curl_setopt($curl, curlopt_postfields, array('name' => 'pdf', 'file' => '@c:\myfile.pdf');  

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 -