I have taken over an application from a previous developer, and there was some code which was half finished using Nusoap, I am getting the error:
Call to register_donation() web service failed due to an exception:
Function ("register_donation") is not a valid method for this service
The application is built on: CakePHP 1.2.10 & using nuSoap 0.9.5.
I have already ini_set('soap.wsdl_cache_enabled', 0); (This doesnt help.)
My code is below (I shortened it for readability), Am I processing this response correctly?
(The code was taken over by myself from a previous developer).
The Pastebin (of the full code) is here: PasteBin Link
A shortened version is below for a glance over, the full version is in the pastebin link above.
<?php
ini_set('soap.wsdl_cache_enabled', 0);
class ServicesController extends AppController
{
var $uses = array("Donation");
var $components = array( "Email", "MatchingEvents" );
function registerDonation($donation = null){
$this->log('hit registerDonation', 'donation');
$this->autoRender = false;
App::import('Vendor','nusoap');
Configure::write('debug',0);
Configure::write('Session.start', false);
//init soap server
$server = new soap_server();
$endpoint = 'http://new.mysite.com/services/registerDonation';
//initialize WSDL support
$server->configureWSDL('donations', 'urn:donations', $endpoint);
//setup service type
$server->wsdl->addComplexType(
'DonationResult',
'complexType',
'struct',
'all',
'',
array(
'success' => array('name' => 'success', 'type' => 'xsd:boolean'),
'msg' => array('name' => 'msg', 'type' => 'xsd:string'),
'error_number' => array('name' => 'error_number', 'type' => 'xsd:string')
)
);
//register the method to expose
$server->register('register_donation',
array('ct_number' => 'xsd:string', 'project_id' => 'xsd:int', 'donation_amount' => 'xsd:decimal',
// Stripped all other params
),
array(
'result' => 'tns:DonationResult'
),
'urn:donations',
'urn:donations#register_donation',
'rpc',
'encoded',
'Accepts the results of a donation to a charity or project on the site'
);
//This inner function is registered and then called (keep within outer function!)
function register_donation(
// Pass in all the params (Stripped for readability)
$ct_number = null, $project_id = null, $donation_amount = null
){
// This function is never hit!.. its not registered, why?
$this->log('hit #3-register_donation called!', 'donation');
$return = $this->Donation->add(array(
'unique_id' => $unique_id,
'ct_number' => $ct_number,
'project_id' => $project_id,
'donation_amount' => $donation_amount,
// Pass in all other params, (Stripped for readability)
));
// Process that request
$log = $this->Donation->log . 'Result: ' . $return['msg'] . "\n";
$this->log( $log, 'donation' );
if ( isset($this->Donation->matching['events']) )
{
//Reserved donations should already have had their events handled
$this->MatchingEvents->handle($this->Donation->matching, !$this->Donation->matching['reserved']);
}
if ( !empty($this->Donation->cacheSaved) )
$this->_sendDonationEmails($this->Donation->cacheSaved);
return $return;
}
$HTTP_RAW_POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
$server->service($HTTP_RAW_POST_DATA);
}
function _sendDonationEmails($donation)
{
// Send the emails
}
}
?>
If there is any more information I can provide, please let me know.
To Summarise: How do I process a nusoap response coming from a external source.
Any debugging ideas, hints & tips or solution will be rewarded (+1!).
In the code posted there’s a function named
registerDonationinside a controller namedServicesController. Inside that function there is another function calledregister_donation(This obviously doesn’t make sense [Old Code]).Remove the function named
register_donationout of theregisterDonationmethod and place it as a method inside theServicesContoller.Then change the following line (where you register the function to expose).
From:
$server->register('register_donation',To:
$server->register('ServicesController.register_donation',So you’re calling
class.methodas opposed to method only (This would work in PHP4 procedural programming, but in OOP, you need to specify thecontroller.methodwhen exposing the function).Having other problems?
Q: CakePHP & nuSOAP is not hitting my controller/method, but redirects to app/webroot
A: Using CakePHP 1.2, I found when it did not have a services model (because it wasn’t
technically required), The request does not hit the controller. So if you’re having this issue, create a model for your controller, even if you’re not using it. For your reference, here a modal example:
Q:What URL should I be receiving responses to?
A: The URL you set your response to hit must include
?wsdl.So.. for eg: you have a controller named
SOAPControllerand method namedprocess.In CakePHP, your URL would look like this:
mydomain.com/SOAP/process.So your
WSDLis located atmydomain.com/SOAP/process?wsdl. Make sure your callback URL is set to this (including thewsdl).Q: How do I debug SOAP requests/responses using CakePHP?
A: CakePHP has a logging feature which proved invaluable in debugging
SOAP. In your controller (or modal) you can use:You can use this through your
SOAPrequest or response to see what parts are being hit/called and debugging variables (eg:$HTTP_RAW_POST_DATA).Q: My WSDL location is shown as `domain.com/app/webroot` when I visit the SOAP page.
A: I thought this was the issue causing all the problems, Looking at the source code
NusoapusesPHP_SELFto get the current script (which in cakePHP isapp/webroot/index.php), Don’t worry about this, you can access thewsdlby appending the URL with?wsdl, This will show you your generatedWSDLfile, You don’t need to worry about fixing this. Its not interfering with yourSOAPrequest whatsoever, It’s merely there for your convenience.Q: My SOAP address location is showing ‘domain.com/app/webroot/index.php`
A: This was a issue I fixed by including the
$endpointin theconfigureWSDL().