2017-12-13 12 views
-1

Curl을 사용하여 파일 업로드 REST API를 호출하려고하지만 빈 화면이 반환됩니다. 잘못된 점을 알고 있습니다. 파일 업로드 API 성공하면 파일 이름을 반환합니다. 우편 배달부 도구를 사용하여 fule 업로드 나머지 API를 호출 할 수있는 것과 동일한 것.CURL Fileupload REST API

오류 :

{ 
    "Message": "An error has occurred.", 
    "ExceptionMessage": "Sequence contains no matching element", 
    "ExceptionType": "System.InvalidOperationException", 
    "StackTrace": " at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)\r\n at PharmaRackv2.WebUI.Areas.Admin.Controllers.UploadController.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Threading.Tasks.TaskHelpersExtensions.d__3`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()" 
} 

PHP 코드 : 그것은 잘 작동

try { 
      $file = "D:\xampp\htdocs\api.pharmarack.com\distributors\11380\08122017123602.zip"; 
      $curl = curl_init(); 
      curl_setopt_array($curl, array(
       CURLOPT_URL => "http://test.com/api/upload", 
       CURLOPT_RETURNTRANSFER => true, 
       CURLOPT_ENCODING => "", 
       CURLOPT_MAXREDIRS => 10, 
       CURLOPT_TIMEOUT => 30, 
       CURLOPT_CUSTOMREQUEST => "POST", 
       CURLOPT_POSTFIELDS => array(
       'attachment' => curl_file_create($file), 
      ), 
       CURLOPT_HTTPHEADER => array(
        "cache-control: no-cache", 
        "content-type: multipart/form-data", 
        "distributorcode: 1HUBH", 
        "foldername: 1HUBH" 
       ), 
      )); 

      $response = curl_exec($curl); 
      $err = curl_error($curl); 

      curl_close($curl); 

      if ($err) { 
       echo "cURL Error #:" . $err; 
      } else { 
       echo $response; 
      } 
     } catch (Exception $e) { 
      echo 'Message: ' . $e->getMessage(); 
     } 

우체부 CALL. 파일 업로드 API가 성공하면 파일 이름을 반환합니다. enter image description here enter image description here

답변

2

문제는 당신이 당신의 컬에 오류가 발생하는 이유 즉, DistributorCodeFolderName는 대소 문자를 구분한다는 것입니다. 좋아하는 수정 :

CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache", 
    "content-type: multipart/form-data", 
    "DistributorCode: 1HUBH", 
    "FolderName: 1HUBH" 
), 

편집 : 문제는 multipart/form-data, 당신은 boundries을 통해 데이터를 보낼 필요가, 당신은 보통 포스트 PARAM처럼 file=abc.php처럼 보낼 수 있다는 것입니다.

<?php 
$curl = curl_init(); 

curl_setopt_array($curl, array(
    CURLOPT_URL => "http://ragabh.com/api/upload", 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_ENCODING => "", 
    CURLOPT_MAXREDIRS => 10, 
    CURLOPT_TIMEOUT => 30, 
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 
    CURLOPT_CUSTOMREQUEST => "POST", 
    CURLOPT_POSTFIELDS => "------FormData\r\nContent-Disposition: form-data; name=\"attachment\"; filename=\"E:\\c5-500x500.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------FormData--", 
    CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache", 
    "content-type: multipart/form-data; boundary=----FormData", 
    "DistributorCode: 1HUBH", 
    "FolderName: 1HUBH", 
), 
)); 

$response = curl_exec($curl); 
$err = curl_error($curl); 

curl_close($curl); 

if ($err) { 
    echo "cURL Error #:" . $err; 
} else { 
    echo $response; 
}