Get method name from soap service call - php

I have a working soap service with several methods available. I am wondering if it is possible to get the name of the method that the user contacting the service is requesting; for example:
try{
$soapServer = new Zend_Soap_Server('http://path-to-service/wsdl');
$soapServer->setClass('My\Soap\Server\Class');
$soapServer->handle();
// is something like this available? :
// $callName = $soapServer->getLastRequestedMethod();
// or
// $callName = $soapServer->getMethod();
}catch(SoapFault $e){
echo $e->getMessage();
}
I didn't see anything like this in the docs # zend or php.net, but just thought I would check to see if anyone knows a way to do this; would be useful for logging purposes. Thanks!

Zend_Soap_Server supports a getLastRequest() method. Example usage would be:
$soapServer = new Zend_Soap_Server('http://path-to-service/wsdl');
$soapServer->setClass('My\Soap\Server\Class');
$soapServer->handle();
$lastRequestXML = $soapServer->getlastRequest()`;

Related

SOAP API: PHP Code doesn't work, but SOAP UI works

I'm trying to create a rental module for my work's website. When I use the same parameters in SmartBear's SOAP UI, it works. So, I'm assuming that it's something to do with my code, but I cannot find the error.
<?php //Submit Reservation
require("connect.php");
$sessionId = $_SESSION['sessionId'];
$moveInDate = date("yy-m-d", strtotime($_POST['moveIn']));
$billDate = date("d", strtotime($_POST['moveIn']));
$rentalOptions = array(
"customer.company" =>$company,
"account.startDate"=> $moveInDate,
"contact.firstName"=>$_POST['fname'],
"contact.lastName"=>$_POST['lname'],
"contact.companyName"=>$_POST['companyName'],
"contact.street1"=>$_POST['street'],
"contact.street2"=>"",
"contact.city"=>$_POST['city'],
"contact.state"=>$_POST['state'],
"contact.zip"=>$_POST['zip'],
"contact.country"=>"US",
"phone.1" => $_POST['mobile'],
"contact.email"=>$_POST['email'],
"account.currency"=>"1",
"account.billDay"=> $billDate,
"user.paymentMethod"=>"1",
"user.draftDay"=>"",
"unit.id"=>$_POST['unit-name'],
"postAccount"=>"Y",
"promotionId"=>$_POST['promo'],
"insuranceId"=>$_POST['insurance']
);
var_dump($rentalOptions);
echo "<br/>". $sessionId ."<br/>Return";
$client = new SoapClient("https://api.doorswap.com/service/system.wsdl");
// Make API Call
$dsReceiver = "customer";
$dsAction = "saveNewCustomer";
$result = $client->makeReceiverCall($sessionId, $dsReceiver, $dsAction, $rentalOptions);
//if($result["success"] == "true") {
//}
?>
The var_dump() and print_r() displays correct variables from the previous form. In fact, ALL variables are correctly output. I just do not understand WHY it isn't working. It's not giving me an error, it's just not POSTING.
FYI: I have tried using javascript SOAP and I run into similar issues. The code is right, the variables are correct, but the addition to the system is not going through.
I'm open to trying anything.
Initialization of the SoapClient class
First of all inialize your soap client the right way. The soap client class takes several options to debug your application. In the following example a soap client is initalized with some options, that will make your life more easy when testing.
try {
$client = new SoapClient('https://api.doorswap.com/service/system.wsdl', [
'exceptions' => true,
'trace' => true,
]);
} catch (SoapFault $fault) {
var_dump($fault->getMessage());
if ($client instanceof SoapClient) {
var_dump(
$client->__getLastRequest(),
$client->__getLastResponse()
);
}
}
As you can see the soap client class is initialized with different option parameters. The exception option is set to true so that the soap client class throws exceptions, when an error occurs. That 's why the code is wrapped in a try and catch block. The trace option allows us to see, what the last request and response looks like. If the soap client was initialized you can use the build in methods __getLastRequest() and __getLastResponse() to see what the sent and received xml looks like. Keep in mind, that these can be empty if the xml wasn 't compiled by the client.
The makeReceiverCall method was a "name"/"value" system. So the above array of $rentalOptions SHOULD have been done as such. This is currently working, and has been flawless in execution.
$rentalOptions = array(
array("name"=> "customer.company","value"=> $company),
array("name"=> "account.startDate", "value"=> $moveInDate),
...
);
This was the error that I had run into and finally figured out. Thank you to all those who tried to help.

Call to undefined method soapclient::GetOrdersByClientPO()?

I am trying to consume a soap service in php. This is the code I am using
define('APIURL','https://cgorders.com/v2.1/Service.asmx?WSDL');
$client = new SoapClient(APIURL);
$search_query = new StdClass();
$search_query ->CustomerID = CustomerID;
$search_query ->ClientPO = $orderID;
$search_query ->AccessToken = $cgTokem; //AccessToken;
$result = $client->GetOrdersByClientPO($search_query);
//echo "<pre>";print_r($result->GetOrdersByClientPOResult->Orders->OrderID);echo "</pre>";exit;
if(isset($result->GetOrdersByClientPOResult->Orders->OrderID))
{
return($result->GetOrdersByClientPOResult->Orders->OrderID);
}
else
{
return('');
}
I am passing appropriate parameters correctly, did not mention them, for security reasons.
I am getting Call to undefined method soapclient::GetOrdersByClientPO() . Can anybody help?
check available functions by calling $client->::__getFunctions(); and see if GetOrdersByClientPO in the returned list
Soap client was not installed over the server, I was testing. After installation, it worked like a charm.
Thank you.

PHP Soap calling Error

I am trying to connect to webservice using SOAP / wsdl, but I constantly get the error. I am new in soap-api in php. I have a document of api detail, it shows:
public WSGetCalendarFareResponse GetCalendarFare(WSGetCalendarFareRequest calanderFareRequest)
I made my code accordingly but still found error/exception. Please review my php code in following:
$wsdl = "http://api.abc.com/xyz/service.asmx?wsdl"; // This is a test Web Service URL
$h = array();
$opta["GetCalendarFare"]["request"]= array(
"Origin"=>"DEL",
"Destination"=>"IXR",
"DepartureDate"=>"2015-05-01T00:00:00",
"ReturnDate"=>"2015-05-01T00:00:00",
"Type"=>"OneWay",
"CabinClass"=>"All",
"PreferredCarrier"=>"",
"AdultCount"=>1,
"ChildCount"=>1,
"InfantCount"=>"0",
"SeniorCount"=>"0",
"PromotionalPlanType"=>"Normal",
"IsDirectFlight"=>false
);
$client_header = new SoapHeader('http://192.168.0.0/TEST/BookingAPI','AuthenticationData',$hparams,false);
$cliente = new SoapClient($wsdl, array('trace' => 0));
$cliente->__setSoapHeaders(array($client_header));
try{
$h= (array)$cliente->__call('GetCalendarFare',$opta);
}catch(Exception $e)
{
echo '<pre>';
var_dump($e);
}
When I execute my code, it returns following error:
"System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at BookingAPI.WSCalendarFareInput(WSGetCalendarFareRequest calanderFareRequest) in c:\inetpub\wwwroot\api.tektravel.com\TboApi_V7\App_Code\Service.cs:line 4544
at BookingAPI.GetCalendarFare(WSGetCalendarFareRequest calanderFareRequest) in c:\inetpub\wwwroot\api.tektravel.com\TboApi_V7\App_Code\Service.cs:line 4360
Can anyone please suggest that where problem exists? It hit & try many times but couldn't get the error point.
Can you debug the execution of your POST on the server side? It is heavy guessing from my side, but I assume that you do not set a mandatory value in the request, which the server needs to deserialize your object. Hence the NullReferenceException.
Problem has resolved.
The Request array should be like this:
$opta["GetCalendarFare"]["calanderFareRequest"]= array(
"Origin"=>"DEL",
"Destination"=>"IXR",
"DepartureDate"=>"2015-05-01T00:00:00",
"ReturnDate"=>"2015-05-01T00:00:00",
"Type"=>"OneWay",
"CabinClass"=>"Economy",
"PreferredCarrier"=>"",
"AdultCount"=>1,
"ChildCount"=>1,
"InfantCount"=>"0",
"SeniorCount"=>"0",
"PromotionalPlanType"=>"Normal",
"IsDirectFlight"=>false
);

Soap not getting sent correctly, need to get the request

I have this class to send a SOAP-request (the class also defines the header)
class Personinfo
{
function __construct() {
$this->soap = new SoapClient('mysource.wsdl',array('trace' => 1));
}
private function build_auth_header() {
$auth->BrukerID = 'userid';
$auth->Passord = 'pass';
$auth->SluttBruker = 'name';
$auth->Versjon = 'v1-1-0';
$authvalues = new SoapVar($auth, SOAP_ENC_OBJECT);
$header = new SoapHeader('http://www.example.com', "BrukerAutorisasjon", // Rename this to the tag you need
$authvalues, false);
$this->soap->__setSoapHeaders(array($header));
}
public function hentPersoninfo($params){
$this->build_auth_header();
$res = $this->soap->hentPersoninfo($params);
return $res;
}
}
The problem is that there's something wrong with my function and the response is an error. I'd like to find out what content I am sending with my request, but I can't figure out how.
I've tried a try/catch-block in the hentPersoninfo-function that calls $this->soap->__getLastRequest but it is always empty.
What am I doing wrong?
Before I ever start accessing a service programmatically, I use SoapUI to ensure that I know what needs sent to the service, and what I should expect back.
This way, you can ensure the issue isn't in the web service and/or in your understanding of how you should access the web service.
After you understand this, you can narrow your focus onto making the relevant SOAP framework do what you need it to do.

How to execute a web service using php

I need to execute a web service from a php page
The web service is located in the following url
https://www.agemni.com/AgemniWebservices/service1.asmx
The Web Service uses a SOAP protocol to exchange messages.
The WSDL info can be located at https://www.agemni.com/AgemniWebservices/service1.asmx?WSDL
The function in that service that we need to use is ValidateEntity
//ValidateEntity("username", "password", "companyID", 2, keys, values)
So , how can i execute this web service and get result from my php page
A simple example, hope it helps...
$service1 = new SoapClient('https://www.agemni.com/AgemniWebservices/service1.asmx');
//here you instanciate your object with those properties
$entity = new Entity();
$entity->strUsername = 'José';
$entity->strPassword = '123';
$entity->strCompanyName = 'Somethin';
$entity->0 //because your type is int
$res = $service1->ValidateEntity($entity);//here you send the information to your service's method, if I'm not mistaken, it must be a object
$res->ValidateEntityResult;//this is the return of your service.
As I said, it is really simple but works.
See soap calls help from php.net:
http://www.php.net/manual/en/soapclient.soapcall.php
You need to use PHP's SOAP libraries...
http://www.php.net/manual/en/soapclient.soapcall.php
For https WebServices you need to enable openssl extension. The WS use .net it means that the class use type hinting so you need to create the ValidateEntity class, here's the code:
$ws = new soapclient('https://www.agemni.com/AgemniWebservices/service1.asmx?wsdl');
class ValidateEntity {
public $strUsername,
$strPassword,
$strCompanyName,
$objecttype;
}
$parameters = new ValidateEntity();
$parameters->strUsername = 'username';
$parameters->strPassword = 'password';
$parameters->strCompanyName = 'company';
$parameters->objecttype = 1;
echo '<pre>';
print_r($ws->ValidateEntity($parameters));
echo '</pre>';

Categories