|
|

楼主 |
发表于 2022-3-11 16:19:32
|
显示全部楼层
下载文件
- //小时降水
- $dirname = 'hourjonswe';
- $url_base = 'http://data.cma.cn/weatherGis/web/bmd/VisDataDef/getVisData?datacode=SURF_R1&d_datetime=';
- $data_start = '2021-01-01';
- $dirpath = __DIR__ . '/' . $dirname;
- $dirpath_ = $dirpath . '_';
- $ch = curl_init(); //初始化
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0'); //"User-Agent:"的值
- curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate, br'); //"Accept-Encoding:"的值
- //curl_setopt($ch, CURLOPT_REFERER, ''); //"Referer:"的值
- //HTTP 头字段的数组
- curl_setopt($ch, CURLOPT_HTTPHEADER,
- array(
- 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
- 'Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
- 'DNT: 1',
- 'Upgrade-Insecure-Requests: 1',
- 'Connection: keep-alive',
- 'Upgrade-Insecure-Requests: 1',
- 'Connection: keep-alive',
- 'Sec-Fetch-Dest: document',
- 'Sec-Fetch-Mode: navigate',
- 'Sec-Fetch-Site: none',
- 'Sec-Fetch-User: ?1',
- 'Pragma: no-cache',
- 'Cache-Control: no-cache',
- )
- );
- //curl_setopt($ch, CURLOPT_NOBODY, 1); //不输出 BODY
- //curl_setopt($ch, CURLOPT_HTTPGET, 1); //强制 GET
- //curl_setopt($ch, CURLOPT_POST, 1); //发送 POST 请求
- //curl_setopt($ch, CURLOPT_PUT, 1); //发送文件
- //curl_setopt($ch, CURLOPT_INFILE, 1); //
- //curl_setopt($ch, CURLOPT_INFILESIZE , 1); //
- //curl_setopt($ch, CURLOPT_PORT, 80); //指定端口
- //curl_setopt($ch, CURLOPT_COOKIE, ''); //设定 Cookie,用分号分隔,分号后带一个空格(例如,"fruit=apple; colour=red")
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //以字符串返回
- curl_setopt($ch, CURLOPT_HEADER, 0);
- $date = date_create($data_start);
- while (date_timestamp_get($date) < time()) {
- echo date_format($date,'Y-m-d') . ': ';
- $url = $url_base . date_format($date,'Ymd');
- curl_setopt($ch, CURLOPT_URL, $url); //请求地址
- //执行并获取HTML文档内容
- $json = curl_exec($ch);
- //echo $output;
- $object = json_decode($json);
- $arr = $object->data;
- $i = 0;
- foreach ($arr as $o) {
- //echo $o->fileURL,"\n";
-
- $filepath = $dirpath.'/'. $o->v_SHIJIAN . '.png';
-
- if (!file_exists($filepath)) {
- echo $i . ' ';
- curl_setopt($ch, CURLOPT_URL, $o->fileURL); //请求地址
- $img = curl_exec($ch);
- file_put_contents($filepath, $img);
- if (getimagesize($filepath) === false) {
- $date = date_create();
- }
- }
- $i += 1;
- }
-
- echo "\n";
- date_add($date,date_interval_create_from_date_string('1 day'));
- }
- //释放curl句柄
- curl_close($ch);
复制代码
|
|