【问题标题】:php read xml content from soap with getElementsByTagNameNSphp使用getElementsByTagNameNS从soap读取xml内容
【发布时间】:2026-02-16 09:55:01
【问题描述】:

这是我拥有的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>
  <notifications xmlns="http://soap.sforce.com/2005/09/outbound">
   <OrganizationId>00Dp00000008kXDEAY</OrganizationId>
   <ActionId>04k28000000L3shAAC</ActionId>
   <SessionId xsi:nil="true"/>
   <EnterpriseUrl>https://232.salesforce.com/services/Soap/c/36.0/00Dp00000008kXD</EnterpriseUrl>
   <PartnerUrl>https://232.salesforce.com/services/Soap/u/36.0/00Dp00000008kXD</PartnerUrl>
   <Notification>
    <Id>034l3004D4247lVAAS</Id>
    <sObject xsi:type="sf:SMS_Activity__c" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
     <sf:Id>a0Ip0000001WxvaEAC</sf:Id>
     <sf:Brand__c>ABC</sf:Brand__c>
     <sf:Content__c>halo test</sf:Content__c>
     <sf:Mobile__c>628145783535</sf:Mobile__c>
    </sObject>
   </Notification>
  </notifications>
 </soapenv:Body>
</soapenv:Envelope>

请帮助我如何读取 (brand,content__C,mobile__c) 的价值 用php代码?

我无法从我尝试下面的代码中获得 Mobile__c

 $notif=simplexml_load_string($xml);
      $Mobile=$notif->sObject-
      >children('urn:sobject.enterprise.soap.sforce.com')-
      >Mobile__c.PHP_EOL;
      $qd="";
      $qd="insert into incoming(phone) values('$Mobile')";
      mysql_query($qd);

【问题讨论】:

    标签: php xml soap domdocument


    【解决方案1】:

    试试这个希望对你有帮助..

    Try this code snippet here

    <?php
    
    ini_set('display_errors', 1);
    $string='<?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <soapenv:Body>
      <notifications xmlns="http://soap.sforce.com/2005/09/outbound">
       <OrganizationId>00Dp00000008kXDEAY</OrganizationId>
       <ActionId>04k28000000L3shAAC</ActionId>
       <SessionId xsi:nil="true"/>
       <EnterpriseUrl>https://232.salesforce.com/services/Soap/c/36.0/00Dp00000008kXD</EnterpriseUrl>
       <PartnerUrl>https://232.salesforce.com/services/Soap/u/36.0/00Dp00000008kXD</PartnerUrl>
       <Notification>
        <Id>034l3004D4247lVAAS</Id>
        <sObject xsi:type="sf:SMS_Activity__c" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
         <sf:Id>a0Ip0000001WxvaEAC</sf:Id>
         <sf:Brand__c>ABC</sf:Brand__c>
         <sf:Content__c>halo test 1</sf:Content__c>
         <sf:Mobile__c>6281457835351</sf:Mobile__c>
        </sObject>
       </Notification> <Notification>
        <Id>034l3004D4247lVAAS</Id>
        <sObject xsi:type="sf:SMS_Activity__c" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
         <sf:Id>a0Ip0000001WxvaEAC</sf:Id>
         <sf:Brand__c>ABC2</sf:Brand__c>
         <sf:Content__c>halo test 2</sf:Content__c>
         <sf:Mobile__c>6281457835352</sf:Mobile__c>
        </sObject>
       </Notification> <Notification>
        <Id>034l3004D4247lVAAS</Id>
        <sObject xsi:type="sf:SMS_Activity__c" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
         <sf:Id>a0Ip0000001WxvaEAC</sf:Id>
         <sf:Brand__c>ABC3</sf:Brand__c>
         <sf:Content__c>halo test 3</sf:Content__c>
         <sf:Mobile__c>6281457835353</sf:Mobile__c>
        </sObject>
       </Notification>
      </notifications>
     </soapenv:Body>
    </soapenv:Envelope>';
    
    $requiredData=array();
    $result= new DOMDocument();
    $result->loadXML($string);
    foreach(array(
        "Brand__c",
        "Content__c",
        "Mobile__c"
    ) as $key)
    {
        foreach($result->getElementsByTagNameNS("urn:sobject.enterprise.soap.sforce.com",$key) as $element)
        {
            if($element instanceof DOMElement)
            {   
               $requiredData[$element->tagName][] =$element->textContent;
            }
        }
    }
    print_r($requiredData);
    

    【讨论】:

    • 谢谢 .. 但是如何直接访问 Mobile__C,我对使用 $expectedOutput[$child->tagName] 感到困惑,我尝试用 $expectedOutput[sf:Mobile__c] 替换但出现错误。对不起,我是 php xml 的新手。非常感谢
    • @Def 谢谢朋友... :) 只是删除你上面的评论听起来不好...再次感谢
    • sahil 先生,抱歉再次打扰,如果我有多个如下数据,如何通过循环访问?
    • @Def 你能分享那个示例 XML 吗?
    • 04lp0000004D7nEAAS1232ABC 管理员测试124125 a0Ip0000001WxyLEASABF 管理员测试123通知>
    【解决方案2】:

    sahil 先生,抱歉再次打扰,如果我有多个如下数据,如何通过循环访问它?

    <Notification>
        <Id>034l3004D4247lVAAS</Id>
        <sObject xsi:type="sf:SMS_Activity__c" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
         <sf:Id>a0Ip0000001WxvaEAC</sf:Id>
         <sf:Brand__c>ABC</sf:Brand__c>
         <sf:Content__c>halo test 1</sf:Content__c>
         <sf:Mobile__c>6281457835351</sf:Mobile__c>
        </sObject>
       </Notification> <Notification>
        <Id>034l3004D4247lVAAS</Id>
        <sObject xsi:type="sf:SMS_Activity__c" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
         <sf:Id>a0Ip0000001WxvaEAC</sf:Id>
         <sf:Brand__c>ABC2</sf:Brand__c>
         <sf:Content__c>halo test 2</sf:Content__c>
         <sf:Mobile__c>6281457835352</sf:Mobile__c>
        </sObject>
       </Notification> <Notification>
        <Id>034l3004D4247lVAAS</Id>
        <sObject xsi:type="sf:SMS_Activity__c" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
         <sf:Id>a0Ip0000001WxvaEAC</sf:Id>
         <sf:Brand__c>ABC3</sf:Brand__c>
         <sf:Content__c>halo test 3</sf:Content__c>
         <sf:Mobile__c>6281457835353</sf:Mobile__c>
        </sObject>
       </Notification>
    

    【讨论】: