0

I've have created a PHP web service method

public function import_external_xml($importXml)

I want to allow a client to upload xml via my web service method. My web service is not on the same domain as the client. The client has a webpage with a button where he want to write some javascript/jQuery to upload the xml via my web service method.

How can he do this?

Web service method in server.php:

public function import_external_xml($importXml)
{ 
    echo 'import_external_xml';
    exit;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3552264
  • 147
  • 1
  • 2
  • 12

2 Answers2

0

I did the same thing using Ruby on Rails. But was sending data that was not in xml format. One important thing as you said that your webservice is not on the same domain, you will have to deal with CORS. You can get idea about CORS here https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

You can refer this Post JQuery JSON Calls To PHP WebService Always Runs "Error" Callback

function callPhpAPI(){
  var dataa = {"your_data":youdata};

  $.ajax({
            url : yourwebserviceurl,
            type: 'POST',
            data : JSON.stringify(dataa),
            contentType : "application/json",
            success:function(data)
            {
                if(data){ 
                  alert(data);   
                  //console.log("Data from Server"+JSON.stringify(data));
                }
                else{
                  console.log("Data is empty");
                } 
            },
            error: function(xhr) {
                alert('Error!  Status = ' + xhr.status + " Message = " + xhr.statusText);
                //console.log('Error!  Status = ' + xhr.status + " Message = " +  xhr.statusText);
            }
        });
});
Community
  • 1
  • 1
Sachin Kadam
  • 265
  • 2
  • 12
  • Hi Sachin, Thanks for your reply. But how can he the import_external_xml method? – user3552264 Nov 25 '14 at 08:53
  • I did not understand your question, but i think you are trying to ask how to call the php function import_external_xml, thats very well explained in the link http://stackoverflow.com/questions/20685949/jquery-json-calls-to-php-webservice-always-runs-error-callback?rq=1 – Sachin Kadam Nov 25 '14 at 09:00
  • @SachinKadam he is trying to pass xml via ajax to his php function – Arunprasanth K V Nov 25 '14 at 09:08
  • Yes i got that, my reply was for "But how can he the import_external_xml method?" above. – Sachin Kadam Nov 25 '14 at 09:10
  • Thanks arun. I'm sorry for my bad english. But yes, the client want to to send XML via my Webservice. – user3552264 Nov 25 '14 at 09:12
  • @SachinKadam : k ,i think you are confusing with this qusn,so i just try help u to understand what actually he wants – Arunprasanth K V Nov 25 '14 at 09:14
0

You can call your php function like this using ajax

xmlDocument = $("<wrap/>").append(xmlDocument).html();
xmlDocument = encodeURIComponent(xmlDocument);

then ajax

jQuery.ajax({
    type: "POST",
    url: 'your_functions_address.php',
       processData     :   false,
   contentType     :   'text/xml',
     data            :   xmlDocument,

    success: function (obj, textstatus) {
                  if( !('error' in obj) ) {
                      yourVariable = obj.result;
                  }
                  else {
                      console.log(obj.error);
                  }
            }
});

i think you want pass xml via this ajax post , then you can refer this discussion

post xml

also check this

Community
  • 1
  • 1
Arunprasanth K V
  • 20,733
  • 8
  • 41
  • 71