1
votes

I might be not the only one posting this issue on Stackoverflow but unfortunately none of them solved my issue.

I've developed an opencast mobile app connected to server through SOAP API, when testing the API via Postman it returns a correct result however when running the app the API returns HTML instead of XML. It's not executing the php file and triggering the functions.

  -(void)loginRequest{
      @try {
          NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
          NSString *password = [NSString stringWithFormat:@"%@", [self md5:SOAP_PASSWORD]];
          [params setObject:SOAP_USER_NAME forKey:@"apiKey"];
          [params setObject:password forKey:@"apiPassword"];
          NSError *error = nil;
          NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:&error];
          NSString *jsonString;
          if(!jsonData){
  //            NSLog(@"Got an error: %@", error);
          }
          else
              jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
          NSString *parameters = [NSString stringWithFormat:@"<attributes xsi:type=\"xsd:string\">%@</attributes>", jsonString];
          NSString *envelope = [self createEnvelope:@"apiLogin" forNamespace:@"urn:Opencart" forParameters:parameters];
          NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString: URL]];
          [request setHTTPMethod:@"POST"];
          NSMutableDictionary *defaultHeaders = [[NSMutableDictionary alloc] init];
          [defaultHeaders setValue:@"gzip" forKey:@"Accept-Encoding"];
          [defaultHeaders setValue:@"en, en-us;q=0.8" forKey:@"Accept-Language"];
          [defaultHeaders setValue:@"text/xml; charset=utf-8" forKey:@"Content-Type"];
          [defaultHeaders setValue:@"urn:Opencart_Api_Model_Server_HandlerAction" forKey:@"SOAPAction"];
          [defaultHeaders setValue:@"com.lognllc.Magento-iOS-Example/1.0.0 (unknown, iPhone OS 8.1, iPhone Simulator, Scale/2.000000)" forKey:@"User-Agent"];
          [request setAllHTTPHeaderFields:defaultHeaders];
          [request setHTTPBody:[envelope dataUsingEncoding:NSUTF8StringEncoding]];
          NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
          if(connection){
              webData = [NSMutableData data];
          }
          else{
            NSLog(@"The Connection is null error: %@", error);
          }
      }
      @catch (NSException *e) {
       NSLog(@"catching %@ reason 1 %@", [e name], [e reason]);
      }
  }

The returned result I'm getting from the client server as following:

<html><head><title>NuSOAP: Opencart</title>
<style type="text/css">
    body    { font-family: arial; color: #000000; background-color: #ffffff; margin: 0px 0px 0px 0px; }
    p       { font-family: arial; color: #000000; margin-top: 0px; margin-bottom: 12px; }
    pre { background-color: silver; padding: 5px; font-family: Courier New; font-size: x-small; color: #000000;}
    ul      { margin-top: 10px; margin-left: 20px; }
    li      { list-style-type: none; margin-top: 10px; color: #000000; }
    .content{
    margin-left: 0px; padding-bottom: 2em; }
    .nav {
    padding-top: 10px; padding-bottom: 10px; padding-left: 15px; font-size: .70em;
    margin-top: 10px; margin-left: 0px; color: #000000;
    background-color: #ccccff; width: 20%; margin-left: 20px; margin-top: 20px; }
    .title {
    font-family: arial; font-size: 26px; color: #ffffff;
    background-color: #999999; width: 100%;
    margin-left: 0px; margin-right: 0px;
    padding-top: 10px; padding-bottom: 10px;}
    .hidden {
    position: absolute; visibility: hidden; z-index: 200; left: 250px; top: 100px;
    font-family: arial; overflow: hidden; width: 600;
    padding: 20px; font-size: 10px; background-color: #999999;
    layer-background-color:#FFFFFF; }
    a,a:active  { color: charcoal; font-weight: bold; }
    a:visited   { color: #666666; font-weight: bold; }
    a:hover     { color: cc3300; font-weight: bold; }
</style>
<script language="JavaScript" type="text/javascript">
<!--
// POP-UP CAPTIONS...
function lib_bwcheck(){ //Browsercheck (needed)
    this.ver=navigator.appVersion
    this.agent=navigator.userAgent
    this.dom=document.getElementById?1:0
    this.opera5=this.agent.indexOf("Opera 5")>-1
    this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0;
    this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
    this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
    this.ie=this.ie4||this.ie5||this.ie6
    this.mac=this.agent.indexOf("Mac")>-1
    this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
    this.ns4=(document.layers && !this.dom)?1:0;
    this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
    return this
}
var bw = new lib_bwcheck()
//Makes crossbrowser object.
function makeObj(obj){
    this.evnt=bw.dom? document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?document.layers[obj]:0;
    if(!this.evnt) return false
    this.css=bw.dom||bw.ie4?this.evnt.style:bw.ns4?this.evnt:0;
    this.wref=bw.dom||bw.ie4?this.evnt:bw.ns4?this.css.document:0;
    this.writeIt=b_writeIt;
    return this
}
// A unit of measure that will be added when setting the position of a layer.
//var px = bw.ns4||window.opera?"":"px";
function b_writeIt(text){
    if (bw.ns4){this.wref.write(text);this.wref.close()}
    else this.wref.innerHTML = text
}
//Shows the messages
var oDesc;
function popup(divid){
    if(oDesc = new makeObj(divid)){
    oDesc.css.visibility = "visible"
    }
}
function popout(){ // Hides message
    if(oDesc) oDesc.css.visibility = "hidden"
}
//-->
</script>
</head>
<body>
<div class=content>
    <br><br>
    <div class=title>Opencart</div>
    <div class=nav>
        <p>View the <a href="/api/appserver.php?wsdl">WSDL</a> for the service.
        Click on an operation name to view it&apos;s details.</p>
        <ul><li><a href='#' onclick="popout();popup('apiLogin')">apiLogin</a></li><div id='apiLogin' class='hidden'>

The HTML file is very long so I pasted part of it. However at the end of the log I get this error message:

An Error was occured { NSXMLParserErrorColumn = 14; NSXMLParserErrorLineNumber = 77; NSXMLParserErrorMessage = "AttValue: \" or ' expected\n"; }

the file name is appserver.php it should execute the file and returns XML result based on the API called.

Example of the correct result should be as tested on a demo server:

{"banners":[{"title":"Images","link":"/index.php?route=product/product&amp;product_id=47","image":"http://domain.comm/image/data/banners/3.png"},{"title":"Testing","link":"Image","image":"http://domain.comm/image/data/banners/4.png"},{"title":"Something","link":"/index.php?route=product/product&amp;path=57&amp;product_id=49","image":"http://domain.comm/image/data/banners/6.png"},{"title":"asdgasdg","link":"/index.php?route=product/product&amp;path=57&amp;product_id=49","image":"http://domain.comm/image/data/banners/5.png"},{"title":"Image","link":"/index.php?route=product/product&amp;path=57&amp;product_id=49","image":"http://domain.comm/image/data/banners/1.png"}],"modules":{"featured":[{"product_id":"47","thumb":"http://domain.com/image/cache/data/hp_1-187.5x187.5.jpg","name":"HP

Again I'm pasting part of the file rather the whole file as a summary so the question won't be much long.

I've checked the server configuration and found that SOAP, XMLWriter and XMLReader are installed on server but still the error persist. It's really a headache trying to resolve this issue for 2 weeks with no result. I appreciate your help if someone has already experienced that and can advise.

1
first, what about headers? do you correctly set all needed headers like auth, content-type, accept, etcDenis Alimov
second, sometimes php sends html in case of 500 error or 403 (unauthorized). save your html page on pastebin and provide a link hereDenis Alimov
Thanks for your support I posted my answer but now I have a new error for certain API functions call returning "nsxmlparser extra content at the end of the document" again when testing on my server it works fine but the clients server it's not. I've tried all possible solution with no luck, any advice please?David Buik

1 Answers

1
votes

After time of researches I found the error at the client's server, he configured his server to force www redirection so for whenever a request comes from non-www domain it should redirects to www, I changed the API URL call and added "www" and now it's loading the app.