Asked  7 Months ago    Answers:  5   Viewed   41 times

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:

<form action="http://myServerURL" method="POST" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="Submit">

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); // <-- raw data here hm?
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookieJar);
curl_setopt( $ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1); <-- using this as I wanted to check if HTTPHEADER is set
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data')); <-- setting content-type header?
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

// i get response from the server
$response = curl_exec( $ch );

// with this I can check what kind of content type the last request had?
$requestContentType = curl_getinfo($ch,CURLINFO_CONTENT_TYPE);
echo "<br>request Content Type was:".$requestContentType."<br>";    


echo "<br><b>SERVER POST IMAGE RESPONSE:</b><br>";
echo $response;

With the code below I am able to see my request headers:

curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);


The content-type in request-headers is shown correctly now. But it seems the image is not send correctly as API would expect. Unfortunately I don't have access to the API...

Any help appreciated, thank you



In case anyone had the same problem: check this as @PravinS suggested. I used the exact same code as shown there and it worked for me perfectly.

This is the relevant part of the server code that helped:

if (isset($_POST['btnUpload']))
$url = "URL_PATH of upload.php"; // e.g. http://localhost/myuploader/upload.php // request URL
$filename = $_FILES['file']['name'];
$filedata = $_FILES['file']['tmp_name'];
$filesize = $_FILES['file']['size'];
if ($filedata != '')
    $headers = array("Content-Type:multipart/form-data"); // cURL headers for file uploading
    $postfields = array("filedata" => "@$filedata", "filename" => $filename);
    $ch = curl_init();
    $options = array(
        CURLOPT_URL => $url,
        CURLOPT_HEADER => true,
        CURLOPT_POST => 1,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_POSTFIELDS => $postfields,
        CURLOPT_INFILESIZE => $filesize,
    ); // cURL options
    curl_setopt_array($ch, $options);
        $info = curl_getinfo($ch);
        if ($info['http_code'] == 200)
            $errmsg = "File uploaded successfully";
        $errmsg = curl_error($ch);
    $errmsg = "Please select the file";

html form should look something like:

<form action="uploadpost.php" method="post" name="frmUpload" enctype="multipart/form-data">
  <td align="center">:</td>
  <td><input name="file" type="file" id="file"/></td>
  <td align="center">&nbsp;</td>
  <td><input name="btnUpload" type="submit" value="Upload" /></td>

Wednesday, March 31, 2021
answered 7 Months ago

you may want to look at pandas.resample:

df['Data'].resample('5Min', how='mean')

or, as how = 'mean' is default parameter:


For example:

>>> rng = pd.date_range('1/1/2012', periods=10, freq='Min')
>>> df = pd.DataFrame({'Data':np.random.randint(0, 500, len(rng))}, index=rng)
>>> df
2012-01-01 00:00:00   488
2012-01-01 00:01:00   172
2012-01-01 00:02:00   276
2012-01-01 00:03:00     5
2012-01-01 00:04:00   233
2012-01-01 00:05:00   266
2012-01-01 00:06:00   103
2012-01-01 00:07:00    40
2012-01-01 00:08:00   274
2012-01-01 00:09:00   494
>>> df['Data'].resample('5Min')
2012-01-01 00:00:00    234.8
2012-01-01 00:05:00    235.4

You can find more examples here.

Thursday, August 5, 2021
answered 3 Months ago

You need to convert your string to stream first.

You can simply do it with this piece of code.

$YourString = 'data-id=2010-10-01_15-15-53';
$stream = fopen('php://memory','r+');
fwrite($stream, $YourString );
$dataLength = ftell($stream);

Then having your stream, you can send it using curl.

$curl = curl_init();
curl_setopt_array( $curl,
        , CURLOPT_URL => 'https://someurl'
        , CURLOPT_HTTPHEADER => array(
            'Content-Type: text/plain'
        , CURLOPT_RETURNTRANSFER => 1                     // means output will be a return value from curl_exec() instead of simply echoed
        , CURLOPT_TIMEOUT => 15                           // max seconds to wait
        , CURLOPT_FOLLOWLOCATION => 0                     // don't follow any Location headers, use only the CURLOPT_URL, this is for security
        , CURLOPT_FAILONERROR => 0                        // do not fail verbosely fi the http_code is an error, this is for security
        , CURLOPT_SSL_VERIFYPEER => 1                     // do verify the SSL of CURLOPT_URL, this is for security
        , CURLOPT_VERBOSE => 0                            // don't output verbosely to stderr, this is for security
        , CURLOPT_INFILE => $stream
        , CURLOPT_INFILESIZE => $dataLength
        , CURLOPT_UPLOAD => 1
        ) );

$response = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

This should work. The lines that help you are highlighted below:




Tuesday, August 17, 2021
answered 2 Months ago

Ok, fixed it; the problem was that one had to specify 8-bit depth in the convert command line; thus the correct invocation is:

convert -depth 8 -size 150x150 gradient:#4b4-#bfb rgb:test.raw

Then we have:

du -b test.raw # 67500 bytes
python -c "import zlib,sys;sys.stdout.write(zlib.compress(" < test.raw > test.flate
du -b test.flate # 664 bytes

# replace /Length 664, and then:

perl -ne 's/^###/`cat test.flate`/e;print' hello.pdf > hello2.pdf

Finally, the hello2.pdf opens in evince and displays the bitmap correctly:



Btw, I found this because I'm actually trying to debug an image in another document; so I basically did the following:

# extract and save the stream of this image object
qpdf --show-object=23 --raw-stream-data mybadfile.pdf > myraw.file

# get raw binary data - deflate the saved object stream 
python -c "import zlib,sys;sys.stdout.write(zlib.decompress(" < myraw.file > myraw.deflate

identify myraw.deflate
# identify: no decode delegate for this image format `myraw.deflate' @ constitute.c/ReadImage/530.

identify rgb:myraw.deflate
# identify: Must specify image size `myraw.deflate' @ rgb.c/ReadRGBImage/155.

identify -size 588x508 rgb:myraw.deflate
# rgb:myraw.deflate=>myraw.deflate RGB 588x508 588x508+0+0 16-bit TrueColor DirectClass 875KiB 0.020u 0:00.030
# identify: Unexpected end-of-file `myraw.deflate': No such file or directory @ rgb.c/ReadRGBImage/261.

display -size 588x508 rgb:myraw.deflate
# display: Unexpected end-of-file `myraw.deflate': No such file or directory @ rgb.c/ReadRGBImage/261. ### but it shows correctly, except for size?

identify -depth 8 -size 588x508 rgb:myraw.deflate
# rgb:myraw.deflate=>myraw.deflate RGB 588x508 588x508+0+0 8-bit TrueColor DirectClass 875KiB 0.020u 0:00  ## OK

display -depth 8 -size 588x508 rgb:myraw.deflate 
# OK; choosing rgba: is already bad - so confirmed 8-bit rgb

Hope this helps someone,

Tuesday, August 24, 2021
answered 2 Months ago

Alamofire has an example in their documentation using Alamofire.upload(_:URLString:headers:multipartFormData:encodingMemoryThreshold:encodingCompletion:) that looks like it will answer your question (note that in their example, the headers and encodingMemoryThreshold parameters have a default value if you don't supply one).

Also see their documentation on the various appendBodyPart() methods on a MultipartFormData class instance.

So, a way in which your provided example code could be modified might be:

func getOCR(image: UIImage) {

  let url = ""
  let apiKey = "xxx-xxx-xxx-xxx-xxx"
  let mode = "document_photo"
  let imageData = UIImagePNGRepresentation(image)

    URLString: url,
    multipartFormData: { multipartFormData in
        data: apiKey.dataUsingEncoding(NSUTF8StringEncoding)!,
        name: "apikey"
        data: mode.dataUsingEncoding(NSUTF8StringEncoding)!,
        name: "mode"
        data: imageData!,
        name: "file",
        fileName: "testIMG.png",
        mimeType: "image/png"
    encodingCompletion: { encodingResult in
      switch encodingResult {
        case .Success(let upload, _, _):
          upload.responseJSON { _, _, JSON in println(JSON) }
        case .Failure(let encodingError):
Monday, October 4, 2021
Alex Chernilov
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :