欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

自己亲自做的 simplexml_load_string 的例子

shiping1 的头像

<?php
/*
 * Created on 2015-4-15
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

 $guishudi = @file_get_contents("http://life.tenpay.com/cgi-bin/mobile/MobileQueryAttribution.cgi?chgmobile=13771894818");
 $guishudi = iconv('GBK','UTF-8',$guishudi);
 $guishudi =  str_replace('encoding="gb2312"','encoding="utf-8"',$guishudi);
 //var_dump($guishudi);
 $data = simplexml_load_string($guishudi);
 //var_dump($data);

 var_dump(strval($data->city)); //这种值是对的 非要加上 strval 进行转换
 foreach($data->city as $key=>$value)
 {
     var_dump($key);
     var_dump($value);//这种值不对
 }

 //var_dump($data->city);//这种值不对
 //var_dump($data->retmsg[0]);//这种值不对
 
?>



http://life.tenpay.com/cgi-bin/mobile/MobileQueryAttribution.cgi?chgmobile=13771894818
的值为 下面的 xml 字符串


<?xml version="1.0" encoding="gb2312" ?>

<root>
<ENV_CgiName>/cgi-bin/mobile/MobileQueryAttribution.cgi</ENV_CgiName>
<ENV_ClientAgent>Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0</ENV_ClientAgent>
<ENV_ClientIp>49.73.89.100</ENV_ClientIp>
<ENV_QueryString>chgmobile=13771894818</ENV_QueryString>
<ENV_RequestMethod>GET</ENV_RequestMethod>
<ENV_referer></ENV_referer>
<chgmobile>13771894819</chgmobile>
<city>苏州 </city>
<province>江苏 </province>
<retcode>0</retcode>
<retmsg>OK</retmsg>
<supplier>移动 </supplier>
<tid></tid>
</root>



下面也是自己亲自做的 simplexml_load_string 例子
function _getGuishude($tel)
{
    $tel = strval(trim($tel));
    if(empty($tel)){
        return '';
    }
    $tel = substr($tel,0,11);
    $guishudi = @file_get_contents("http://life.tenpay.com/cgi-bin/mobile/MobileQueryAttribution.cgi?chgmobile=".$tel);
    $guishudi = iconv('GBK','UTF-8',$guishudi);
    $guishudi =  str_replace('encoding="gb2312"','encoding="utf-8"',$guishudi);
    $dataObj = simplexml_load_string($guishudi);
    
    if(strval($dataObj->retmsg)=='OK'){
        $outputstring = trim(strval($dataObj->province)).'省'.trim(strval($dataObj->city)).'市  '.strval($dataObj->supplier).' ';
    }
    else{
        $outputstring = '不合法的手机号码';
    }
    
    return $outputstring;
}

普通分类: