OCR free API Whit PHP

Hi, i m using free OCR space API whit PHP but the post parameters dont change my result, is correct this code?

   $r = $client->request('POST', 'https://api.ocr.space/parse/image',[
    'headers' => ['apiKey' => 'xxxxxxxxxxxxxx'],
    'multipart' => [
        [
            'name' => 'file',
            'contents' => $fileData
        ]
    ]
], ['file' => $fileData],
   ['OCREngine' => '2'],
   ['language' => 'ita'],
   ['detectOrientation' => true],
   ['scale' => true]);

Hi, what do you mean with “does not change result”? Do you get a result back? If so, what is wrong with it?

Hi! I don’t know if I must define the arrays parameter after $file => file data or after apikey… I ask you because the parameter don’t work

See here: How Use OCREngine 2 on PHP? (same for engine1, of course)

  [
	'language' => 'por', 
	'OCREngine' =>  '1',               
    'name' => 'file',
    'contents' => $fileData
 ]

thank you but it the same, do not change the result, what is wrong?

if(isset($_POST['submit']) && isset($_FILES)) {
require __DIR__ . '/vendor/autoload.php';
$target_dir = "uploads/";
$uploadOk = 1;
$FileType = strtolower(pathinfo($_FILES["attachment"]["name"],PATHINFO_EXTENSION));
$target_file = $target_dir . generateRandomString() .'.'.$FileType;
// Check file size
if ($_FILES["attachment"]["size"] > 5000000) {
    header('HTTP/1.0 403 Forbidden');
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
if($FileType != "pdf" && $FileType != "png" && $FileType != "jpg") {
    header('HTTP/1.0 403 Forbidden');
    echo "Sorry, please upload a pdf file";
    $uploadOk = 0;
}
if ($uploadOk == 1) {

    if (move_uploaded_file($_FILES["attachment"]["tmp_name"], $target_file)) {
        uploadToApi($target_file);
    } else {
        header('HTTP/1.0 403 Forbidden');
        echo "Sorry, there was an error uploading your file.";
    }
} 

} else {
header(‘HTTP/1.0 403 Forbidden’);
echo “Sorry, please upload a pdf file”;
}

function uploadToApi($target_file){
require DIR . ‘/vendor/autoload.php’;
$fileData = fopen($target_file, ‘r’);
$client = new \GuzzleHttp\Client();
try {
$r = $client->request(‘POST’, ‘https://api.ocr.space/parse/image’,[
‘headers’ => [‘apiKey’ => ‘xxxxxxxxxxx’],
‘multipart’ => [
[
‘language’ => ‘ita’,
‘detectOrientation’ => ‘true’,
‘scale’ => ‘true’,
‘OCREngine’ => ‘2’,
‘name’ => ‘file’,
‘contents’ => $fileData
]
]
], [‘file’ => $fileData]);
$response = json_decode($r->getBody(),true);
if($response[‘ErrorMessage’] == “”) {
?>

Result
Result <?php foreach($response['ParsedResults'] as $pareValue) { echo $pareValue['ParsedText']; } ?>
<?php } else { header('HTTP/1.0 400 Forbidden'); echo $response['ErrorMessage']; } } catch(Exception $err) { header('HTTP/1.0 403 Forbidden'); echo $err->getMessage(); } }

function generateRandomString($length = 10) {
$characters = ‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’;
$charactersLength = strlen($characters);
$randomString = ‘’;
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}