SOAP Request/Parsing XML From wp_send_json_success

enter image description here

I’m developing a cURL and AJAX SOAP request to grab XML/WSDL data. The request is effectively grabbing the data I want and I am using wp_send_json_success(); to manipulate the response. The response I receive is the following:

I am unsure how to parse the data. Here is how far I’ve gone with it:

<?php /** Set Up Plugin */

function clientside() {
  if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $var = $_POST['var'];
  $var = $_POST['var'];    

  $curl = curl_init();

  curl_setopt_array($curl, array(
    CURLOPT_URL => "http://api/?wsdl=",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => array(
      "SOAPAction: "urn:xmlname"",
       "Authorization: Basic XXXxxx",
       "Cache-Control: no-cache",
       "Postman-Token: XXXxxx",
       ),
    ));

   $response = curl_exec($curl);
   $err = curl_error($curl);

   curl_close($curl);

   if ($err) {
     wp_send_json_error([ "error" => $err]);
   } else {
     wp_send_json_success([ "data" => $response ]);
   }
  }
}

/** admin-ajax set up */

?>

Ajax:

jQuery(function ($) {
    $('#search_form').submit(function (event) {

       var $this = $(this);
       // initialize form data
       var data = $this.serializeArray();
       data.push({ 
        name: "action", 
        value: "clientside",
    });

    event.preventDefault();

    $.ajax({
        // method for paremeters
        method: 'POST',
        url: clientside.url,
        data: $.param(data),
        success: function (data) {
            console.log(data);
              // make stuff happen
            });
        },
        error: function(){
            alert('An error has occured while processing XML file.');
        },
      });
   });
});

Read more here:: SOAP Request/Parsing XML From wp_send_json_success

Leave a Reply

Your email address will not be published. Required fields are marked *