国学研究开发者API
示例代码说明
以下多种语言代码仅供参考,不建议直接复制使用,需要结合实际业务情况进行处理,为了示例方便,以下代码均使用同一个择吉日接口,密钥已做脱敏处理。
PHP代码示例(POST)
//您的密钥
$api_secret = "wD******XhOUW******pvr";
//请求择日择时接口
$gateway_host_url = "http://api.panyipan.com/api/gongju/zeshi";
//请求参数
$request_data = [
'api_key' => $api_secret,
'future' => '0',
'incident' => '3',
];
//curl
function curlSend($process_gateway, $data_arr, $type = 1) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $process_gateway);
curl_setopt($ch, CURLOPT_POST, $type);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data_arr));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FAILONERROR, true); // Set this option to handle errors
$response = curl_exec($ch);
if ($response === false) {
$error_message = curl_error($ch);
// Handle or output the error message as needed
echo "cURL error: " . $error_message;
}
curl_close($ch);
return $response;
}
function process_host($curlPost,$gateway_url) {
$response = curlSend($gateway_url,$curlPost,1);
print_r($response);
}
process_host($request_data,$gateway_host_url);
PHP代码示例(GET)
//您的密钥
$api_secret = "wD******XhOUW******pvr";
//请求择日择时接口
$gateway_host_url = "http://api.panyipan.com/api/gongju/zeshi";
//目标页面的URL
$url = $gateway_host_url. "?" . "api_key=" . $api_secret . "&future=0" . "&incident=3";
// 使用 cURL 的错误处理
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 禁用 SSL 证书验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$content = curl_exec($ch);
if (curl_errno($ch)) {
// 打印 cURL 错误信息
echo 'Curl error: ' . curl_error($ch);
} else {
// 打印返回结果
echo $content;
}
curl_close($ch);
Javascript代码示例
// 您的密钥
const apiSecret = "wD******XhOUW******pvr";
// 请求择日择时接口
const gatewayHostUrl = "http://api.panyipan.com/api/gongju/zeshi";
// 请求参数
const requestData = {
api_key: apiSecret,
future: '0',
incident: '3',
};
// fetch 函数的 JavaScript 实现
async function fetchSend(processGateway, dataObj, type = 'POST') {
try {
const response = await fetch(processGateway, {
method: type,
headers: {
'Content-Type': 'application/form-data',
},
body: new URLSearchParams(dataObj),
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return await response.text();
} catch (error) {
// Handle or output the error message as needed
console.error(`Fetch error: ${error.message}`);
return null;
}
}
// processHost 函数的 JavaScript 实现
async function processHost(curlPost, gatewayUrl) {
const response = await fetchSend(gatewayUrl, curlPost);
if (response !== null) {
console.log(response);
}
}
// 调用 processHost 函数
processHost(requestData, gatewayHostUrl);
联系我们

18611913241