如何使用cURL而不是exec()?

时间:2013-05-12 03:00:00

标签: php curl

我收到错误:

  

警告:出于安全原因,exec()已被停用

对于以下代码,有关如何在不使用exec()的情况下解决此问题的任何想法?

$file = 'test.jpg';
$cmd  = 'curl -F userfile=$file ' .
        '-F outputencoding="utf-8" ' .
        '-F outputformat="txt" ' .
        'http://maggie.ocrgrid.org/cgi-bin/weocr/ocr_scene.cgi >result.txt';
exec($cmd, $result);
echo $result;

1 个答案:

答案 0 :(得分:0)

试试这个:

$url = 'http://maggie.ocrgrid.org/cgi-bin/weocr/ocr_scene.cgi';
$filename = 'test.jpg';

// postfields
$data = array(
    'userfile' => '@'.realpath($filename)
);

$fp1 = fopen('res_error.txt','w');
$fp2 = fopen('res_header.txt','w');

// cURL
$ch = curl_init();
curl_setpot_array($ch, array(
    CURLOPT_URL            => $url  ,
    CURLOPT_POSTFIELDS     => $data ,
    CURLOPT_CONNECTTIMEOUT => 60    ,
    CURLOPT_HEADER         => false ,
    CURLOPT_RETURNTRANSFER => true  ,
    CURLOPT_POST           => true  ,
    CURLOPT_VERBOSE        => true  ,
    CURLOPT_STDERR         => $fp1  ,
    CURLOPT_WRITEHEADER    => $fp2  ,
));
$res = curl_exec($ch);

fclose($fp1);
fclose($fp2);

echo $res;