PHP - 在PHP中将XML转换为数组 - 在php中解析soap xml并将其存储在数据库中

I want to convert a soap xml response and store it in a database. Here is the XML that I have.

<ENV:Envelope xmlns:ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://example.com/soap/example">
   <ENV:Body>
      <ns1:GetCentresResponse>
         <ExampleCentre>
            <ns1:Cent>
               <ID>200</ID>
               <Name>example2</Name>
               <Code>ex2</Code>
               <Email>example2@example2.com</Email>
               <Address1>example2, example2 </Address1>
               <Address2>example2, example2 </Address2>
               <City>example2</City>
               <PostCode>111111</PostCode>
               <Telephone>1111111111</Telephone>
               <Location>11.11,-11.11</Location>
               <URL>/example2/exam2/ex2</URL>
            </ns1:Cent>
         </ExampleCentre>
      </ns1:GetCentresResponse>
   </ENV:Body>
</ENV:Envelope>

I get this soap response from the server. I want to convert this to a array and store it in database. What should I do? I know the answer might be pretty straight forward, but hey, am a newbie :D

Would really appreciate any help I get.

Thank you in anticipation.

Regards

3个回答

The best solution would be to use PHP's SoapClient class to do the call which will return you an object and then converting this object to an array, like so:

<?php
$client = new SoapClient("http://localhost/code/soap.wsdl");

// Soap call with HelloWorld() method
$something =  $client->HelloWorld(array('option1' => 'attribute1'));

// Convert object to array
$array = (array)$something;

?>

Which you can then store in the database.

dongya767979565
dongya767979565 嗨Lhunar,谢谢你的回复。 有效 :)
大约 9 年之前 回复
dtewnsdf47253
dtewnsdf47253 谢谢你,不知道这个班:)
大约 9 年之前 回复
douqiang7462
douqiang7462 您可能想要提到SOAP调用的返回值将包含一个对象,该对象具有与该XML文件匹配的成员。
大约 9 年之前 回复

If you can't use SoapClient to retrieve the SOAP response in a PHP object, then use SimpleXML to parse the soap response.

For example (where $xmlstr contains the SOAP response):

$element = new SimpleXMLElement( $xmlstr );
$centerElement = $element->Body->GetCentresResponse->ExampleCentre->Cent;

$center = array(
    $centerElement->ID,
    $centerElement->Name,
    $centerElement->Code,
    $centerElement->Email,
    $centerElement->Address1,
    $centerElement->Address2,
    $centerElement->City,
    $centerElement->PostCode,
    $centerElement->Telephone,
    $centerElement->Location,
    $centerElement->URL,
);

Now you can store $center in the database.

ds342222
ds342222 嗨,谢谢Jon的回应。 它确实有效。 随着给出的另一个答案。 非常感谢。
大约 9 年之前 回复

Parse SOAP response to an Array using following code:

You just have to call function with SOAP-XML. After that it will return a Plain XML, then you must convert it to an array using JSON encode-decode.

$plainXML = mungXML($soapXML);
$arrayResult = json_decode(json_encode(SimpleXML_Load_String($plainXML, 'SimpleXMLElement', LIBXML_NOCDATA)), true);


// FUNCTION TO MUNG THE XML SO WE DO NOT HAVE TO DEAL WITH NAMESPACE
function mungXML($xml)
{
    $obj = SimpleXML_Load_String($xml);
    if ($obj === FALSE) return $xml;

    // GET NAMESPACES, IF ANY
    $nss = $obj->getNamespaces(TRUE);
    if (empty($nss)) return $xml;

    // CHANGE ns: INTO ns_
    $nsm = array_keys($nss);
    foreach ($nsm as $key)
    {
        // A REGULAR EXPRESSION TO MUNG THE XML
        $rgx
        = '#'               // REGEX DELIMITER
        . '('               // GROUP PATTERN 1
        . '\<'              // LOCATE A LEFT WICKET
        . '/?'              // MAYBE FOLLOWED BY A SLASH
        . preg_quote($key)  // THE NAMESPACE
        . ')'               // END GROUP PATTERN
        . '('               // GROUP PATTERN 2
        . ':{1}'            // A COLON (EXACTLY ONE)
        . ')'               // END GROUP PATTERN
        . '#'               // REGEX DELIMITER
        ;
        // INSERT THE UNDERSCORE INTO THE TAG NAME
        $rep
        = '$1'          // BACKREFERENCE TO GROUP 1
        . '_'           // LITERAL UNDERSCORE IN PLACE OF GROUP 2
        ;
        // PERFORM THE REPLACEMENT
        $xml =  preg_replace($rgx, $rep, $xml);
    }
    return $xml;
}

print_r($arrayResult);
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐