PHP——做服务
xml的写法和特点
<?xml version='1.0' encoding='utf-8'?>
<Info>
<code>c001</code>
<name>张三</name>
</Info>
1.标记不一样
2.标签名可以自定义
3.大小写敏感
4.开始和结束必须要写全
5.标签名也可以有属性
<?php //如何创建JSON $attr = array("one"=>123,"two"=>456); $json = json_encode($attr); //解析JSON $attrjson = json_decode($json); print_r($attrjson); //创建XML $str = "<?xml version='1.0' ?>"; $str = $str."<Info>"; $str = $str."<code>c001</code>"; $str = $str."<name>张三</name>"; $str = $str."</Info>"; file_put_contents("aa.txt",$str); //解析XML $xml = file_get_contents("aa.txt"); $attrxml = simplexml_load_string($xml); print_r($attrxml); $code = $_GET["code"]; include("mydbda.php"); $db = new mydbda(); $sql = "select * from Info where Code = '{$code}'"; $str = $db->Select($sql,"CX","mydb"); $lie = explode("^",$str); $str = "<?xml version='1.0' ?>"; $str .=<<<XX <Info> <code>{$lie[0]}</code> <name>{$lie[1]}</name> </Info> XX; $str = $str."<Info>"; $str = $str."<code>{$lie[0]}</code>"; $str = $str."<name>{$lie[1]}</name>"; $str = $str."</Info>"; echo $str; //以下使用的是md5签名方式,并不是简单签名 header("Content-Type:text/html;charset=UTF-8"); date_default_timezone_set("PRC"); $showapi_appid = '4711'; //替换此值 $showapi_sign = '6c942094705c4a469d01114136e608e7'; //替换此值。 $showapi_timestamp = date('YmdHis'); $paramArr = array( 'showapi_appid'=> $showapi_appid, 'areaid' => '' , 'area' => '淄博' , 'needMoreDay' => '' , 'needIndex' => '' , 'needHourData' => '' , 'need3HourForcast' => '' , 'needAlarm' => '' , 'showapi_timestamp' => $showapi_timestamp // other parameter ); function createSign ($paramArr) { global $showapi_sign; $sign = ""; ksort($paramArr); foreach ($paramArr as $key => $val) { if ($key != '' && $val != '') { $sign .= $key.$val; } } $sign.=$showapi_sign; $sign = strtoupper(md5($sign)); return $sign; } function createStrParam ($paramArr) { $strParam = ''; foreach ($paramArr as $key => $val) { if ($key != '' && $val != '') { $strParam .= $key.'='.urlencode($val).'&'; } } return $strParam; } $sign = createSign($paramArr); $strParam = createStrParam($paramArr); $strParam .= 'showapi_sign='.$sign; $url = 'http://route.showapi.com/9-2?'.$strParam; $result = file_get_contents($url); $result = json_decode($result); $attr = object_array($result); //echo $attr['showapi_res_body']['f1']['day_air_temperature']; echo "今天的温度是:{$attr['showapi_res_body']['f1']['day_air_temperature']}--{$attr['showapi_res_body']['f1']['night_air_temperature']}"; function object_array($array) { if(is_object($array)) { $array = (array)$array; } if(is_array($array)) { foreach($array as $key=>$value) { $array[$key] = object_array($value); } } return $array; } ?>
<?php header("Content-Type:text/html;charset=UTF-8"); $str = file_get_contents("http://f.apiplus.cn/ssq-10.json"); $result = json_decode($str); print_r($result); ?>