Here is a new PHP code snippet. The code is from an OCR.space user, posted here with permission:
Problem:
cURL call to API ocr.space with PHP not going through?
Answer:
Problems were:
- do not use
json_encode()
- change content type to
Content-Type:multipart/form-data
- set quotation marks around
"false"
and"true"
values.
This works now:
$imageurl = "https://...imageurl.jpg";
$data = array(
"language" => "ger",
"isOverlayRequired" => "false",
"detectOrientation" => "true",
"scale" => "true",
"filetype" => 'JPG',
"url" => $imageurl,
);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => "https://api.ocr.space/parse/image",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => array('Content-Type:multipart/form-data', 'apikey:apikey:MYAPIKEY'),
CURLOPT_POSTFIELDS => $data,
));
$result = curl_exec($ch);
curl_close($ch);
$result_array = json_decode($result);
if(!empty($result_array->ErrorMessage))
{
// catch errors https://docs.mathpix.com/#errors
echo 'Problem: '.$result_array->ErrorMessage[0];
}
else
{
// recognized text
$ocrresult = $result_array->ParsedResults[0]->ParsedText;
}
See also this C# post “No file uploaded or URL provided” when calling ocr.space API