2013-10-29 4 views
0

클라이언트 및 CodeIgniter Rest Server로 cURL을 사용하여 몇 가지 테스트를 수행합니다. GET, POST 및 DELETE 메소드는 완벽하게 작동하지만 PUT은 작동하지 않습니다.CodeIgniter Rest Server가 cURL을 통해 PUT 메소드와 함께 실패합니다.

다음은 PUT에 대한 내 클라이언트 코드입니다. 그것은 (CURLOPT_CUSTOMREQUEST 제외) POST와 동일합니다 :

<?php 

    /** 
    * Keys 
    */ 
    include('inc/keys.inc.php'); 

    /** 
    * Data sent 
    */ 
    $content = array(
     'name' => 'syl', 
     'email' => '[email protected]' 
    ); 

    /** 
    * Source 
    */ 
    $source = 'http://localhost/test-rest-api-v2/api_apps/app/id/1'; 

    /** 
    * Init cURL 
    */ 
    $handle = curl_init($source); 

    /** 
    * Headers 
    */ 
    $headers = array(
     'X-API-Key: '. $public_key 
    ); 

    /** 
    * Options 
    */ 
    curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); 

    /** 
    * For POST 
    */ 
    curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT'); 
    curl_setopt($handle, CURLOPT_POSTFIELDS, $content); 

    /** 
    * Result 
    */ 
    $result = curl_exec($handle); 

    /** 
    * Close handle 
    */ 
    curl_close($handle); 

    echo $result; 
?> 

는 또한 헤더에 추가하려고 : 'Content-Type: application/x-www-form-urlencoded',. 같은 결과.

내 서버 번호 :

<?php 
    function app_put() { 
     var_dump($this->put()); 
    } 
?> 

결과 :

어레이 (1) {[ "-------------------- ---------- 1f1e080c85df 콘텐츠 - 처리 : _form-data; _name "] => 문자열 (174)" ""이름 syl --------------- --------------- 1f1e080c85df 콘텐츠 - 처분 : 양식 데이터, 이름 = "이메일"[email protected] --------------- --------------- 1f1e080c85df - "}

PUT 방법의 문제점은 무엇입니까?

+0

: -d 'X-API-키 MY_API_KEY'를 '이름 = SYL 및 이메일 = 시험'HTTP : //rest-api.local/api_apps/app/ id/1. 그럼 내 PHP 클라이언트입니다. – Syl

답변

0

올바른 방법으로 발견되었습니다. 파일 업로드를 "시뮬레이트"해야합니다. PUT 요청에만 해당 :

<?php 

    /** 
    * Data 
    */ 
    $data = array(
     'name' => 'syl', 
     'email' => '[email protected]' 
    ); 

    /** 
    * Convert array to an URL-encoded query string 
    */ 
    $data = http_build_query($data, '', '&'); 

    /** 
    * Open PHP memory 
    */ 
    $memory = fopen('php://memory', 'rw'); 
    fwrite($memory, $data); 
    rewind($memory); 

    /** 
    * Simulate file uploading 
    */ 
    curl_setopt($handle, CURLOPT_INFILE, $memory); 
    curl_setopt($handle, CURLOPT_INFILESIZE, strlen($data)); 
    curl_setopt($handle, CURLOPT_PUT, true); 

?> 
2

동일한 문제가있어이 게시물을 발견했습니다. 그런 다음 http_build_query를 찾았고 파일 업로드를 "시뮬레이트"하지 않고 트릭을 수행했습니다.

-H PUT -X 컬 그것은 터미널 작동
curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($content));