次の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);
}
json_encodeに、すでにJSオブジェクトの形式の文字列ではなく、PHP配列を渡します。 – JAL
私はcurl_setopt($ ch、CURLOPT_POSTFIELDS、array(json_encode(array( "categoryId" => "5016"))))));)を試しました。 json_encode(array( "categoryId" => "5016"))); が動作していません – Michael
(あなたのコメントで示されているように)明らかに問題を解決したが、解決策が何であるかを示すために投稿を更新していないのは面倒です。何らかの理由で – shanusmagnus