How does PHP parse XML code?

Source: Internet
Author: User
How does PHP parse XML code? Is an SMS interface: the called code is as follows: $ res & nbsp ;=& nbsp; file_get_contents ("http: // XXXXXXX/webservice. asmx/mt? Sn = $ name & amp; Pwd = $ pwd & amp; mobile = PHP how to parse XML code?
Is an SMS interface: the called code is as follows:

$res = file_get_contents("http://XXXXXXX/webservice.asmx/mt?Sn=$name&Pwd=$pwd&mobile=$tel&content=$msg");
print_r($res);



Print_r ($ res) and get an XML code, as follows:

 



Have you seen 0 in this code? I marked it in red. If the returned value is 0, the message is sent successfully.
However, I cannot determine this:

If ($ res = "0 "){
Mysql_query ("update sms set etime = now (), wei = 'sent successfully' where sms_id = '$ sms_id '");
}



How can I determine to get 0 and update the sending status in the database?
------ Solution --------------------
Preg_match ('/ (. +) <\/Int>/S', $ res, $ r );
If ($ r [1] = "0 "){
Mysql_query ("update sms set etime = now (), wei = 'sent successfully' where sms_id = '$ sms_id '");
}
------ Solution --------------------
The returned xml is a string (and very short). you need to judge whether it is a string. Therefore, you do not need to parse xml first.
Of course, it is not an error to parse it. it only consumes more resources. load a dom class first and then parse
If you want to obtain an xml node (complex), consider the latter method.

# The regular expression of 3 is the same, but it is determined from the returned string.
In this case, you can execute and do not execute the Boolean value, so I think even the return can be saved.
It is enough to directly judge whether preg_match () is True.

Reference:
I don't understand. Should I use php to parse XML first?
Can someone help me with the version?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.