0

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:

enter image description here

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.');
        },
      });
   });
});
  • what is in the console log after success? – Bridget Arrington Apr 12 '18 at 19:31
  • {success: true, data: {…}} data: "<?xml version...>"; proto: Object success: true proto: Object – zqhmagic Apr 12 '18 at 19:47
  • I think this question may help you parse your data stackoverflow.com/questions/10223865/ajax-jquery-xml-parse – Bridget Arrington Apr 13 '18 at 14:28
  • No, I believe I have to Parse the XML in the PHP. – zqhmagic Apr 16 '18 at 15:31
  • well you are sending the full response as json, if you want to manipulate in PHP you need to take your $response object and manipulate it before using wp_send_json_success([ "data" => $response ]); – Bridget Arrington Apr 17 '18 at 19:35
1

Much easy:

$xml = simplexml_load_string($response);
wp_send_json_success([ "data" => $xml ]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.