12

I am getting below error after requesting SOAP call.

fault code: HTTP, fault string: Bad Request

Is this badly formed message?

try{
    $client = new SoapClient("http://ip_add/something.asmx?WSDL", array("trace" => true, 'exceptions' => 1));

    $params = new \SoapVar('<?xml version="1.0" encoding="utf-8"?>
                <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                <soap:Body>
                    <RemittanceService xmlns="http://tempuri.org/">
                    <CreditTxnMessage xmlns="http://my_url">
                    <Corporate_UID xmlns="">66666</Corporate_UID>
                    <Mandate_Type xmlns="">P</Mandate_Type>
                    <MICR_No xsi:nil="true" xmlns="" />
                    <Instrument_No xsi:nil="true" xmlns="" />
                    <Remitter_Address1 xmlns="">285 enfiled pl</Remitter_Address1>
                    <Remitter_Address2 xmlns="">mississauga</Remitter_Address2>
                    <Remitter_Address3 xmlns="">16y2n4</Remitter_Address3>
                    <Remitter_Country xmlns="">Canada</Remitter_Country>
                    <Remitter_ZIP_Code xsi:nil="true" xmlns="" />
                    <Remitter_EmailID xsi:nil="true" xmlns="" />
                    <Remitter_Contact_No xmlns="" />
                    <Beneficiary_ZIP_Code xsi:nil="true" xmlns="" />
                    <Beneficiary_EmailID xsi:nil="true" xmlns="" />
                    <Beneficiary_Contact_No xmlns="" />
                    <Beneficiary_Bank_Name xmlns="">PNB</Beneficiary_Bank_Name>
                    </CreditTxnMessage>
                    </RemittanceService>
                </soap:Body>
                </soap:Envelope>', XSD_ANYXML);

    $result = $client->__soapCall('RemittanceService', array($params));
    highlight_string($client->__getLastRequest());
}
catch(SoapFault $fault){
    die("SOAP Fault:<br />fault code: {$fault->faultcode}, fault string: {$fault->faultstring}");
}

I don't know what's wrong here.

Stack Trace

SoapFault exception: [HTTP] Bad Request in /var/www/mtes/public_html/application/controllers/bank_api_pnb.php:146
Stack trace:
#0 [internal function]: SoapClient->__doRequest('<?xml version="...', 'http://124.124....', 'http://tempuri....', 1, 0)
#1 /var/www/mtes/public_html/application/controllers/bank_api_pnb.php(146): SoapClient->__soapCall('RemittanceServi...', Array)
#2 [internal function]: Bank_api_pnb->test()
#3 /var/www/mtes/public_html/system/core/CodeIgniter.php(359): call_user_func_array(Array, Array)
#4 /var/www/mtes/public_html/index.php(220): require_once('/var/www/mtes/p...')
#5 {main}
RNK
  • 5,582
  • 11
  • 65
  • 133
  • 1
    try{ // ... }catch ( SoapFault $e ) { echo $e->getMessage (); # Check the error msg } – PHPJungle Jun 26 '15 at 15:40
  • It says: Bad Request. `faultstring` and `getMessage()` are same. – RNK Jun 26 '15 at 15:43
  • Do you have a proxy server? I suggest you use Fiddler to capture the HTTP Request, then check the Server response. – PHPJungle Jun 26 '15 at 16:03
  • Remove "?WSDL" from your request and try again. Adding that is used in a GET request to pull the WSDL down so you can implement to it. If you already know your methods that should not be in the URL. – Brian from state farm Jun 29 '15 at 19:03
  • @Brian: I did that. And now I am getting this error: `SOAP-ERROR: Parsing WSDL: Couldn't load from URL. Premature end of data in tag html` – RNK Jun 29 '15 at 19:57
  • Sorry I may have misled you because I did not understand the SoapClient object you are working with. Disregard my first comment. The call you are using __soapCall does not look like it takes a SOAP formed request. Did you try __doRequest? – Brian from state farm Jun 29 '15 at 20:26
  • @Brian: I don't have much knowledge in this. Can you please tell me how to put 4 params in `__dorequest()` with the data I provided? Thanks. – RNK Jun 29 '15 at 20:40
  • Hie.... This is because of your envelop binding is wrong, Just create your envelope like this http://stackoverflow.com/questions/18973611/call-soap-webservice-in-phonegap-for-android/18980489#18980489 – MDroid Jul 06 '15 at 13:46
  • I have made this envelop for PhoneGap. but I am sure it will help you! – MDroid Jul 06 '15 at 13:47
  • If you dont need header you can remove that part – MDroid Jul 06 '15 at 13:49

4 Answers4

7

The whole point of the SoapClient is to convert calls to xml; so you shouldn't be doing this manually. Try this instead:

try {
    $client = new SoapClient("http://ip_add/something.asmx?WSDL", array("trace" => true, 'exceptions' => 1));

    $result = $client->RemittanceService(array(
            'CreditTxnMessage' => array(
                    'Corporate_UID' => 66666,
                    'Mandate_Type' => 'P',
                    'MICR_No' => null,
                     /* you get the idea */
                    'Beneficiary_Contact_No' => '',
                    'Beneficiary_Bank_Name' => 'PNB'
            )
    ));

    highlight_string($client->__getLastRequest());
}
catch(SoapFault $fault){
    die("SOAP Fault:<br />fault code: {$fault->faultcode}, fault string: {$fault->faultstring}");
}

The exact format of the parameters and their names would be specified in the WSDL.

Sjon
  • 4,989
  • 6
  • 28
  • 46
5

Generally a Bad Request response to a SOAP request is returned when the message is not in a good format (invalid header, body, ..) and therefor the document can't be parsed. First of all try to remove the XML version declaration from your SoapVar and see if it fixes the problem (remove the line below):

<?xml version="1.0" encoding="UTF-8"?> 

Alternatively you can always test your Soap requests in tools like SoapUI to make sure they work and then complete your code. If it doesn't work in SoapUI it means there is something wrong with the request. Try to revise the WS and make sure you are sending everything in the correct format (eg. maybe you need to authenticate? SoapHeader? ..)

hatef
  • 5,491
  • 30
  • 43
  • 46
  • I tried to remove above line from `SOAP` message. No,w It's giving me this error: `An exception is thrown by the Orchestration schedule` – RNK Jun 29 '15 at 14:21
  • And I can not check it on `SOAPUI` because our client restricted SOAP access to our server's IP address only. – RNK Jun 29 '15 at 17:13
1

I am not to familiar with PHP but try this.

$Request = '<RemittanceService xmlns="http://tempuri.org/">
                    <CreditTxnMessage xmlns="http://my_url">
                    <Corporate_UID xmlns="">66666</Corporate_UID>
                    <Mandate_Type xmlns="">P</Mandate_Type>
                    <MICR_No xsi:nil="true" xmlns="" />
                    <Instrument_No xsi:nil="true" xmlns="" />
                    <Remitter_Address1 xmlns="">285 enfiled pl</Remitter_Address1>
                    <Remitter_Address2 xmlns="">mississauga</Remitter_Address2>
                    <Remitter_Address3 xmlns="">16y2n4</Remitter_Address3>
                    <Remitter_Country xmlns="">Canada</Remitter_Country>
                    <Remitter_ZIP_Code xsi:nil="true" xmlns="" />
                    <Remitter_EmailID xsi:nil="true" xmlns="" />
                    <Remitter_Contact_No xmlns="" />
                    <Beneficiary_ZIP_Code xsi:nil="true" xmlns="" />
                    <Beneficiary_EmailID xsi:nil="true" xmlns="" />
                    <Beneficiary_Contact_No xmlns="" />
                    <Beneficiary_Bank_Name xmlns="">PNB</Beneficiary_Bank_Name>
                    </CreditTxnMessage>
                    </RemittanceService>';
$result = $client->__doRequest($Request, "http://ip_add/something.asmx", "RemittanceService",  soap_1_2, 0);
Brian from state farm
  • 2,825
  • 12
  • 17
  • I used `SOAP_1_1` and I am getting this response: `soap:ClientServer did not recognize the value of HTTP Header SOAPAction: RemittanceService.` – RNK Jun 30 '15 at 14:25
1

I'm not sure how SoapVar works, but I would advise against passing in raw XML to the SoapClient. I would try to recreate the XML structure in PHP arrays (painful, I know), especially since the XML appears in the stack trace:

$params = array(
    "RemittanceService" => array("xmlns"=>"http://tempuri.org/", "_" => array(
        "CreditTxnMessage" => array("xmlns" => "http://my_url", "_" => array(
            "Corporate_UID" => array("xmlns" => "", "_" => 66666),
            "Mandate_Type" => array("xmlns" => "", "_" => "P"),
            "MICR_No" => array("xsi:nil" => "true", "xmlns" => ""),
            // and so on...
        ))
    ))
);

Also, you should probably specify the SOAP version (SOAP_1_1 or SOAP_1_2) in the constructor of the SOAPClient:

$client = new SoapClient("http://ip_add/something.asmx?WSDL", array('soap_version' => SOAP_1_2, "trace" => true, 'exceptions' => 1));

Also, the arguments array in __soapCall() is pretty picky about the formatting. Try the following:

$result = $client->__soapCall('RemittanceService', array('parameters' => $params));

Or even:

$result = $client->__soapCall('RemittanceService', $params);

I'm basically guessing as to what the issue is, so this isn't a very thorough solution. You can also try looking elsewhere on SO. For example, this answer uses SoapVar.

Community
  • 1
  • 1
ajiang
  • 1,702
  • 2
  • 12
  • 12