2012-02-08 7 views
1

PHP 5.3.6을 사용하고 있으며 CUT을 사용하여 PUT 요청을 만들 수 없습니다.PHP에서 CURL을 사용하여 PUT 요청하기

function put_data($url, $data) 
{ 
    $useragent="SimpleAgent-1.0"; 
    $fh = fopen('php://memory', 'rw'); 
    fwrite($fh, $data); 
    rewind($fh);$ch = curl_init(); 
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent); 
    curl_setopt($ch, CURLOPT_INFILE, $fh); 
    curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data)); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
    curl_setopt($ch, CURLOPT_PUT, 1); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $result = curl_exec($ch); 
    curl_close($ch); 
    fclose($fh); 

    return $result; 
} 

여기에서 $ data는 내가 PUT 할 문자열입니다. 작동 다음과 같은 오류를 반환하지 않습니다 :

난 당신의 코드가 너무 URL을 정의하는 문자열과 데이터를 작성하고 예상대로 모든 일을 사용

500 Internal Server Error The server has either erred or is incapable of performing the requested operation.

expected string or buffer

+0

PHP에'error_log'가 있습니까? – alex

+0

되감기 전에 닫으려고합니다 ($ fh); 어쨌든 아파치 로그를 읽으십시오 – ZiTAL

+0

되감기 후에 죄송합니다 ($ fh); – ZiTAL

답변

0

나는 현재 사용중인 버전의 데이터로만 배열을 전달할 수 있습니다. 그래서 지금 내가하고있는 일입니다 :

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE); 
curl_setopt($ch, CURLOPT_COOKIE, $curl_cookie); 
$arr = array(); 
if (isset($data)) { 
    $arr['my_data'] = $data; 
} 

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($arr)); 
curl_exec($ch); 
0

. 나는 그 웹 사이트에 의해 거절 당했으며, 그 끝을 처리 할 수있는 끝이 없었습니다. 단지 라인 curl_setopt($ch, CURLOPT_VERBOSE, true);

를 추가 쉽게 정보를 얻으려면, 당신은 같은 것을 얻을 것이다 : 당신이 데이터를 넣을 그러나 때, 나가서 당신은 로그인 요청에서 볼 수 있듯이

* About to connect() to yyyy.xxxx.com port 80 (#0) 
* Trying 62.221.196.28... 
* connected 
* Connected to yyyy.xxxx.com (zz.221.196.28) port 80 (#0) 
> PUT/HTTP/1.1 
User-Agent: SimpleAgent-1.0 
Host: yyyy.xxxx.com 
Accept: */* 
Content-Length: 14 
Expect: 100-continue 

< HTTP/1.1 100 Continue 
* We are completely uploaded and fine 
< HTTP/1.1 405 Method Not Allowed 
< Date: Thu, 09 Feb 2012 19:46:28 GMT 
< Server: Apache 
< Allow: GET,HEAD,POST,OPTIONS 
< Vary: Accept-Encoding 
< Content-Length: 231 
< Content-Type: text/html; charset=iso-8859-1 
< 
* Connection #0 to host yyy.xxxx.com left intact 
* Closing connection #0 

을의 아파치 설정을 사용하면 해당 URL에 데이터를 넣을 수 없습니다. 따라서 서버에 따라 PUT을 수락하는 수신 URL을 처리해야합니다.

+1

안녕하세요 user1200241, 어떤 PHP 버전을 사용하고 있습니까? 내가 게시 한 코드는 이전 버전의 PHP에서 작동하는 것 같습니다. 저에게 알려주세요. –

+0

안녕 Bijoy 다른 테스트를 할 때 메모리 누수 문제가 발생하여 최신 PHP 5.3.10 버전으로 업그레이드했습니다. cli 아래서 예제를 실행했는데, 가장 유용한 확장은 실제 진행 정보를 얻는 자세한 옵션입니다. 안톤 – Anton

관련 문제