cURL调用API ocr.space与PHP无法通过?

API: https://ocr.space/ocrapi#curl

My code:

$imageurl = "https://...imageurl.jpg";

$data = array(
    "language" => "ger",
    "isOverlayRequired" => false,
    "detectOrientation" => true,
    "scale" => true,
    "filetype" => 'JPG',
    "url" => $imageurl,
    // "base64Image" => "data:image/jpeg;base64,".base64_encode( file_get_contents($imageurl) ),
);

$ch = curl_init();

curl_setopt_array($ch, array(
    CURLOPT_URL => "https://api.ocr.space/parse/image",
    CURLOPT_RETURNTRANSFER => true, // receive server response
    CURLOPT_POST => 1, // do post
    CURLOPT_HTTPHEADER => array('Content-Type:application/json', 'apikey:MYAPIKEY'),
    CURLOPT_POSTFIELDS => json_encode($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];
}

Always leads to:

Problem: No file uploaded or URL or base64 provided

I have tried it with url and base64Image, but still the same problem.

Anyone can see what is going on?

--

This C# post "No file uploaded or URL provided" when calling ocr.space API says that the file content must be read and posted. However, I am pretty sure the url option should accept a url only.

Thanks for all your help (see comments).

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;
}