Magento 1.9 – Fix Soap API Response 500 Internal Server Error

apimagento-1.9soap

I have just started learning magento.

I was trying to use magento SOAP services. For that I have created SOAP user and SOAP user role. Assigned role to user.

Now, my code to access SOAP service is something like this.

$client = new SoapClient('http://HOST_NAME/index.php/api/v2_soap/?wsdl');
print_r($client);
$sessionId = $client->login('USER_XX', 'XXXXXX');
var_dump($sessionId);
exit;

here I got proper object to $client but when I try to login with that I am getting this error.
Output of above code is:

SoapClient Object
(
    [_soap_version] => 1
    [sdl] => Resource id #2
)

Fatal error:  Uncaught SoapFault exception: [Client] DTD are not supported by SOAP in /PATH_TO_FOLDER/test.php:6
Stack trace:
#0 /PATH_TO_FOLDER/test.php(6): SoapClient->__call('login', Array)
#1 /PATH_TO_FOLDER/test.php(6): SoapClient->login('USER_XX', 'XXXXXX')
#2 {main}
  thrown in /PATH_TO_FOLDER/test.php on line 6

After some googling I came to know that above error occurs because webservice returning some html response. For like 404, 403 or 500 server errors.

So I have checked same webservice using software "SoapUI" and I got this result.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <title>Server error!</title>
      <link rev="made" href="mailto:info@edenweb.ch"/>
      <style type="text/css">
         <!--/*-->
         /*>&lt;!--*/ 
    body { color: #000000; background-color: #FFFFFF; }
    a:link { color: #0000CC; }
    p, address {margin-left: 3em;}
    span {font-size: smaller;}
/**/-->
      </style>
   </head>
   <body>
      <h1>Server error!</h1>
      <p>The server encountered an internal error and was 
    unable to complete your request. Either the server is
    overloaded or there was an error in a CGI script.</p>
      <p>
         If you think this is a server error, please contact
the
         <a href="mailto:info@edenweb.ch">webmaster</a>
         .
      </p>
      <h2>Error 500</h2>
      <address>
         <a href="/">b2c.biodis.ch</a>
         <br/>
         <span>
            Thu Apr 23 10:45:35 2015
            <br/>
            Apache
         </span>
      </address>
   </body>
</html>

Do you have any idea what's going wrong?

Thank you.

Best Answer

Resolved: Issue was

Duplicate Content-Type header on API responses | MageGyver

At file app/code/core/Mage/Api/Model/Server/V2/Adapter/Soap.php

function run previous like this :

$this->getController()->getResponse()
    ->clearHeaders()
    ->setHeader('Content-Type', 'text/xml; charset=' . $apiConfigCharset)
    ->setHeader('Content-Length', strlen($content), true)
    ->setBody($content);

Changed it to:

$this->getController()->getResponse()
    ->clearHeaders()
    ->setHeader('Content-Type', 'text/xml; charset=' . $apiConfigCharset, true) // <== Included true
    ->setHeader('Content-Length', strlen($content), true)
    ->setBody($content);