我正在检索xml:
$xml = file_get_contents($query);
echo $xml->Email;
echo $xml[0]->Email;
xml(echo $ xml)如下所示:
<GetUserInfo>
<Customer>TestUser</Customer>
<Email>[email protected]</Balance>
</GetUserInfo>
但是这两种方法都产生以下错误:
Notice: Trying to get property of non-object in test.php on line 86
Notice: Trying to get property of non-object in test.php on line 87
我如何获得电子邮件和客户的价值?
file_get_contents()
返回文件内容,而不是对象。仅preg_match
当您要坚持字符串内容时才可以使用(完全不建议使用):
preg_match('~<Email>([^<]+)</Email>~i', file_get_contents($__filePath__), $emails);
我建议使用DOMDocument
和DOMXPath
(未经测试的代码):
$XMLDoc = new DOMDocument();
$XMLDoc->load($__filePath__);
$XPath = new DOMXPath($XMLDoc);
$emails = $XPath->query('//email');
foreach ($emails as $email)
var_dump($email->nodeValue);
如果只需要第一个邮件,则可以使用另一个Xpath表达式,例如//email[1]
或/GetUserInfo/Email
。也可以将foreach替换为$email = reset($emails);
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句