html5 - cURL PHP API call not working -


trying make curl php api call weocr, can't receive response api. here's code 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 incorrect syntax of curl command.

thanks in advance.

the error i'm getting:

sorry! received null image (0byte). see tips page.  

i guess 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 comments, 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: don't need set curlopt_post 1 if you're using curlopt_postfields , curl automagically. curl sets content-type: multipart/form-data, if post content array, that's case, no need too.


curlopt_postfields

the full data post in http "post" operation. post file, prepend filename @ , use full path. filetype can explicitly specified following filename type in format ';type=mimetype'. parameter can either passed urlencoded string 'para1=val1&para2=val2&...' or array field name key , field data value. if value array, content-type header set multipart/form-data. of php 5.2.0, value must array if files passed option @ prefix. of php 5.5.0, @ prefix deprecated , files can sent using curlfile. @ prefix can disabled safe passing of values beginning @ setting curlopt_safe_upload option true.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -