Home  >  Article  >  Backend Development  >  How to query the old almanac in php

How to query the old almanac in php

青灯夜游
青灯夜游Original
2022-10-19 18:37:371587browse

Query method: 1. Open the old almanac interface service and obtain the calling voucher request key of the interface; 2. Call the interface API to make a request, process the data and return the result; 3. Use "$response=juheHttpRequest($apiUrl , $paramsString,1);" Get the content returned by the interface; 4. Use "$result=json_decode($response,true);" to parse the returned content into an array; 5. Print the parsed content.

How to query the old almanac in php

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer

old PHP-based Almanac interface call example

Preliminary preparation

  • ##Through

    https://www.juhe.cn/ docs/api/id/65?s=cpphpcn Self-service application for opening the interface

  • Get the calling credential request key of the interface

  • You You can see the calling credential request key of this interface in Personal Center ➡️ Data Center ➡️ My API Module

Interface Description

  • Free to use, depending on the membership level, the number of calls per day is different. Please log in to the official website of the aggregated data to view it

  • Provides old almanac query, almanac daily good and bad luck query

  • For more detailed interface usage information, please log in to the official website of Aggregation Data. This article is a simple usage example for reference only

1. Old Almanac - Calendar interface:

Request parameters

NameRequiredTypeDescriptionkey isString in the personal center-> My data, view date above the interface name is String date, format 2020-11-20
Code example

<?php
//请求的接口URL
$apiUrl = &#39;http://v.juhe.cn/laohuangli/d&#39;;

//请求参数
$params = [
    //聚合数据上申请的接口调用key
    &#39;key&#39; => &#39;聚合数据上申请的接口调用key&#39;,
    //要查询的日期
    &#39;date&#39; => &#39;要查询的日期&#39;
];
//参数数组转换成字符串
$paramsString = http_build_query($params);

//发起接口网络请求
$response = null;
try {
    $response = juheHttpRequest($apiUrl, $paramsString, 1);
} catch (Exception $e) {
    var_dump($e);
    //此处根据自己的需求进行具体的异常处理
}
if (!$response) {
    echo &#39;请求异常&#39; . PHP_EOL;
}
//接收接口返回内容
$result = json_decode($response, true);//获取接口返回内容(json字符串),并解析成数组
if (!$result) {
    echo &#39;请求异常&#39; . PHP_EOL;
}
$errorCode = $result[&#39;error_code&#39;];
if ($errorCode == 0) {
    $data = $result[&#39;result&#39;];
} else {
    echo "请求异常:{$errorCode}_{$result[&#39;reason&#39;]}" . PHP_EOL;
}
//打印接口返回结果
var_dump($result);

/**
 * 发起网络请求函数
 * @param String $url 请求的URL
 * @param bool $params  请求的参数内容
 * @param int $isPost   是否POST请求
 * @return bool|string  返回内容
 */
function juheHttpRequest($url, $params = false, $isPost = 0)
{
    $httpInfo = [];
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_USERAGENT,  &#39;Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36&#39;);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
    curl_setopt($ch, CURLOPT_TIMEOUT, 12);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    if ($isPost) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_URL, $url);
    } else {
        if ($params) {
            curl_setopt($ch, CURLOPT_URL, $url . &#39;?&#39; . $params);
        } else {
            curl_setopt($ch, CURLOPT_URL, $url);
        }
    }
    $reponse = curl_exec($ch);
    if ($reponse === FALSE) {
        // echo "cURL Error: ".curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
    curl_close($ch);
    return $reponse;
}

Return result example

array(3) {
  ["reason"]=>
  string(9) "successed"
  ["result"]=>
  array(10) {
    ["id"]=>
    string(4) "3850"
    ["yangli"]=>
    string(10) "2020-11-20"
    ["yinli"]=>
    string(26) "庚子(鼠)年十月初六"
    ["wuxing"]=>
    string(19) "炉中火 定执位"
    ["chongsha"]=>
    string(20) "冲鸡(辛酉)煞西"
    ["baiji"]=>
    string(49) "丁不剃头头必生疮 卯不穿井水泉不香"
    ["jishen"]=>
    string(44) "阴德 民日 三合 时阴 五合 鸣犬对"
    ["yi"]=>
    string(145) "祭祀 祈福 订盟 纳采 裁衣 拆卸 修造 动土 起基 安床 移徙 入宅 安香 入殓 移柩 安葬 谢土 赴任 进人口 会亲友"
    ["xiongshen"]=>
    string(6) "元武"
    ["ji"]=>
    string(13) "作灶 治病"
  }
  ["error_code"]=>
  int(0)
}

2. Old almanac - time interface:

Request parameters

NameRequiredTypeDescriptionkey is StringView in Personal Center->My Data, above the interface namedate is the String date, format 2020-11-20
Code example

<?php
//请求的接口URL
$apiUrl = &#39;http://v.juhe.cn/laohuangli/h&#39;;

//请求参数
$params = [
    //聚合数据上申请的接口调用key
    &#39;key&#39; => &#39;聚合数据上申请的接口调用key&#39;,
    //要查询的日期
    &#39;date&#39; => &#39;要查询的日期&#39;
];
//参数数组转换成字符串
$paramsString = http_build_query($params);

//发起接口网络请求
$response = null;
try {
    $response = juheHttpRequest($apiUrl, $paramsString, 1);
} catch (Exception $e) {
    var_dump($e);
    //此处根据自己的需求进行具体的异常处理
}
if (!$response) {
    echo &#39;请求异常&#39; . PHP_EOL;
}
//接收接口返回内容
$result = json_decode($response, true);
if (!$result) {
    echo &#39;请求异常&#39; . PHP_EOL;
}
$errorCode = $result[&#39;error_code&#39;];
if ($errorCode == 0) {
    $data = $result[&#39;result&#39;];
} else {
    echo "请求异常:{$errorCode}_{$result[&#39;reason&#39;]}" . PHP_EOL;
}
//打印接口返回结果
var_dump($result);

/**
 * 发起网络请求函数
 * @param String $url 请求的URL
 * @param bool $params  请求的参数内容
 * @param int $isPost   是否POST请求
 * @return bool|string  返回内容
 */
function juheHttpRequest($url, $params = false, $isPost = 0)
{
    $httpInfo = [];
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_USERAGENT,  &#39;Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36&#39;);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
    curl_setopt($ch, CURLOPT_TIMEOUT, 12);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    if ($isPost) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_URL, $url);
    } else {
        if ($params) {
            curl_setopt($ch, CURLOPT_URL, $url . &#39;?&#39; . $params);
        } else {
            curl_setopt($ch, CURLOPT_URL, $url);
        }
    }
    $reponse = curl_exec($ch);
    if ($reponse === FALSE) {
        // echo "cURL Error: ".curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
    curl_close($ch);
    return $reponse;
}

Return result example

array(3) {
  ["reason"]=>
  string(9) "successed"
  ["result"]=>
  array(12) {
    [0]=>
    array(5) {
      ["yangli"]=>
      string(10) "2020-11-20"
      ["hours"]=>
      string(3) "1-3"
      ["des"]=>
      string(54) "冲猴 煞北 时冲丙申 路空 大退 青龙 国印"
      ["yi"]=>
      string(20) "赴任 出行 修造"
      ["ji"]=>
      string(80) " 见贵 求财 嫁娶 进人口 移徙 安葬 祭祀 祈福 求嗣 斋醮 订婚"
    }
    [1]=>
    array(5) {
      ["yangli"]=>
      string(10) "2020-11-20"
      ["hours"]=>
      string(3) "3-5"
      ["des"]=>
      string(54) "冲猪 煞东 时冲己亥 朱雀 帝旺 进禄 驿马"
      ["yi"]=>
      string(41) "祭祀 祈福 斋醮 开光 赴任 出行"
      ["ji"]=>
      string(63) " 订婚 嫁娶 安床 移徙 入宅 修造 安葬 求财 见贵"
    }
    [2]=>
    array(5) {
      ["yangli"]=>
      string(10) "2020-11-20"
      ["hours"]=>
      string(3) "5-7"
      ["des"]=>
      string(54) "冲狗 煞南 时冲戊戍 不遇 天刑 日害 武曲"
      ["yi"]=>
      string(34) "作灶 祭祀 祈福 斋醮 酬神"
      ["ji"]=>
      string(28) " 赴任 出行 修造 动土"
    }
    [3]=>
    array(5) {
      ["yangli"]=>
      string(10) "2020-11-20"
      ["hours"]=>
      string(3) "7-9"
      ["des"]=>
      string(47) "冲马 煞南 时冲甲午 日刑 地兵 司命"
      ["yi"]=>
      string(0) ""
      ["ji"]=>
      string(34) "作灶 祭祀 祈福 斋醮 酬神"
    }
    [4]=>
    array(5) {
      ["yangli"]=>
      string(10) "2020-11-20"
      ["hours"]=>
      string(4) "9-11"
      ["des"]=>
      string(54) "冲羊 煞东 时冲乙未 勾陈 唐符 武曲 水星"
      ["yi"]=>
      string(0) ""
      ["ji"]=>
      string(69) "修造 盖屋 移徙 作灶 安床 入宅 开市 求嗣 订婚 嫁娶"
    }
    [5]=>
    array(5) {
      ["yangli"]=>
      string(10) "2020-11-20"
      ["hours"]=>
      string(5) "11-13"
      ["des"]=>
      string(55) " 冲鼠 煞北 时冲庚子 地兵 喜神 日禄 金匮"
      ["yi"]=>
      string(69) "祈福 求嗣 订婚 嫁娶 出行 求财 开市 交易 安床 赴任"
      ["ji"]=>
      string(13) "修造 动土"
    }
    [6]=>
    array(5) {
      ["yangli"]=>
      string(10) "2020-11-20"
      ["hours"]=>
      string(5) "13-15"
      ["des"]=>
      string(55) " 冲牛 煞西 时冲辛丑 三合 天赦 天德 宝光"
      ["yi"]=>
      string(90) "祈福 求嗣 订婚 嫁娶 出行 求财 开市 交易 安床 修造 入宅 安葬 祭祀"
      ["ji"]=>
      string(1) "-"
    }
    [7]=>
    array(5) {
      ["yangli"]=>
      string(10) "2020-11-20"
      ["hours"]=>
      string(5) "15-17"
      ["des"]=>
      string(55) " 冲虎 煞南 时冲壬寅 六戊 白虎 太阳 功曹"
      ["yi"]=>
      string(41) "入宅 修造 安葬 祭祀 斋醮 酬神"
      ["ji"]=>
      string(59) "祈福 求嗣 白虎须用 麒麟符制 否则 诸事不宜"
    }
    [8]=>
    array(5) {
      ["yangli"]=>
      string(10) "2020-11-20"
      ["hours"]=>
      string(5) "17-19"
      ["des"]=>
      string(34) " 冲兔 煞东 时冲癸卯 日破"
      ["yi"]=>
      string(1) "-"
      ["ji"]=>
      string(25) "日时相冲 诸事不宜"
    }
    [9]=>
    array(5) {
      ["yangli"]=>
      string(10) "2020-11-20"
      ["hours"]=>
      string(5) "19-21"
      ["des"]=>
      string(55) " 冲龙 煞北 时冲甲辰 天牢 地兵 六合 右弼"
      ["yi"]=>
      string(62) "祈福 求嗣 订婚 嫁娶 求财 开市 交易 安床 见贵"
      ["ji"]=>
      string(34) "赴任 修造 移徙 出行 词讼"
    }
    [10]=>
    array(5) {
      ["yangli"]=>
      string(10) "2020-11-20"
      ["hours"]=>
      string(5) "21-23"
      ["des"]=>
      string(55) " 冲蛇 煞西 时冲乙已 元武 三合 贵人 左辅"
      ["yi"]=>
      string(69) "祈福 求嗣 订婚 嫁娶 求财 开市 交易 安床 祭祀 见贵"
      ["ji"]=>
      string(27) "赴任 出行 修造 动土"
    }
    [11]=>
    array(5) {
      ["yangli"]=>
      string(10) "2020-11-20"
      ["hours"]=>
      string(4) "23-1"
      ["des"]=>
      string(54) "冲鸡 煞西 时冲丁酉 路空 明堂 进贵 木星"
      ["yi"]=>
      string(97) "求嗣 嫁娶 移徙 入宅 开市 交易 修造 安葬 祈福 订婚 赴任 出行 见贵 求财"
      ["ji"]=>
      string(46) " 朱雀须用 凤凰符制 否则 诸事不宜"
    }
  }
  ["error_code"]=>
  int(0)
}

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to query the old almanac in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn