I am trying to post an image with cURL in PHP using multipart/form-data header since API that I am sending to is expecting image to be sent as multi-part form.
I don't have problems talking to the API with other requests; only posting an image is an issue.
I am using this form on client side:
and this is the server I am posting to [here I am trying to post this data forward to an API]:
$ch = curl_init[$url];
curl_setopt [$ch, CURLOPT_POST, 1];
curl_setopt [$ch, CURLOPT_POSTFIELDS, $imgRawData]; // $content] {
$data .= "--" . $delimiter . $eol
. 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $name . '"' . $eol
//. 'Content-Type: image/png'.$eol
. 'Content-Transfer-Encoding: binary'.$eol
;
$data .= $eol;
$data .= $content . $eol;
}
$data .= "--" . $delimiter . "--".$eol;
return $data;
}
Some web services ask to upload or Post some files on server. In this post we will explain you How to send files via Form Post. We are using cURl to send files on server. cURL is very powerful
library, you can Get or Post data using cURL method. First create a simple HTML form to upload a file. Make sure that form use ‘multipart/form-data‘ enctype value. This will post data in URL encrypted form.
Please chooseafile:
PHP cURL code to Send File
Now create a receiver.php file to handle this form post request.
In this file cURL send file to any web server. Check it out below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
If you found that file is imported as empty or any other issue, must upload the file in target path.
Than try to upload file again using html form. This will work without any issue.
Recommended Posts For You
- How to Convert HTML to PDF Document in PHP using fpdf
- Visitor Counter in PHP for Website
- Multiple Star Rating Feature on Single Page in PHP, MySQL and Ajax
About Harish
I am professional web developer and blogger. Use this blog to share own api and other web development experience with you. I do accept paid work. Write to me at -
View all posts by Harish