Read API documentation: How to POST binary data using cURL in PHP?

Ertugrul Kurnaz 1 Reputation point
2021-01-15T10:45:36.647+00:00

I got the same problem, I only get words in my response. This is a problem in my case since I will need to get numeric values (partially code). The Read API documentation is really poor and I could not figure out how to send my binary data file using cURL in PHP.

in OCR (console) this will be the request:

POST https://westeurope.api.cognitive.microsoft.com/vision/v3.1/ocr?language=unk&detectOrientation=true HTTP/1.1
Host: westeurope.api.cognitive.microsoft.com
Content-Type: multipart/form-data

[Binary image data]

in PHP my code looks like this:

  $mime = mime_content_type($file['tmp_name']);
  $postfields = array('image' => new CURLFile($file['tmp_name'], $mime, $file['name']));

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$ocr_url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$postfields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                        'Content-Type: multipart/form-data', 
                        'Ocp-Apim-Subscription-Key: '.$ocr_key
                    ));
    $result = curl_exec($ch);
    curl_close($ch);

This works for OCR but when I want to use the Read API multipart/form-data is not an option as content-type. Changing the content-type (and the URL of course) in the Read API does not help. I have already tried several ways.
Is there anyone who can help me out please ?

Azure Computer Vision
Azure Computer Vision
An Azure artificial intelligence service that analyzes content in images and video.
326 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. romungi-MSFT 42,976 Reputation points Microsoft Employee
    2021-01-18T08:00:16.097+00:00

    @Ertugrul Kurnaz Yes, the content-type multipart/form-data is not supported by Read API. content-type application/octet-stream seems to be the correct type to use. Did you get a chance to try this with application/octet-stream?
    I am not really an expert of PHP but the sample for PHP on the console page with the URL in the body seems to be provided. Does this work for your scenario?

    0 comments No comments