1
$response = curl_exec($ch);
curl_close($ch);
dd($response);

It is of type response string.

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <ns3:GetProductListResponse xmlns:ns3="http://xxx1">
            <result>
                <status>success</status>
            </result>
            <products>
                <product>
                    <currencyAmount>900.00</currencyAmount>
                    <currencyType>1</currencyType>
                    <displayPrice>900.00</displayPrice>
                    <isDomestic>false</isDomestic>
                    <id>557830715</id>
                    <price>900.00</price>
                    <productSellerCode>TSRT7777</productSellerCode>
                    <approvalStatus>6</approvalStatus>
                    ...

To convert this data to xml I used simplexml_load_string()

$response = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($response);
dd($xml);

And the output is like this.

^ SimpleXMLElement {#435}

I'm trying to access the data in it and try this.

$status = (string)$xml->result->status;
dd($status);

Returns :

^ ""

I tried using simplexml_load_file() and got no results. My main goal is to get this data as json, but I can't do it because I can't read the values.Any help would be great. Thanks in advance.

After @Jacob Mulquin's suggestion I used:

if ($xml === false) {
        dump("b");
        foreach (libxml_get_errors() as $error) {
            dump($error->message);
        }

        dd("a");
    } else {
        dd("c");
    }

Returned : "c"

kaann.gunerr
  • 170
  • 1
  • 13

1 Answers1

1

Your sample xml is not well formed, for various reasons, but assuming that the actual $response is a well formed xml string, the following should get you what you need:

#first you need to deal with namespaces
$xml->registerXPathNamespace("ns3", "http://xxx1");

#then use xpath to select your target element
$status = $xml->xpath('//ns3:GetProductListResponse//status')[0];
echo $status;

Output should be

success
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
  • Yes, that worked. But how can I convert it all to json? – kaann.gunerr Oct 20 '22 at 14:33
  • 1
    @kaann.gunerr Well, you should probably post it as a separate question, if necessary. Second, and more importantly, why would you want to convert the xml to json in the first place? – Jack Fleeting Oct 20 '22 at 14:43
  • Because I get data from more than one marketplace and keep them as json in my database. I have now integrated with 5 marketplaces in total and 4 of them are kept as json in the database and I want to keep the data from this marketplace as json. Also, I don't think I need to open a different topic, I already state that I need it in the question. – kaann.gunerr Oct 20 '22 at 14:48
  • 1
    @kaann.gunerr I'm not sure what you're describing is the right approach, but you are obviously free to do what you want. In the meantime, I see that someone closed the question. – Jack Fleeting Oct 20 '22 at 14:50
  • Yes, but I marked it as solved anyway, it worked for me. Do you think I shouldn't keep it as json? – kaann.gunerr Oct 20 '22 at 14:52
  • 1
    @kaann.gunerr - Yes, converting xml to json frequently results in loss of data, especially when the xml is deeply nested. If at all possible, keep xml as xml (maybe even in an xml database - like basex - if necessary) and query it using standard xpath (which is far more flexible than jsonpath) like in the answer.. – Jack Fleeting Oct 20 '22 at 14:56
  • Thanks for the great answers, I'll look into them. @Jack Fleeting – kaann.gunerr Oct 20 '22 at 14:58