求解:为什么我的内容不显示,只有一个白框???程序如下
$ch = curl_init();
$url = 'http://apis.baidu.com/apistore/weatherservice/citylist?cityname=北京';
$header = array(
'apikey: d574fa37feebe5aead4783dc37460112',
);
// 添加apikey到header
curl_setopt($ch, CURLOPT_HTTPHEADER , $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 执行HTTP请求
curl_setopt($ch , CURLOPT_URL , $url);
$res = curl_exec($ch);
$arr = json_decode( $res,ture );
$content = $arr['retDate']['province_cn']."\n".$arr['retDate']['district_cn']."\n".$arr['retDate'][' name_cn']."\n".$arr['retDate']['name_en'];
// $content = $arr['basic']['city']."\n".$arr['HeWeather data service 3.0']['daily_forecast']['cond']."\n".$arr['HeWeather data service 3.0']['daily_forecast']['date']."\n".$arr['HeWeather data service 3.0']['daily_forecast']['wind'];
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
//注意模板中的中括号 不能少 也不能多
$fromUser = $postObj->ToUserName;
$toUser = $postObj->FromUserName;
$time = time();
$msgType = 'text';
echo sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
JSON返回示例 :
{
errNum: 0,
errMsg: "success",
retData: [
{
province_cn: "北京", //省
district_cn: "北京", //市
name_cn: "朝阳", //区、县
name_en: "chaoyang", //城市拼音
area_id: "101010300" //城市代码
},
{
province_cn: "辽宁",
district_cn: "朝阳",
name_cn: "朝阳",
name_en: "chaoyang",
area_id: "101071201"
},
{
province_cn: "辽宁",
district_cn: "朝阳",
name_cn: "凌源",
name_en: "lingyuan",
area_id: "101071203"
},
{
province_cn: "辽宁",
district_cn: "朝阳",
name_cn: "喀左",
name_en: "kazuo",
area_id: "101071204"
},
{
province_cn: "辽宁",
district_cn: "朝阳",
name_cn: "北票",
name_en: "beipiao",
area_id: "101071205"
},
{
province_cn: "辽宁",
district_cn: "朝阳",
name_cn: "建平县",
name_en: "jianpingxian",
area_id: "101071207"
}
]
}
不是二维数组,你可以把$arr打印出来看一下是个几维数组
城市名称cityname要用urlencode()进行编码吧.