php读取百度天气API Json数据
本文简单介绍如何利用百度的车联网API读到JSON数据 官方手册 http://developer.baidu.com/map/wiki/index.php?title=car/api/weather#.E8.BF.94.E5.9B.9E.E7.BB.93.E6.9E.9C 今天折腾了好几个小时才把百度API里的天气接口搞明白,说句实在的,百度也够坑的,
本文简单介绍如何利用百度的车联网API读到JSON数据
官方手册
http://developer.baidu.com/map/wiki/index.php?title=car/api/weather#.E8.BF.94.E5.9B.9E.E7.BB.93.E6.9E.9C
今天折腾了好几个小时才把百度API里的天气接口搞明白,说句实在的,百度也够坑的,我第一次试因为选的应用类型是服务器,怎么都验证不了,而且我选了sn验证白名单
第二次我直接用浏览器试了,结果成功读取到json数据,剩下再来解析....
直接上代码吧
<span style="font-size:18px;"> <title> 天气 </title> <?php require("sn_caculation.php"); //获得查询城市 if ($_POST['city']) echo $_POST['city']; $location = $_POST['city']; //经过urlcode编码 $location = urlencode($location); $output = "json"; //设置查询模式 //@param $mode //默认流浏览器模式 $mode = 0; switch ($mode){ case 0: $result = browser($output,$location); break; case 1: $result = server($output,$location); break; } function server($output,$location) { //服务端(server)测试 $ak = "xxxxxxx"; $sk = "xxxxxxx; $url = "http://api.map.baidu.com/telematics/v3/weather?ak=%s&location=%s&output=%s&sn=%s"; $querystring_arrays = array("ak" => $ak, "location" => $location, "output" => $output);</span>
//百度sn算法 $sn = caculateAKSN($ak, $sk, $url, $querystring_arrays); return sprintf($url, $ak, $location, $output, $sn); } function browser($output,$location){ //浏览器测试----- $ak = "xxxxxxxx"; $url = "http://api.map.baidu.com/telematics/v3/weather?ak=%s&location=%s&output=%s"; return sprintf($url, $ak, $location, $output); } //$template = http://api.map.baidu.com/telematics/v3/weather?location=%s&output=%s&ak=%s $weather_contents = file_get_contents($result); print_r(json_decode($weather_contents)); ?>
我从另一处提交的城市,进来之后的操作就像上面这样,这里解释几个地方,ak就是百度应用里的app key,如果是服务器类型的,secret key,浏览器类型的没有,利用百度提供的sn算法进行加密,代码如下:
<?php /** * @brief 计算SN签名算法 * @param string $ak access key * @param string $sk secret key * @param string $url url值,例如: /geosearch/nearby 不能带hostname和querstring,也不能带? * @param array $querystring_arrays 参数数组,key=>value形式。在计算签名后不能重新排序,也不能添加或者删除数据元素 * @param string $method 只能为'POST'或者'GET' */ function caculateAKSN($ak, $sk, $url, $querystring_arrays, $method = 'GET'){ if ($method === 'POST'){ ksort($querystring_arrays); } $querystring = http_build_query($querystring_arrays); return md5(urlencode($url.'?'.$querystring.$sk)); } ?>
http://developer.baidu.com/map/index.php?title=lbscloud/api/appendix
请原谅我的智商。
location可以利用urlencode进行转码,免得出错,最后拿到完整的result,利用file_get_contents就可以得到数据了,这里已数组的格式输出的。。。
初学就是蛋精姐虑....哎,就到这了,希望能帮到大家

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.
