0

Uncaught SoapFault exception: [a:InternalServiceFault] Object reference not set to an instance of an object. in Line ...

Already got lots of question related to this, but I am unable to make it work out, so posting this possible duplicate again.

Im missing something here in the request, but couldn't figure it out. Any help is appreciated.

I tried this in both PHP & NodeJS, and same error occurred in both.

<?php     
$wsdl = 'http://test.eprabhu.com/Api/Utility.svc?wsdl';

$params = array(
"UserName" => "CLIENT",
"Password" => "CLIENT12",
"OperatorCode" => 2,
"MobileNumber" => "9803111111",
"Amount" => 100,
"PartnerTxnId" => "P2019042205330171261"
);

$client = new SoapClient($wsdl, ['trace' => true]);
print_r($client->__getFunctions());
// gives
// Array
// (
//     [0] => MobileTopupResponse MobileTopup(MobileTopup $parameters)
//     [1] => RechargePinsResponse RechargePins(RechargePins $parameters)
//     ...
// )
print_r($client->__getTypes());
// gives
// Array
// (
//    [0] => struct InputMobileTopup {
//              string UserName;
//              string Password;
//              int OperatorCode;
//              string MobileNumber;
//              float Amount;
//              string PartnerTxnId;
//    }
//    [1] => struct ReturnTransaction {
//              string Code;
//              string Message;
//              string TransactionId;
//              string Data;
//    }
//    ...
// )

// $r = $client->MobileTopup($params);
// or 
$response = $client->__soapCall("MobileTopup", [$params]);
// gives error Object reference not set to an instance of an object
var_dump($response);
$xml = $soapClient->__getLastRequest();
print_r($xml);

// Also I tried using the class after looking into __getTypes result
$wsdl = 'http://test.eprabhu.com/Api/Utility.svc?wsdl';
class InputMobileTopup {
    public function __construct($UserName, $Password, $OperatorCode, $MobileNumber, $Amount, $PartnerTxnId) {
        $this->UserName = $UserName;
        $this->Password = $Password;
        $this->OperatorCode = $OperatorCode;
        $this->MobileNumber = $MobileNumber;
        $this->Amount = $Amount;
        $this->PartnerTxnId = $PartnerTxnId;
    }  
}

$InputMobileTopup = new InputMobileTopup("CLIENT", "CLIENT12", 2, "9803111111", 1000, "P2019042205330171261");
print_r($InputMobileTopup);

$client = new SoapClient($wsdl, ['trace' => true]);

$response = $client->__soapCall("MobileTopup", [$InputMobileTopup]);
var_dump($response);
$xml = $soapClient->__getLastRequest();
print_r($xml);
// Still the same error.

In NodeJS too, the same error occurs Object reference not set to an instance of an object.

"use strict";

var soap = require('strong-soap').soap;

var url = 'http://test.eprabhu.com/Api/Utility.svc?wsdl&UserName=CLIENT';
var requestArgs = {
    'UserName': 'CLIENT',
    'Password': 'CLIENT12',
    'OperatorCode': 2,
    'MobileNumber': '9803111111',
    'Amount': 100,
    'PartnerTxnId': 'P201904220218335187'
};

var options = {
  'user-agent': 'sampleTest',
  'Content-Type': 'text/xml;charset=UTF-8',
  // 'soapAction': 'http://test.eprabhu.com/Api/Utility.svc?wsdl#MobileTopup',
  'soapAction': 'http://tempuri.org/IUtility/MobileTopup'
};

soap.createClient(url, options, function(err, client) {

    var method = client['MobileTopup'];
    method(requestArgs, function(err, result, envelope, soapHeader) {
        //response envelope
        console.log('Response Envelope: \n' + envelope);
        //'result' is the response body
        console.log('Result: \n' + JSON.stringify(result));

        console.log('Soap Header: \n', soapHeader);
    });
});

Any help will be appreciated. Thanx. Please prioritize the answer for NodeJS. ..

1 Answer 1

1
+50

According to your WSDL the function MobileTopup takes as parameter message of type MobileTopup:

Array
(
    [0] => MobileTopupResponse MobileTopup(MobileTopup $parameters)
    [1] => RechargePinsResponse RechargePins(RechargePins $parameters)
    ...
)

on the other hand the type MobileTopup is defined like:

Array
(
    ...
    [85] => struct MobileTopup {
              InputMobileTopup MobileTopupRequest;
            }
    ...
)

so you have to call the function like this:

$response = $client->__soapCall("MobileTopup", ['MobileTopup' => ['MobileTopupRequest' => $params]]);

this returns (according to the params you have set)

object(stdClass)#3 (1) {
  ["MobileTopupResult"]=>
  object(stdClass)#4 (4) {
    ["Code"]=>
    string(3) "021"
    ["Message"]=>
    string(30) "Duplicate Partner Txn ID Found"
    ["TransactionId"]=>
    string(0) ""
    ["Data"]=>
    string(0) ""
  }
}

I guess the same is true for node.js

UPDATE

In node.js the problem comes from your requestArgs object. it should be like this:

var requestArgs = {
    MobileTopupRequest: {
        UserName: 'CLIENT',
        Password: 'CLIENT12',
        OperatorCode: 2,
        MobileNumber: '9803111111',
        Amount: 100,
        PartnerTxnId: 'P201904220218335187'
    }
};
Sign up to request clarification or add additional context in comments.

5 Comments

Good for php,it worked. thx... but for node its not working. we need to generte envelop and send in headers, nut inable to do it..
@SanjayKhadka I have updated the answer for node.js, please have a look
ty it worked ... only problem I had is I re-tried with this format just like in php {MobileTopup: MobileTopupRequest: { Username: .... } }; Also I added a bounty to your answer... :)
btw can you explain one thing. How do you, or how can we know about the MobileTopupRequest request parameter format looking at the xml wsdl.. Is there a way to understand this?
@SanjayKhadka actually it is not declared in WSDL, so I was basing on your structure

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.