2014-03-06 4 views
0

다음은 CURLOPT_HTTPHEADER이없는 예제입니다. 제대로 작동합니다.PHP Curl - 내용 유형 알기

<?php 
$URL = "http://someurl.info"; 
$data = 'x_test_request=0&x_version=3.1&x_delim_data=true&x_relay_response=false'; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $URL); 
curl_setopt($ch, CURLOPT_TIMEOUT, 1800); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
$response = curl_exec($ch); 

?> 

컬은 우리가 게시하는 데이터 유형을 자동 감지하고 문자열 길이를 자동으로 계산합니까? 또는 항상 Both를 지정해야합니까?

또는 대상의 규칙과 기대에 크게 좌우됩니까?

<?php 
$contentlen = strlen(trim($data)); 
$headersARR = array(
     "Content-type: application/x-www-form-urlencoded", 
       "Content-length: ".$contentlen, 
     ); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headersARR); 
?> 

내가 http://us1.php.net/curl_setopt를 읽을 수 있지만 궐석 한 내용 유형 매개 변수에 대한 모든 세부 사항을 발견하지 않았습니다 :

안전을 위해, 나는 보통 같은 헤더 정보를 추가 할 수 있습니다.

답변

0

Content-type에 대해 아무것도 지정하지 않은 경우 CURLOPT_POSTFIELDS을 사용하면 헤더는 기본값으로 항상 Content-type: application/x-www-form-urlencoded입니다. 또한 Content-length:을 자동으로 설정합니다. 이러한 이유로

, 우리는 POST 통해 JSON 내용을 게시 할 때 우리는 content-typeHTTPHEADER을 통해 옵션을 재정의해야합니다. XML 또는 기타 특정 유형의 콘텐츠와 유사합니다.

+0

기본적으로 내가 듣기를 기대했지만 무엇을해야만했는지 묻습니다. – coffeemonitor