1. 程式人生 > >php解析xml應用例項

php解析xml應用例項

<?
error_reporting(E_ALL^E_NOTICE^E_WARNING);
include_once("inc/auth.inc.php");
//include_once("inc/conn.php");
include_once("inc/utility_all.php");
include_once("inc/header.inc.php");
ob_end_clean();
//ini_set('soap.wsdl_cache_enabled','0');//關閉快取
//$soap=new SoapClient('http://mail.china-hei.com/apiws/services/API?wsdl');

$TYPE_NEW_ID=$TYPE_ID;
$NOTIFY_MODULE_BODY.= "<ul>";
$CUR_DATE=date("Y-m-d H:i:s",time());
$CUR_DATE_U=time();
$COUNT=0;
$user_id=$_SESSION["LOGIN_USER_ID"];
//$user_id="admin";
if($user_id=="admin") {  $account="test45"; } else {
$query="select account from umail_oa_user_match as a,user as b where a.oa_uid=b.uid and b.user_id='$user_id' limit 1";
$cursor=exequery(TD::conn(),$query);
while($row=mysql_fetch_array($cursor)){
	$account=$row["account"];
	}
}
//$account="test45";
if($account!=""){  //有這個使用者
$adminname="swef";
$adminpass=md5("dfsdfsdf");//
$email=$account."@com.cn";
$password="wwwww";//非空任意
$domain="www.com.cn";
$mailbox="INBOX";
$url="http://www.mail.com/webmail/api.php?do=getMail";
//echo $url."<br>";
echo $url."<br>";
$xml = simplexml_load_file($url);
$data = json_encode($xml);
//echo $data;
//echo "<br>";
$data = json_decode(json_encode($xml),TRUE);
//echo "<pre>";
//print_r($data);
//echo "</pre>";
$re = file_get_contents($url);  
//echo $re."<br>";
$simple1=str_replace("<![CDATA[","",$re);
$simple2=str_replace("]]>","",$simple1); //< ? xml version='1.0' encoding='utf-8' ? >
$simple3=str_replace("<?xml version='1.0' encoding='utf-8'?>","",$simple2);
$simple4=str_replace("></from>","</from>",$simple3);
$simple=str_replace("></to>","</to>",$simple4);
$simple="<result>
<status>0</status>
<data>
<mailbox>
<mailcount>12</mailcount>
<unread>1</unread>
<recent>0</recent>
</mailbox>
<mail>
<mailbox>INBOX</mailbox>
<id>12</id>
<msgno>12</msgno>
<subject>AAAAA</subject>
<from>
[email protected]
</from> <to>[email protected]</to> <cc></cc> <bcc></bcc> <flagged></flagged> <size>1251</size> <date>2018-10-13 14:45:14</date> <unseen>1</unseen> <attachment>0</attachment> </mail></data></result>"; $simple=$re; $postObj = simplexml_load_string($re, 'SimpleXMLElement', LIBXML_NOCDATA); $jsonStr = json_encode($postObj); $jsonArray = json_decode($jsonStr,true); //echo "<pre>"; //print_r($jsonArray); //echo "</pre>"; //echo "<hr>"; $string = <<<XML $simple XML; echo $string."<br>"; $xml = simplexml_load_string($string); $xml = simplexml_load_string($re, 'SimpleXMLElement', LIBXML_NOCDATA);//使用此方法 print_r($xml); $arr=object_array($xml); echo "<pre>"; print_r($arr); echo "</pre>"; //echo $arr["status"]."<br>"; //echo iconv("UTF-8", "GB2312",$arr["data"]["mail"][0]["subject"]); $mailcount=$arr["data"]["mailbox"]["mailcount"];//收件箱裡的郵件計數 //echo $mailcount."<br>"; // exit; // echo "<pre>"; // print_r($arr["data"]["mail"]); // echo "</pre>"; if($mailcount>1){ // echo "<hr>"; foreach($arr["data"]["mail"] as $k=>$v){ // echo "<pre>"; // print_r($v); // echo "</pre>"; $msgno=$v["msgno"]; $unseen=$v["unseen"]; $subject=mb_convert_encoding($v["subject"],"GBK","UTF-8"); $from=mb_convert_encoding($v["from"],"GBK","UTF-8"); $date=mb_convert_encoding($v["date"],"GBK","UTF-8"); $NOTIFY_MODULE_BODY.= "<li><a href='kuaijiedenglu/gotoumail.php' target='_blank'>".$subject."&nbsp;【來自".$from."】(".$date.")</a></li>";// } /****************************/ } else if($mailcount==1){ $subject=mb_convert_encoding($arr["data"]["mail"]["subject"],"GBK","UTF-8"); $from=mb_convert_encoding($arr["data"]["mail"]["from"],"GBK","UTF-8"); $date=mb_convert_encoding($arr["data"]["mail"]["date"],"GBK","UTF-8"); $NOTIFY_MODULE_BODY.= "<li><a href='kuaijiedenglu/gotoumail.php' target='_blank'>".$subject."&nbsp;【來自".$from."】(".$date.")</a></li>"; } else { $NOTIFY_MODULE_BODY.= "<li>"._("收件箱中暫無未讀郵件")."</li>"; } ?> <!--<a href="http://mail.china-hei.com/coremail/XT3/mbox/viewmail.jsp?<?=$sid?>&mid=<?=$mid?>" target="_blank">郵件檢視</a>--> <? // $NOTIFY_MODULE_BODY.= "<li>".$CUR_DATE."</li>"; //$NOTIFY_MODULE_BODY.= "<li><a href='../a.php' target='_blank'>"._("暫無公告通知")."</a></li>"; // $NOTIFY_MODULE_BODY.= "<li>"._("暫無此類別的公告通知")."</li>"; // $NOTIFY_MODULE_BODY.= "<li>"._("暫無未讀的公告通知")."</li>"; } else {//沒有這個使用者 $NOTIFY_MODULE_BODY.= "<li>"._("Umail郵箱系統中不存在".$account."這個使用者!")."</li>"; } $NOTIFY_MODULE_BODY.= "</ul>"; echo $NOTIFY_MODULE_BODY; 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; } ?>