批改状态:合格
老师批语:
PHP 所支持的 libcurl 库能够连接通讯各种服务器、使用各种协议。
libcurl 目前支持的协议有 http、https、ftp、gopher、telnet、dict、file、ldap, 同时也支持 HTTPS 证书、HTTP POST、HTTP PUT、 FTP 上传 、HTTP 基于表单的上传、代理、cookies、用户名+密码的认证。
PHP 版本>=7.10.5,并且安装 libcurl 包才能使用 cURL 函数。
更多请参考:https://www.php.net/manual/zh/function.curl-setopt.php
| 序号 | 预定义常量 | 描述 |
|---|---|---|
| 1 | CURLOPT_URL |
需要获取的 URL 地址,也可以在 curl_init($url) 初始化会话的时候直接传参设置。 |
| 2 | CURLOPT_HTTPGET |
true 时会设置 HTTP 的 method 为 GET,由于默认是 GET,所以只有 method 被修改时才需要这个选项。 |
| 3 | CURLOPT_HEADER |
true,启用时会将头文件的信息作为数据流输出。 |
| 4 | CURLOPT_RETURNTRANSFER |
设置为 true 或 1 表示如果请求成功只将结果返回,不自动输出任何内容。 |
curl_init() 初始化 cURL 会话
通过 curl_setopt() 设置需要的全部选项
使用 curl_exec() 来执行会话
请求接口数据:
<?php// echo phpinfo();$url = 'http://apis.juhe.cn/simpleWeather/query?';$key = '99ab468953521e0fa2a28ee905871cc5';$city = '上海';// http_build_query 将数组格式化成url$params = http_build_query(["key" => $key, "city" => $city]);// var_dump($params);$curl = curl_init(); // 返回一个curl句柄资源类型// curl_setopt()来设置各项设置curl_setopt($curl, CURLOPT_URL, $url . $params);curl_setopt($curl, CURLOPT_POST, 1);// true,启用时会将头文件的信息作为数据流输出。curl_setopt($curl, CURLOPT_HEADER, false);// 设置为true或1表示如果请求成功只将结果返回,不自动输出任何内容。如果想拿到数据转成数组,只能以数据流的方式,不能让她输出结果curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);$res = curl_exec($curl);$res = json_decode($res, true);echo '<pre>';print_r($res);// 需要关闭数据会话curl_close($curl);// echo $res["reason"] . "</br>";if (!$res["error_code"] == '') {switch ($res["error_code"]) {case '207301':echo '错误的查询城市名';break;case '207302':echo '查询不到该城市的相关信息';break;case '10012':echo '请求超过次数限制';break;case '10013':echo '测试KEY超过请求限制';break;default:echo '网络错误,请重试';break;}}?><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>天气情况</title></head><style type="text/css">table,table tr th, table tr td {border:1px solid #e6e6e6;}thead{background-color: #f2f2f2;}tr{text-align: center;height: 42px;}tr:hover{background-color: #f2f2f2;}input,textarea{box-sizing: border-box;-webkit-box-sizing: border-box;-moz-box-sizing: border-box;}</style><body><table cellspacing="0"><h2><?= $city ?>当前天气信息</h2><thead><tr><th>温度</th><th>湿度</th><th>天气情况</th><th>天气标识id</th><th>风向</th><th>风力</th><th>空气质量指数</th></tr></thead><tbody><tr><td><?= $res['result']['realtime']['temperature'] ?></td><td><?= $res['result']['realtime']['humidity'] ?></td><td><?= $res['result']['realtime']['info'] ?></td><td><?= $res['result']['realtime']['wid'] ?></td><td><?= $res['result']['realtime']['direct'] ?></td><td><?= $res['result']['realtime']['power'] ?></td><td><?= $res['result']['realtime']['aqi'] ?></td></tr></tbody></table><table><h2><?= $city ?>未来5天天气信息</h2><thead><tr><th>温度</th><th>湿度</th><th>天气情况</th><th>天气标识id</th><th>风向</th><th>风力</th><th>空气质量指数</th></tr></thead><tbody><?php foreach($res['result']['future'] as $val):?><tr><td><?= $val['temperature'] ?></td><td><?= $val['humidity'] ?></td><td><?= $val['info'] ?></td><td><?php echo '白天:' .$val['wid']['day'] .'<br>'; echo '夜间:' .$val['wid']['night'] ?></td><td><?= $val['direct'] ?></td><td><?= $val['power'] ?></td><td><?= $val['aqi'] ?></td></tr><?php endforeach?>;</tbody></table></body></html>

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号