PHP cURL,POST JSON
发布时间:2020-12-13 17:36:18 所属栏目:PHP教程 来源:网络整理
导读:我需要POST以下 JSON代码,但由于某种原因,它不起作用.以下是我有的代码. $fieldString = "395609399";//the curl request processorfunction processCurlJsonrequest($URL,$fieldString) { //Initiate cURL request and send back the result $ch = curl_ini
|
我需要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,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);
}
问题的一点是:
curl_setopt($ch,json_encode("{categoryId: $fieldString}"));
CURLOPT_POSTFIELDS将接受参数数组或URL编码的参数串: curl_setopt($ch,array('json'=>json_encode($stuff)));
curl_setopt($ch,'json='.urlencode(json_encode($stuff)));
其中json将是POST字段的名称(即:将导致$_POST [‘json’]可访问). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
