PHP code example of office360 / weather-crawler

1. Go to this page and download the library: Download office360/weather-crawler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

office360 / weather-crawler example snippets




eather\WeatherCrawler;

// 创建实例
$weatherCrawler = WeatherCrawler::create();

try {
    // 通过城市名称获取当前基础天气
    $basicWeather = $weatherCrawler->forCity('北京')
                                  ->getWeather('basic');
    print_r($basicWeather);
} catch (Exception $e) {
    echo '错误: ' . $e->getMessage();
}



eather\WeatherCrawler;

// 创建实例
$weatherCrawler = WeatherCrawler::create();

try {
    // 1. 首先通过城市名称获取城市ID
    $cityCode = $weatherCrawler->getCityCodeByName('北京');
    echo "城市代码: {$cityCode}\n";
    
    // 2. 然后使用城市ID获取当前天气数据
    $currentWeather = $weatherCrawler->getCurrentWeather($cityCode);
    print_r($currentWeather);
} catch (Exception $e) {
    echo '错误: ' . $e->getMessage();
}



eather\WeatherCrawler;

// 创建实例
$weatherCrawler = WeatherCrawler::create();

try {
    // 使用流式接口通过经纬度获取当前基础天气(北京的经纬度)
    $basicWeather = $weatherCrawler->forCoordinates(39.9042, 116.4074)
                                   ->getWeather('basic');
    print_r($basicWeather);
} catch (Exception $e) {
    echo '错误: ' . $e->getMessage();
}



eather\WeatherCrawler;

// 创建实例
$weatherCrawler = WeatherCrawler::create();

try {
    // 使用流式接口通过IP地址获取当前基础天气(北京的IP地址)
    $basicWeather = $weatherCrawler->forIp('114.247.50.2')
                                   ->getWeather('basic');
    print_r($basicWeather);
} catch (Exception $e) {
    echo '错误: ' . $e->getMessage();
}



eather\WeatherCrawler;

// 创建实例
$weatherCrawler = WeatherCrawler::create();

// 根据访客所在城市获取当前基础天气数据
// forCurrentLocation() 方法会自动获取访客IP并定位城市

try {
    $basicWeather = $weatherCrawler->forCurrentLocation()
                                   ->getWeather('basic');
    print_r($basicWeather);
} catch (Exception $e) {
    echo '错误: ' . $e->getMessage();
}



eather\WeatherCrawler;

// 创建实例
$weatherCrawler = WeatherCrawler::create();

// 已知城市代码的情况下,可以直接使用城市代码获取天气数据

try {
    // 使用流式接口获取7日天气数据(北京)
    $sevenDayWeather = $weatherCrawler->forCityCode('101010100')
                                     ->getWeather('7day');
    print_r($sevenDayWeather);
} catch (Exception $e) {
    echo '错误: ' . $e->getMessage();
}



eather\WeatherCrawler;

// 创建实例
$weatherCrawler = WeatherCrawler::create();

// 通过城市名称获取天气数据

try {
    // 使用流式接口获取逐小时天气数据
    $hourlyWeather = $weatherCrawler->forCity('上海')
                                    ->getWeather('hourly');
    print_r($hourlyWeather);
} catch (Exception $e) {
    echo '错误: ' . $e->getMessage();
}



eather\WeatherCrawler;

// 创建实例
$weatherCrawler = WeatherCrawler::create();

// 手动指定IP地址获取天气数据(适用于代理环境或需要模拟不同地区的情况)

try {
    // 使用流式接口通过指定IP获取当前天气(模拟北京IP)
    $currentWeather = $weatherCrawler->forIp('114.247.50.2')
                                     ->getWeather('basic');
    print_r($currentWeather);
} catch (Exception $e) {
    echo '错误: ' . $e->getMessage();
}



eather\WeatherCrawler;

// 创建实例
$weatherCrawler = WeatherCrawler::create();

// 获取综合天气数据

try {
    // 使用流式接口获取综合天气数据(广州)
    $comprehensiveWeather = $weatherCrawler->forCity('广州')
                                         ->getWeather('comprehensive');
    print_r($comprehensiveWeather);
} catch (Exception $e) {
    echo '错误: ' . $e->getMessage();
}

$weatherCrawler = WeatherCrawler::create();

[
    'ip' => string, // 访客IP地址
    'province' => string, // 省份
    'city' => string, // 城市
    'district' => string, // 区域
    'cityCode' => string // 城市代码
]

[
    'cityCode' => string, // 城市代码
    'cityName' => string, // 城市名称
    'temperature' => string, // 当前温度
    'weather' => string, // 天气状况
    'windDirection' => string, // 风向
    'windPower' => string, // 风力
    'humidity' => string, // 湿度
    'time' => string, // 更新时间
    'aqi' => string, // 空气质量指数
    'airQuality' => [ // 空气质量详情
        'aqi' => string,
        'quality' => string,
        'pm25' => string,
        'pm10' => string,
        'o3' => string,
        'no2' => string,
        'so2' => string,
        'co' => string
    ],
    'weatherIndex' => [ // 天气指数
        [
            'name' => string, // 指数名称
            'value' => string, // 指数值
            'level' => string, // 指数等级
            'desc' => string // 指数描述
        ],
        // ...
    ]
]

[
    [
        'date' => string, // 日期
        'day' => string, // 星期
        'weather' => string, // 天气状况
        'tempMax' => string, // 最高温度
        'tempMin' => string, // 最低温度
        'windDay' => string, // 白天风向
        'windNight' => string, // 夜间风向
        'windPowerDay' => string, // 白天风力
        'windPowerNight' => string, // 夜间风力
        'humidityDay' => string, // 白天湿度
        'humidityNight' => string // 夜间湿度
    ],
    // ... 共7天
]

[
    [
        'date' => string, // 日期
        'day' => string, // 星期
        'weather' => string, // 天气状况
        'tempMax' => string, // 最高温度
        'tempMin' => string, // 最低温度
        'windDay' => string, // 白天风向
        'windNight' => string, // 夜间风向
        'windPowerDay' => string, // 白天风力
        'windPowerNight' => string, // 夜间风力
        'humidityDay' => string, // 白天湿度
        'humidityNight' => string // 夜间湿度
    ],
    // ... 共15天
]

[
    [
        'time' => string, // 时间
        'temperature' => string, // 温度
        'weather' => string, // 天气状况
        'windDirection' => string, // 风向
        'windPower' => string, // 风力
        'humidity' => string // 湿度
    ],
    // ... 根据limit参数返回指定条数
]

[
    'currentWeather' => array, // 当前天气数据,同getCurrentWeather返回值
    'fifteenDayWeather' => array, // 15天天气预报数据,同get7DayWeather返回值格式
    'hourlyWeather' => array // 逐小时天气预报数据,同getHourlyWeather返回值格式
]