반응형
PHP cURL HTTP PUT
cURL을 사용하여 HTTP PUT 요청을 생성하려고하는데 작동하지 않습니다. 나는 많은 튜토리얼을 읽었지만 실제로는 작동하지 않았습니다. 내 현재 코드는 다음과 같습니다.
$filedata = array('metadata' => $rdfxml);
$ch = curl_init($url);
$header = "Content-Type: multipart/form-data; boundary='123456f'";
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($filedata));
$returned = curl_exec($ch);
if (curl_error($ch))
{
print curl_error($ch);
}
else
{
print 'ret: ' .$returned;
}
나는 또한 PHP PEAR를 사용해 보았지만 동일한 결과를 얻었습니다. 문제는 저장소에서 메타 데이터가 설정되지 않았다고 말합니다. 정말 도움이 필요 해요! 감사!
오늘 제가 그렇게 해왔어요 ... 여기 제가 저를 위해 일하는 코드가 있습니다 ...
$data = array("a" => $a);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
if (!$response)
{
return false;
}
소스 : http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl
Chrome 용 Postman을 사용하여 CODE를 선택하면이 기능을 사용할 수 있습니다.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://blablabla.com/comorl",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => "{\n \"customer\" : \"con\",\n \"customerID\" : \"5108\",\n \"customerEmail\" : \"jordi@correo.es\",\n \"Phone\" : \"34600000000\",\n \"Active\" : false,\n \"AudioWelcome\" : \"https://audio.com/welcome-defecto-es.mp3\"\n\n}",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json",
"x-api-key: whateveriyouneedinyourheader"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
POST 메서드에서 배열을 넣을 수 있습니다. 그러나 PUT 메소드에서는 다음 http_build_query
과 같은 매개 변수를 작성하는 데 사용해야 합니다.
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $postArr ) );
표준 2 개를 혼합했습니다.
오류는 $header = "Content-Type: multipart/form-data; boundary='123456f'";
The function http_build_query($filedata)
is only for "Content-Type: application/x-www-form-urlencoded", or none.
참고URL : https://stackoverflow.com/questions/5043525/php-curl-http-put
반응형
'program story' 카테고리의 다른 글
명령 줄을 통해 GNU make 변수에 추가 (0) | 2020.12.11 |
---|---|
SessionTimeout : web.xml 대 session.maxInactiveInterval () (0) | 2020.12.11 |
MongoDB에서 $ in 쿼리에 전달되는 최대 매개 변수 수는 얼마입니까? (0) | 2020.12.11 |
"현재 사용 중이므로 데이터베이스를 삭제할 수 없습니다." (0) | 2020.12.11 |
연결된 응용 프로그램으로 파일 열기 (0) | 2020.12.11 |