首页 > 解决方案 > 如何将 Soap Env API 响应转换为 PHP 数组或 JSON 数组

问题描述

我面临将 Soap Env API 响应转换为 PHP 数组或 JSON 数组的问题。请各位大侠告诉我,我们如何才能轻松做到这一点?如何将 Soap Env API 响应转换为 PHP 数组或 JSON 数组?

我得到如下 API 的响应:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <ns6:getLocationByCriteriaResponse xmlns:ns6="http://api.atdconnect.com/atd/3_4/locations" xmlns:c="http://api.atdconnect.com/atd/3_4/common" xmlns:f="http://api.atdconnect.com/atd/3_4/fitments" xmlns:o="http://api.atdconnect.com/atd/3_4/orders" xmlns:p="http://api.atdconnect.com/atd/3_4/products">
            <ns6:locations>
                <ns6:location>
                    <ns6:locationNumber>1104464</ns6:locationNumber>
                    <ns6:locationName>CONCORDE AUTOMOBILE 1990 LTEE</ns6:locationName>
                    <ns6:phoneNumber>4507745336</ns6:phoneNumber>
                    <ns6:customerNumber>507281</ns6:customerNumber>
                    <ns6:address>
                        <c:address1>3003 RUE PICARD</c:address1>
                        <c:city>SAINT-HYACINTHE</c:city>
                        <c:state>QC</c:state>
                        <c:zipCode>J2S1H2</c:zipCode>
                    </ns6:address>
                    <ns6:servicingDC>866</ns6:servicingDC>
                </ns6:location>
                <ns6:location>
                    <ns6:locationNumber>1106909</ns6:locationNumber>
                    <ns6:locationName>CORONADO LUBE</ns6:locationName>
                    <ns6:phoneNumber>9154404222</ns6:phoneNumber>
                    <ns6:customerNumber>507281</ns6:customerNumber>
                    <ns6:address>
                        <c:address1>6508 ESCONDIDO DR</c:address1>
                        <c:city>EL PASO</c:city>
                        <c:state>TX</c:state>
                        <c:zipCode>79912</c:zipCode>
                    </ns6:address>
                    <ns6:servicingDC>615</ns6:servicingDC>
                </ns6:location>
            </ns6:locations>
        </ns6:getLocationByCriteriaResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

标签: phparraysjsonapisoap

解决方案


这是将字符串转换为数组。

    $string = '1104464CONCORDE AUTOMOBILE 19905......'; // Your Responce

    $array = explode(' ', $string);

    echo '<pre>';

    print_r($array);

Output:
Array
(
    [0] => 1104464CONCORDE
    [1] => AUTOMOBILE
    [2] => 1990
    [3] => LTEE45077453365072813003
    [4] => RUE
    [5] => PICARDSAINT-HYACINTHEQCJ2S1H28661106909CORONADO
    [6] => LUBE91544042225072816508
    [7] => ESCONDIDO
    [8] => DREL
    [9] => PASOTX79912615
)

这是将您的数组转换为 json。

$string = '1104464CONCORDE AUTOMOBILE 19905......'; // Your Responce
$array = explode(' ', $string); 
$json = json_encode($array); 
echo $json;



 Output: 
    [
    "1104464CONCORDE",
    "AUTOMOBILE",
    "1990",
    "LTEE45077453365072813003",
    "RUE",
    "PICARDSAINT-HYACINTHEQCJ2S1H28661106909CORONADO",
    "LUBE91544042225072816508",
    "ESCONDIDO",
    "DREL",
    "PASOTX79912615"
    ]

推荐阅读