dquh37673 2015-04-29 09:04
浏览 82
已采纳

cURL PHP API调用不起作用

Trying to make a cURL PHP API call to WEocr, but I can't receive a response by the API. Here's the code I am using:

    $url = 'http://jimbocho.ocrgrid.org/cgi-bin/weocr/submit_ocrad.cgi';
    $header = array('Content-Type: multipart/form-data');
    $fields = array('userfile' => '@' . $_FILES['file']['tmp_name'][0]);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    $result = json_decode(curl_exec($ch));

I don't know if i'm writing a incorrect syntax of the cURL command.

Thanks in advance.

The error i'm getting:

Sorry!
Received a null image (0byte). See TIPS page. 
  • 写回答

1条回答 默认 最新

  • dt20081409 2015-04-29 09:11
    关注

    I guess your path isn't correct, try this:

    $url = 'http://jimbocho.ocrgrid.org/cgi-bin/weocr/submit_ocrad.cgi';
    $file_name_with_full_path = '/home/example/dir/sample.jpeg';
    $post = array('userfile'=>'@'.$file_name_with_full_path);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    $result=curl_exec ($ch);
    curl_close ($ch);
    

    Update based on your comments, also try this:

    $file_new = /path/to/your/file.jpg
    $cmd="curl -F userfile=$file_new \ 
          -F outputencoding=\"utf-8\" \
          F outputformat=\"txt\" \ 
          http://maggie.ocrgrid.org/cgi-bin/weocr/ocr_scene.cgi >result.txt"
    $result = shell_exec($cmd);
    print_r($result);
    

    Note: You don't need to set CURLOPT_POST 1 if you're using CURLOPT_POSTFIELDS , curl do that for you automagically. Curl also sets the Content-Type: multipart/form-data, if the post content is an array, that's your case, no need for that too.


    CURLOPT_POSTFIELDS

    The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix. As of PHP 5.5.0, the @ prefix is deprecated and files can be sent using CURLFile. The @ prefix can be disabled for safe passing of values beginning with @ by setting the CURLOPT_SAFE_UPLOAD option to TRUE.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)