gpt4 book ai didi

PHP cURL,POST JSON

转载 作者:可可西里 更新时间:2023-10-31 22:05:30 25 4
gpt4 key购买 nike

我需要发布以下 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);
}

最佳答案

问题是:

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'] 可访问)。

关于PHP cURL,POST JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4271621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com