개발! 딱 깔끔하고 센스있게!

PHP cURL POST 가 지원하지 않는 미디어 형식으로 되돌아가겠습니다 본문

개발 스크랩 메모/PHP

PHP cURL POST 가 지원하지 않는 미디어 형식으로 되돌아가겠습니다

렉사이 2020. 12. 18. 23:57

나는 간단한 PHP 스크립트를 통해 HTTP POST 요청을 보내며 json 문자열을 호응 (기존 라이브를 좋아하는 라이브러리, 예를 들어 pecl HTTTP / HTTTP requesst를 보내는 것을 기대한다.

시종 실패, 415 오류, 지원하지 않는 미디어 유형.나는 CURL을 정확히 설정하지 않았다고 생각했지만 여러 번 검색을 거쳐 내가 잘못한 것을 찾을 수 없었다.

다음은 일부 코드:

class URLRequest { public $url; public $headers; public $params; public $body; public $expectedFormat; public $method; public function URLRequest($aUrl, array $aHeaders, array $aParams, $aFormat = "json", $isPost = false, $aBody = "+") { $this->url = $aUrl; $this->headers = $aHeaders; $this->params = $aParams; $this->expectedFormat = $aFormat; $this->method = ($isPost ? "POST" : "GET"); $this->body = $aBody; } public function exec() { $queryStr = "?"; foreach($this->params as $key=>$val) $queryStr .= $key . "=" . $val . "&"; //trim the last '&' $queryStr = rtrim($queryStr, "&"); $url = $this->url . $queryStr; $request = curl_init(); curl_setopt($request, CURLOPT_URL, $url); curl_setopt($request, CURLOPT_HEADER, 1); curl_setopt($request, CURLOPT_HTTPHEADER, $this->headers); curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); //curl_setopt($request, CURLOPT_SSL_VERIFYPEER, false); if($this->method == "POST") { curl_setopt($request, CURLOPT_POST, 1); curl_setopt($request, CURLOPT_POSTFIELDS, $this->body); //this prevents an additions code 100 from getting returned //found it in some forum - seems kind of hacky curl_setopt($request, CURLOPT_HTTPHEADER, array("Expect:")); } $response = curl_exec($request); curl_close($request); preg_match("%(?<=HTTP/[0-9]\.[0-9] )[0-9]+%", $response, $code); $resp = ""; if($this->expectedFormat == "json") { //parse response } elseif($this->expectedFormat == "xml") { //parse response } return $resp; } } $url = "http://mydomain.com/myrestcall"; $query = array( "arg_1" => "test001", "arg_2" => "test002", "arg_3" => "test003"); $headers = array( "Accept-Encoding" => "gzip", "Content-Type" => "application/json", "Accept" => "application/json", "custom_header_1" => "test011", "custom_header_2" => "test012", "custom_header_3" => "test013"); $body = array( "body_arg_1" => "test021", "body_arg_2" => array("test022", "test023"), "body_arg_3" => "test024"); $request = new URLRequest($url, $headers, $query, "json", true, $body); $response = $request->exec(); 

응답 및 응답:

HTTP/1.1 415 Unsupported Media Type Server: Apache-Coyote/1.1 X-Powered-By: Servlet 2.5; JBoss-5.0/JBossWeb-2.1 Content-Type: text/html;charset=utf-8 Content-Length: 1047 Date: Mon, 18 Jun 2012 16:30:44 GMT                                                 
                

                
                
                JBoss Web/2.1.3.GA - Error report
                

                
                













                
                

HTTP Status 415 -

type Status report

message

description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().

JBoss Web/2.1.3.GA

어떤 견해나 생각이 있습니까?

미리 감사합니다!

대답

문제 해결!문제는:

헤더 연결 배열은 cURL 에 적용되지 않습니다.

몇몇 포럼이 주변에 퍼져 연관 배열을 제목으로 하는 예시를 보여 주었다.

이러지 마!

올바른 방법 (Internet 에 분산되지만, 나는 너무 밀집되어 있어서) 헤드키 / 값을 문자열로 구성하고 CURLOPT HTPHEADER 옵션을 설정할 때 이 문자열의 표준 그룹을 전달합니다.

어쨌든

오류:

$headers = array( "Accept-Encoding" => "gzip", "Content-Type" => "application/json", "custom_header_1" => "test011", "custom_header_2" => "test012", "custom_header_3" => "test013"); 

정확한:

$headers = array( "Accept-Encoding: gzip", "Content-Type: application/json", "custom_header_1: test011", "custom_header_2: test012", "custom_header_3: test013"); 

나는 그들이 나처럼 시간을 낭비하는 시간을 낭비하지 않도록 다른 몇몇 고귀한 바보에게 유용하기를 바란다.

만약 내가 추측하지 않으면, 나는 같은 규칙을 설정해도 POST body 키 / 값을 적용할 수 있다.

이것이 바로 왜 @drew010 은 httpubuildu query () 또는 jsonu encode () 에 대한 메시지를 문자열화하는 것도 좋은 생각이다.

여러분의 귀중한 의견에 감사드립니다.

여러분의 시간과 고려에 감사드립니다.

마지막으로 http 흐름을 통해 (Wirreshark 캡처)의 병행으로 이 문제를 비교적 발견했다.

감사합니다.

Comments