Blogger Information
Blog 14
fans 0
comment 0
visits 9509
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP CURL/JSON应用
Mr.Ran
Original
719 people have browsed it

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>

运行结果:

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post