parse cURL response in PHP using preg_match() not working

时间:2015-06-15 14:52:51

标签: php curl preg-match

I have cURL response in PHP like this, and I want only get the <a:RefNo>A1231640000001</a:RefNo> content only. I have tried simpleXML() but I failed, I use also preg_match() but I'm having a hard time..

1 个答案:

答案 0 :(得分:1)

首先,您的XML在此行中有错误,因为您的开放代码a:Signature没有>

<a:SignatureJJqwH0QlPO9QQ/f9FT4G8q+I/zdIILLtxETLgrAZuolVCgqIdhNxuw3xgusq1HqxcGG6TZZIkwlylgI3VbWLJQ==</a:Signature>

验证它是否只是一个拼写错误,或者您的WebService是否正在发送带有此错误的响应。

接下来,避免使用正则表达式解析此级别的XML(让核心为你做;-))。这是一个使用DOMDocument来解析XML(已更正)的示例:

    $dom = new DOMDocument();
    $dom->loadXML($xml);

    ///getting the Signatute tag and Partner RefNo
    $signature = $dom->getElementsByTagNameNS('http://domain.com','Signature');
    $partner = $dom->getElementsByTagNameNS('http://domain.com','PartnerRefNo');
    print_r ($signature->item(0)->nodeValue);
    print_r ("\n\n");
    print_r ($partner->item(0)->nodeValue);

结果是:

  

JJqwH0QlPO9QQ / f9FT4G8q + I / zdIILLtxETLgrAZuolVCgqIdhNxuw3xgusq1HqxcGG6TZZIkwly​​lgI3VbWLJQ ==

     

A1231640000001

OBS :要处理SOAP WebSErvices,请更喜欢使用SoapClient。仅将cURL用作最后一个选项。