douyuanqia665858 2018-07-25 15:20
浏览 118

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.

  • 写回答

1条回答 默认 最新

  • dtyz76562 2018-07-25 15:45
    关注

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条