php 获取全年节假日(二 )

该PHP代码通过HTTP请求获取全年节假日数据,然后使用json_decode解析,并将结果转换为XML格式,存储到本地文件Noworking.xml中。主要涉及网络请求、JSON解析和XML生成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php
namespace Home\Controller;
use Think\Controller;
class JiejiariController extends Controller {
        public function index(){
            $Jiejiari = file_get_contents("http://www.easybots.cn/api/holiday.php?m=202001,202002,202003,202004,202005,202006,202007,202008,202009,202010,202011,202012");
           $array =  json_decode($Jiejiari,true);
           $a=1;
           foreach ($array as $key => $value) {

                foreach ($value as $key => $values) {
                    if($a<10){
                   $b='0'.$a;   
                    }else{
                         $b=$a; 
                    }
                         $cuis[]  = intval('2020'.$b.$key);
                }
            $a++;
            
        }

        dump($this->arrayToXml($cuis));

        
}

public  function arrayToXml($arr){
    $path = dirname(__FILE__);
        $xml = "<studentcareer>\n";
        foreach ($arr as $key=>$val){
           $xml.="\t<period>\n";
            if(is_array($val)){
                $xml.="\t\t<date>".arrayToXml($val)."</date>";
            }else{
                $xml.="\t\t<date>".$val."</date>\n";
            }
        $xml.="\t</period>\n";

        }
$xml.="</studentcareer>";

$file = 'Noworking.xml';

//echo $path.'/'.$file;die;
file_put_contents($path.'/'.$file,$xml);

        //return $xml;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值