PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

博客列表 > PHP CURL/JSON应用

PHP CURL/JSON应用

Mr.Ran
Mr.Ran 原创
2021年10月18日 00:12:04 666浏览

PHP CURL\JSON 应用

  • API 天气接口请求
  • curl
  • json_decode
  • json_encode

PHP 代码

  1. <?php
  2. define('JUHE_API','http://apis.juhe.cn/simpleWeather/query');
  3. define('JUHE_KEY','abc4b64ae7656b460723402175a5650b');
  4. function getTianqi(string $city,$isget=1){
  5. $data = ['key'=> JUHE_KEY,'city'=>$city];
  6. $url = JUHE_API.'?';
  7. $ch = curl_init();
  8. curl_setopt($ch,CURLOPT_URL,JUHE_API);
  9. curl_setopt($ch,CURLOPT_TIMEOUT,30);
  10. curl_setopt($ch,CURLOPT_AUTOREFERER,1);
  11. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  12. if ($isget) {
  13. foreach ($data as $k => $v) {
  14. $url .= $k.'='.$v.'&';
  15. }
  16. curl_setopt($ch,CURLOPT_URL,$url);
  17. } else {
  18. curl_setopt($ch,CURLOPT_POST,1);
  19. curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
  20. }
  21. $rs = curl_exec($ch);
  22. curl_close($ch);
  23. return json_decode($rs,true);
  24. }
  25. $arrs = getTianqi(isset($_POST['city'])?$_POST['city']:'北京',0);
  26. $errorMessage='';
  27. if ($arrs['error_code'] == 0) {
  28. $city = $arrs['result']['city'];
  29. $real = $arrs['result']['realtime'];
  30. $future = $arrs['result']['future'];
  31. $futureHTML = '<table class="table"><thead class="thead-dark"><tr><th>日期:</th><th>温度</th><th>天气情况</th><th>风向</th></tr><thead/>';
  32. foreach ($future as $f) {
  33. $futureHTML.= "<tr><td>".$f['date']."</td>";
  34. $futureHTML.= "<td>".$f['temperature']."</td>";
  35. $futureHTML.= "<td>".$f['weather']."</td>";
  36. $futureHTML.= "<td>".$f['direct']."</td></tr>";
  37. }
  38. $futureHTML .= '</table>';
  39. } else {
  40. $errorMessage = $arrs['reason'];
  41. }
  42. ?>

HTML 代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>天气预报</title>
  8. <link
  9. href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.css"
  10. rel="stylesheet"
  11. />
  12. <style>
  13. .lead label {
  14. margin-right: 10px;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <?=$errorMessage?>
  20. <div
  21. class="jumbotron"
  22. style="display:<?=!empty($errorMessage)?'none':'block'?>;"
  23. >
  24. <h1 class="display-4">当前城市天气:</h1>
  25. <p class="lead">
  26. <label>城市:<?=!empty($city)?$city:'暂无数据'?></label>
  27. <label
  28. >温度:<?=!empty($real['temperature'])?$real['temperature']:''?></label
  29. >
  30. <label>天气:<?=!empty($real['info'])?$real['info']:''?></label>
  31. <label>湿度:<?=!empty($real['humidity'])?$real['humidity']:''?></label>
  32. <label>风向:<?=!empty($real['direct'])?$real['direct']:''?></label>
  33. <label>风力:<?=!empty($real['power'])?$real['power']:''?></label>
  34. <label>空气质量:<?=!empty($real['aqi'])?$real['aqi']:''?></label>
  35. </p>
  36. <hr class="my-4" />
  37. <form action="" method="post" class="input-group">
  38. <input
  39. class="form-control"
  40. type="text"
  41. name="city"
  42. value="<?=$city?>"
  43. placeholder="请输入城市名称"
  44. />
  45. <button class="btn btn-primary input-group-append" type="submit">
  46. 查询天气
  47. </button>
  48. </form>
  49. <hr class="my-4" />
  50. <p>近5天天气情况:</p>
  51. <?=!empty($futureHTML)?$futureHTML:'暂无数据'?>
  52. </div>
  53. </body>
  54. </html>

运行结果:

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议