2015-01-16 6 views
4

나는 초보자이며 HP의 IDOL OnDemand API로 운동하여 어떤 이미지 파일에서 텍스트를 추출하고 있습니다.파일 업로드에 @filename API를 사용하는 것이 더 이상 사용되지 않습니다. 대신 CURLFile 클래스를 사용하십시오.

curl 연결을 설정하고 api 요청을 실행해야했지만 PHP 메소드에서 @ method를 사용하여 파일을 게시하려고 할 때 해당 deprecated가 CURLFile을 사용하도록 권장합니다.

는 또한 PHP 매뉴얼을 팠 및 다음과 같습니다이 https://wiki.php.net/rfc/curl-file-upload

코드 같은 것을 함께했다 :

$url = 'https://api.idolondemand.com/1/api/sync/ocrdocument/v1'; 

$output_dir = 'uploads/'; 
if(isset($_FILES["file"])){ 

$filename = md5(date('Y-m-d H:i:s:u')).$_FILES["file"]["name"]; 

move_uploaded_file($_FILES["file"]["tmp_name"],$output_dir.$filename); 

$filePath = realpath($output_dir.$filename); 
$post = array(
    'apikey' => 'apikey-goes-here', 
    'mode' => 'document_photo', 
    'file' => '@'.$filePath 
); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_POST,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
$result = curl_exec($ch); 
curl_close($ch); 
echo $result; 

unlink($filePath); 

어떤 재 작성 코드의 경우와 어떻게 감사 할 것 Curlfile를 사용하는 나에게 보여줍니다. 나는 제 3 자 API를 통합 할 때 때문에 시간 압력에

덕분에,

+0

이 질문에 게시 한 API 키 사용을 중단하고 싶을 수도 있습니다. 지금 공개 속성이므로 ... : 삭제했습니다. 그러나 편집 기록에 영원히 남아 있습니다. (참고 : HP 근무) –

답변

11

나는 '@'.$filePath 대신 CurlFile을 사용하는 것이 간단하다고 생각합니다.

$post = array('apikey' => 'key', 'mode' => 'document_photo', 'file' => new CurlFile($filePath));

은 위의 나를 위해 일했다.

참고 : 저는 HP에서 근무하고 있습니다.

1

, 나는 빠른 해결 방법을했다. 아래 코드를 찾을 수 있습니다.

$ url : 게시 할 도메인 및 페이지. 예 : http://www.snyggamallar.se/en/ $ params : array [key] = value 형식으로, $ post에있는 것과 같습니다.

경고 : @로 시작하는 값은 파일로 취급되며 이는 물론 제한 사항입니다. 제 경우에는 문제가되지 않지만 코드에서 고려하십시오.

static function httpPost($url, $params){ 
    foreach($params as $k=>$p){ 
     if (substr($p, 0, 1) == "@") { // Ugly 
      $ps[$k] = getCurlFile($p); 
     } else { 
      $ps[$k] = utf8_decode($p); 
     } 
    } 

    $ch = curl_init($url); 
    curl_setopt ($ch, CURLOPT_POST, true); 
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $ps); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); 

    $res = curl_exec($ch); 
    return $res; 
} 

static function getCurlFile($filename) 
{ 
    if (class_exists('CURLFile')) { 
     return new CURLFile(substr($filename, 1)); 
    } 
    return $filename; 
}