테스트 사이트 - 개발 중인 베타 버전입니다

curl post 전송시 header를 추가하면 파라메터가 전송이 안되고있어요 채택완료

라균 4년 전 조회 3,742

외부 API 호출 테스트 도중 발생한 문제입니다.

postman으로 호출 테스트 결과 잘 동작해서 php curl 으로 호출테스트를 하는데 필수파라메터들이 없다고 나와서 test.php를 통해 보니 $_POST 에 값이 없어서 이거저거 만지다 header 없이 보내니 $_POST에 값을 넘겨 받을수는 있었습니다.

 

하지만 외부api 호출 시 apiKey는 필수이기에 헤더는 넣어주어야 하는데

헤더를 넣으면 파라메터들이 사라지는 현상이 발생하여 도움을 요청합니다!!!

 

</p>

<pre>
function request_curl ($url, $is_post = 0, $data = array(), $custom_header = NULL)
{
 //승인 요청을 보내는 함수입니다.
 $ch = curl_init ();
 curl_setopt ($ch, CURLOPT_URL, $url);
 curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
 curl_setopt ($ch, CURLOPT_SSLVERSION, 1);
 curl_setopt ($ch, CURLOPT_POST, $is_post);
 if ($is_post) {
  curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
 }
 
 curl_setopt ($ch, CURLOPT_TIMEOUT, 300);
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
 //curl_setopt ($ch, CURLOPT_HEADER, true);
 
 if ($custom_header) {
  curl_setopt ($ch, CURLOPT_HTTPHEADER, $custom_header);
 }
 $result[0] = curl_exec ($ch);
 $result[1] = curl_errno ($ch);

  if (!curl_errno($ch)) {
    $info = curl_getinfo($ch);
//    echo 'Took ', $info['total_time'], ' seconds to send a request to ', $info['url'], "\n";
    echo json_encode($info);
    echo "
";
  }
 curl_close ($ch);
 return $result[0];
}</pre>

<p> </p>

<p>$url = G5_URL.'/test.php';</p>

<p>$params_c = [</p>

<p>  test => 'test'</p>

<p>];</p>

<p>$header[] = 'apikey: abcd....'; // <span style="color:#e74c3c;">요 헤더를 지우고 보내면 test.php에서 post data들을 볼순있습니다.</span></p>

<pre>
request_curl($url, 1, http_build_query($params_c), $header);</pre>

<p>

 

test.php 내용입니다.

post data들을 잘 가져왔는지 여부만 보여줄려고 생성되었습니다.

 

</p>

<pre>
$postData = json_encode($_POST);
var_export($_POST);
exit();</pre>

<p>

댓글을 작성하려면 로그인이 필요합니다.

답변 3개

채택된 답변
+20 포인트
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Authorization: someAuthorization',
  'x-api-key: somekey',
  'Content-Type: application/x-www-form-urlencoded'   // 이것도 추가해 보세요.
));
로그인 후 평가할 수 있습니다

답변에 대한 댓글 2개

라균
4년 전
@엑스엠엘 님 감사합니다. content-type: application/x-www-form-urlencoded 추가해서 파라메타 전송 확인되었습니다.
엑스엠엘
4년 전
매뉴얼에 의하면
'Content-Type: application/x-www-form-urlencoded'
이게 default 값인데 왜 지정해 줘야 하는지, curl은 참 심오?합니다.
하여간에 해결되었다니 다행입니다.

댓글을 작성하려면 로그인이 필요합니다.

받는쪽에서 file_get_contents('php://input')  이걸로 받아보시면 안되나요?

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

핑크빈
4년 전

CURLOPT_HEADER 옵션에 1추가해보실레요?

로그인 후 평가할 수 있습니다

답변에 대한 댓글 3개

라균
4년 전
'HTTP/1.1 200 OK Date: Wed, 23 Jun 2021 08:48:41 GMT Server: Apache/2.4.46 (Unix) OpenSSL/1.0.2u PHP/5.6.40 mod_wsgi/3.5 Python/2.7.13 mod_fastcgi/mod_fastcgi-SNAP-0910052141 mod_perl/2.0.11 Perl/v5.30.1 X-Powered-By: PHP/5.6.40 P3P: CP="ALL CURa ADMa DEVa TAIa OUR BUS IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC OTC" Set-Cookie: PHPSESSID=9e939...; path=/ Set-Cookie: 2a0d2...=Ojox; expires=Thu, 24-Jun-2021 08:48:41 GMT; Max-Age=86400; path=/ Expires: 0 Last-Modified: Wed, 23 Jun 2021 08:48:41 GMT Cache-Control: pre-check=0, post-check=0, max-age=0 Pragma: no-cache Content-Length: 0 Content-Type: text/html; charset=utf-8 '

test.php 보내니 이렇게 보여주고 있어요
엑스엠엘
4년 전
Content-Length: 0
post 방식으로 보내셨다면 0이면 안 될 텐데요...
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($data)));
를 추가해 보세요.
라균
4년 전
Content-Length: 0
이건 test.php 에서 echo로 보여준 값들의 길이인거 같아요
헤더 포함해서 json_encode 를 통한값이 [] 빈배열로 보여줄땐 Content-Length: 2
헤더 없이 json_encode 를 통한값이 {"test":"test"} Content-Length: 15

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인