从wp_Send_json_Success请求/解析XML

时间:2018-04-12 作者:zqhmagic

我正在开发一个cURL和ajaxsoap请求来获取XML/WSDL数据。该请求有效地获取了我想要的数据,我正在使用wp\\u send\\u json\\u success();操纵响应。我收到的答复如下:

enter image description here

我不确定如何解析数据。以下是我在这方面所做的努力:

<?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.\');
        },
      });
   });
});

1 个回复
SO网友:zqhmagic

Much easy:

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

相关推荐

我的Understrap-子主题没有head.php、footer.php

我想向我的wordpress站点添加一个小部件,但在wordpress的后端看不到它。我知道我做错了什么。模板没有发送头。php和页脚。php我确实将小部件放入了函数中。php,但我想要标题。php。在标题中。php我希望小部件显示在资源的标题上。