解析CURL XML响应PHP

时间:2016-07-31 08:29:44

标签: php xml curl

请告诉我如何获得关键字段名称="联系人标签(#)" 的$ String值 基本上我需要这样的东西:

$ XML->结果 - > contact-> Group_Tag("序列和标签") - >字段("联系人标签(原始)" )

这是我得到的结果

enter image description here

我的PHP代码:

<?php
$data = <<<STRING
<search>
    <equation>
    <field>E-Mail</field>
    <op>e</op>
    <value>anast@gmail.com</value>
    </equation>
</search>
STRING;

$data = urlencode(urlencode($data));

$appid = "xxxxxxxxxxxxxxxxxx";
$key = "xxxxxxxxxxxxxxxxxx";

$reqType = "search";
$postargs = "appid=".$appid."&key=".$key."&reqType=".$reqType."&data=".$data;
$request = "https://api.moon-ray.com/cdata.php";

$session = curl_init($request);
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $postargs);
curl_setopt ($session, CURLOPT_HEADER, false);
curl_setopt ($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
header("Content-Type: text/xml");
$decoded = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $response);
echo $decoded;

1 个答案:

答案 0 :(得分:0)

你可以试试这个

$string = '<result>
<contact id="90676">
<Group_Tag name="Sequences and Tags">
    <field name="Contact Tags (Raw)">
        test test
    </field>
</Group_Tag>
</contact>
</result>';

$xml = simplexml_load_string($string);

if($xml->contact->Group_Tag['name'] == 'Sequences and Tags'){
    if($xml->contact->Group_Tag->field['name'] == 'Contact Tags (Raw)'){
        echo $xml->contact->Group_Tag->field;
    }
}

更新

<?php
    $xml=simplexml_load_file("http://i.shn-host.ru/json/srch.php") or die("Error: Cannot create object");

    foreach ($xml->contact->Group_Tag as $Group_Tag) {
        if($Group_Tag['name'] == 'Sequences and Tags'){
            foreach ($Group_Tag as $field) {
                if($field['name'] == 'Contact Tags (Raw)'){
                    echo $field;
                }
            }
        }
    }
?> 

结果我得到了

  

22 * / 96 / 72 / 77 / 78 / 79 / 82 / 105 / 121 / 136 / 146 / 209 / 246 / 259 / 260 / 262 / 264 / 281 / * 282