2010-11-24 20 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; 
} 

cURLの要求を呼び出す関数である:

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(array( "categoryId" => "5016"))); が動作していません – Michael

+3

(あなたのコメントで示されているように)明らかに問題を解決したが、解決策が何であるかを示すために投稿を更新していないのは面倒です。何らかの理由で – shanusmagnus

答えて

16

問題であるビットである:

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

CURLOPT_POSTFIELDSパラメータの配列、またはAのいずれかを受け入れます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": ""}配列([Message] =>リクエストの処理中にエラーが発生しました。 StackTrace] => [ExceptionType] =>)。投稿が適切に行われていないためだと思います。 – Michael

+0

私はphp curlで複製しようとしているhttpアナライザーからの正確な投稿要求を見ることができるように、このリンクに画像をアップロードしました。 http://img502.imageshack.us/img502/1346/analyzer.jpg – Michael

+1

httpアナライザに表示されるように、投稿フィールドは表示されません(例: "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

私はcURLを介して、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を文字列として取ります。エンコーディングは必要ありません。