dongwen9051 2016-07-31 08:29
浏览 49
已采纳

解析CURL XML响应PHP

Please tell me how can i get $String value for key field name="Contact Tags (Raw)" Basically i need something like this:

$XML->result->contact->Group_Tag("Sequences and Tags")->field("Contact Tags (Raw)")

This is result I got

enter image description here

MY PHP CODE:

<?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条回答 默认 最新

  • dongluedeng1524 2016-07-31 08:44
    关注

    You can try this

    $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;
        }
    }
    

    Updated

    <?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;
                    }
                }
            }
        }
    ?> 
    

    Result I got

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里