[英]cURL call to API ocr.space with PHP not going through?
API: https://ocr.space/ocrapi#curl 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 问题:未上传文件或未提供URL或base64
I have tried it with url
and base64Image
, but still the same problem. 我已经尝试使用url
和base64Image
,但是仍然是同样的问题。
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. 调用ocr.space API时,此C#帖子“没有上传文件或提供URL”表示必须读取和发布文件内容。 However, I am pretty sure the url
option should accept a url only. 但是,我很确定url
选项应该只接受一个url。
Thanks for all your help (see comments). 感谢您的所有帮助(请参阅评论)。
Problems were: 问题是:
json_encode()
不要使用json_encode()
Content-Type:multipart/form-data
将内容类型更改为Content-Type:multipart/form-data
"false"
and "true"
values. 在"false"
和"true"
值周围设置引号。 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.