0

I am attempting to call a SOAP API and then load some XML information from user input.

I have a from with POST value data. However, I am unable to get around a 401 Error. I am authorized but I feel as if I am not correctly passing the data. However, I am having trouble with finding the answer.

This is what is going on with my data exchange:

Server Side:

function ddn_clientside()
{
    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => "http://api.xxxxxxxxxxx.com/general/xxx/?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(
            "Cache-Control: no-cache",
            "Postman-Token: xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx",
            "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YRxkTrFu0gW"
        ),
    ));

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

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response;
    }
}

Client Side:

jQuery(function($) {
  $("#search_form").submit(function(event) {
    event.preventDefault();
    $.ajax({
      method: "POST",
      url: ddn_clientside.url,
      data: {
        action: "ddn_clientside",
        zip_code: $("#zip_code").val(),
        distance: $("#distance").val()
      },
      success: function(data) {
        alert(data);
      },
      fail: function(error) {
        alert("There was an error communicating with the server.");
      }
    });
  });
});

What is dynamic in the form:

<input type="text" id="zip_code" class="searchZip" placeholder="Please enter your zip code..." value="<?php $name = $_POST['zip_code']; ?>" maxlength="5" name="<?php $zip = $_POST['zip_code']; ?>">
<select name="distance" id="distance" value="<?php $name = $_POST['distance']; ?>" >

The XML Loads but with this error:

401 Authorization Required


nginx
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
zqhmagic
  • 19
  • 4
  • Which bit isn't working? The AJAX request or the SOAP request? – jjok Mar 07 '18 at 14:42
  • "I am unable to get around a 401 Error. I am authorized " ...these two statements contradict each other. A 401 is a challenge to provide valid credentials. Nothing to do with the data. You never even get as far as sending the data. – ADyson Mar 07 '18 at 15:04
  • I do not see anywhere in your cURL request where you send any kind of credentials - either username/password or an authorisation token. I don't know what your API requires, but you need to find out and then send something suitable in your request. – ADyson Mar 07 '18 at 15:05
  • No. The first header is asking the server not to send a cached response. The second one relates to the piece of software called PostMan, which is not being used here, and the third one is used when you want to send multi-format data (e.g. text and attached files). Where exactly did you the idea that any of that was anything to do with authentication (not authorisation btw, it is not the same thing)? – ADyson Mar 07 '18 at 16:49
  • If you want to use Basic Authentication, there are many examples online, including https://stackoverflow.com/a/2140445/5947043 – ADyson Mar 07 '18 at 16:51
  • "usernamer:password changes in cURL to base64 string"...yes that's the documented behaviour. That's how proper Basic Auth is supposed to work. What's your point? – ADyson Mar 08 '18 at 10:10
  • Thanks for your help. – zqhmagic Mar 08 '18 at 15:38

0 Answers0