2010-11-24 11 views
10

다음 JSON 코드를 POST해야하지만 어떤 이유로 작동하지 않습니다. 아래 코드는 제가 가지고있는 코드입니다. 여기 PHP cURL, POST JSON

$fieldString = "395609399"; 
//the curl request processor 
function processCurlJsonrequest($URL, $fieldString) { //Initiate cURL request and send back the result 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type: application/json')); 
    curl_setopt($ch, CURLOPT_URL, $URL); 
    curl_setopt($ch, CURLOPT_USERAGENT, $this->_agent); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $this->_cookie_file_path); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $this->_cookie_file_path); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE); 
    if ($fieldCount) { // in case of post fields present then pass it on 
     curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode("{categoryId: $fieldString}")); 
     curl_setopt($ch, CURLOPT_POST, 1); 
    } 
    $resulta = curl_exec($ch); 
    if (curl_errno($ch)) { 
     print curl_error($ch); 
    } else { 
     curl_close($ch); 
    } 
    return $resulta; 
} 

는 컬 요청을 호출하는 기능이다

function get_cat($categoryId, $URL) { 
    $fields = array(
     "categoryId" => $categoryId 
    ); 
    $fields_string = $fields; 
    return $this->processCurlJsonrequest($URL, $fields_string); 
} 
+0

json_encode에 이미 JS 객체 형식의 문자열이 아닌 PHP 배열을 지정합니다. – JAL

+0

curl_setopt ($ ch, CURLOPT_POSTFIELDS, array (json_encode (array ( "categoryId"=> "5016"))));)를 시도했습니다. 및 json_encode (배열 ( "categoryId"=> "5016")))); 그리고 작동하지 않습니다 – Michael

+3

당신이 분명히 문제를 풀었지만 해결 방법이 무엇인지 나타 내기 위해 게시물을 업데이트하지 않은 것은 매우 귀찮습니다. 일부 이유 때문에 – shanusmagnus

답변

16

문제가 비트는 :

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode("{categoryId: $fieldString}")); 

어느 파라미터의 어레이를 받아들이 CURLOPT_POSTFIELDS 또는 URL로 인코딩 된 매개 변수 문자열 :

curl_setopt($ch, CURLOPT_POSTFIELDS, array('json'=>json_encode($stuff))); 
curl_setopt($ch, CURLOPT_POSTFIELDS, 'json='.urlencode(json_encode($stuff))); 

여기서 json은 POST 필드의 이름입니다 (예 : $_POST['json']에 액세스 할 수 있음).

+0

이 작동하지 않습니다. { "메시지": "요청을 처리하는 중 오류가 발생했습니다.", "StackTrace": "", "ExceptionType": ""} Array ([Message] => 요청을 처리하는 중 오류가 발생했습니다. [ StackTrace] => [예외 타입] =>). 나는 게시물이 제대로 만들어지지 않았기 때문이라고 생각합니다. – Michael

+0

나는 php curl로 복제하려고하는 http 분석기의 정확한 게시물 요청을 볼 수 있도록이 링크에 그림을 업로드했습니다. http://img502.imageshack.us/img502/1346/analyzer.jpg http 분석기에서 볼 수 있듯이 – Michael

+1

은 게시 필드를 표시하지 않습니다 (예 : "json") – Michael

8

는 JSON 인코딩없이 텍스트를 보내기,이 헤더 코드를 추가

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8","Accept:application/json, text/javascript, */*; q=0.01")); 
3

나는 한 컬을 통해 JSON을 보내는 문제, 문제 내가 명시 적으로 헤더의 내용 길이를 설정되지 않았 음을이었다.

그래서 헤더가 있어야한다 : 초기 예와

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($json))); 
6

이 같아야 작업 코드 :

//the curl request processor 
function processCurlJsonrequest($URL, $fieldString) { //Initiate cURL request and send back the result 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
    curl_setopt($ch, CURLOPT_URL, $URL); 
    curl_setopt($ch, CURLOPT_USERAGENT, $this->_agent); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $this->_cookie_file_path); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $this->_cookie_file_path); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array("myJsonData" => "test"))); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    $resulta = curl_exec($ch); 
    if (curl_errno($ch)) { 
     print curl_error($ch); 
    } else { 
     curl_close($ch); 
    } 
    return $resulta; 
} 
2

정말 아주 간단, 확인 당신은 여분의 내용을 설정 한 json에 대한 -Type 헤더 세트를 설정 한 다음 CURLOPT_POSTFIELDS는 json을 문자열로 취할 수 있습니다. 인코딩이 필요하지 않습니다.