让ChatGPT为我们写一套自动查询武汉市天气预报并推送到webhook的程序代码
随着ChatGPT日益智能,它将能代替我们做很多很多的事情,比如,让ChatGPT为我们写一套自动查询武汉市天气预报并推送到webhook的程序代码,下面再来看看ChatGPT写的代码是否能跑起来?
代码运行结果:
完整PHP代码如下
<?php // 定义要查询的城市 $city = "武汉"; // 查询天气接口的URL // $apiUrl = "http://api.weatherapi.com/v1/current.json?key=YOUR_API_KEY&q=" . urlencode($city); $apiUrl = "http://v1.yiketianqi.com/api?unescape=1&version=v61&appid=37638000&appsecret=000000&ext=&cityid=&city=" . urlencode($city); // 发起 API 请求 $response = file_get_contents($apiUrl); if ($response) { // 解析 API 响应 $weatherData = json_decode($response, true); // 获取天气预报信息 $location = $weatherData['city']; $temp = $weatherData['tem']; $condition = $weatherData['wea']; $text = "【".$location."】当前温度".$temp."°C,天气状况".$condition.",空气质量".$weatherData['air_level'].",今日最高温".$weatherData['tem1']."°C,最低温".$weatherData['tem2']."°C,".$weatherData['win'].$weatherData['win_speed']; $postData = '{"msgtype":"text","text":{"content":"'.$text.'"}'; // webhook URL $webhookUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=99999999-1f09-448f-9442-73a077db7f00"; // 使用 cURL 发起 POST 请求 $ch = curl_init($webhookUrl); // curl_setopt($ch, CURLOPT_URL, $ch); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); curl_close($ch); // 输出结果 echo "天气预报[".$postData."]已推送到 webhook."; } else { echo "无法获取天气预报信息."; } ?>