I need to call a method from a wadl service and pass a parameter
wadl: http://domain.com/application.wadl
method: checkInfo
How can i do that?
//Wsdl - Soap: I need to do this but with a wadl service
$wsdl = new SoapClient('http://domain.com/application?wsdl');
$wsdl->__call('checkInfo',array('data'=> ''));
//or
$wsdl->checkInfo(array('data'=> ''));
Thank you!!!!
you can do this by using SOAP server :
class MyClass{
function checkInfo() {
return "Hello";
}
}
//when in non-wsdl mode the uri option must be specified
$options=array('uri'=>'http://localhost/');
//create a new SOAP server
$server = new SoapServer(NULL,$options);
//attach the API class to the SOAP Server
$server->setClass('MyClass');
//start the SOAP requests handler
$server->handle();
then use it :
<?php
/*
* PHP SOAP - How to create a SOAP Server and a SOAP Client
*/
$options = array('location' => 'http://localhost/server.php',
'uri' => 'http://localhost/');
//create an instante of the SOAPClient (the API will be available)
$api = new SoapClient(NULL, $options);
//call an API method
echo $api->checkInfo();
?>
code example from here
Related
I am trying to connect to a SOAP webservice using php. I am very new to using php.
I can connect to the service, the test below returns a list of all the available functions of the webservice.
$url = "http://...client_ip.../dkServiceDefault/dkWSItemsCGI.exe/wsdl/IItemService";
$client = new SoapClient($url);
var_dump($client->__getFunctions());
If I try to access one of these functions(ex. NumberOfModifiedItems) then I get an error stating that I need to supply a SOAP header with a username and password.
According to the documentation of the SOAP service the header needs to look like this:
<soap:Header>
<q1:BasicSecurity id="h_id1" xmlns:q1="urn:dkWSValueObjects">
<Username xsi:type="xsd:string">username</Username>
<Password xsi:type="xsd:string">password</Password>
</q1:BasicSecurity>
</soap:Header>
How can I make this header in php? How do I attach it to the SoapClient? I have a username and password but I can't figure out how to create the exact header to send to the webservice. I have tried following several tutorials, but I just can't seem to get it to work.
You may pass SOAP headers with SoapHeader class and SoapClient::__setSoapHeaders method:
<?php
$url = "http://...client_ip.../dkServiceDefault/dkWSItemsCGI.exe/wsdl/IItemService";
$client = new SoapClient($url);
$namespace = "urn:dkWSValueObjects";
$authentication = array(
'Username' => 'yourname',
'Password' => 'yourpassword'
);
$header = new SoapHeader($namespace, 'BasicSecurity', $authentication, false);
$client->__setSoapHeaders($header);
var_dump($client->__getFunctions());
?>
I have a client call with a WSSE Security Header:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><wsse:UsernameToken wsu:Id="UsernameToken-7BCCD9337425FBA038149772606059420"><wsse:Username>USERNAME</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">PASSWORD</wsse:Password><wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">NONCE</wsse:Nonce><wsu:Created>2017-06-17T19:01:00.594Z</wsu:Created></wsse:UsernameToken></wsse:Security></soapenv:Header>
<soapenv:Body>
<ns:FUNCTION/>
</soapenv:Body>
</soapenv:Envelope>
As SoapServer I have a simple one:
// the SOAP Server options
$options=array(
'trace' => true
, 'cache_wsdl' => 0
, 'soap_version' => SOAP_1_1
, 'encoding' => 'UTF-8'
);
$wsdl = 'http://localhost/index.php?wsdl';
$server = new \SoapServer($wsdl, $options);
$server->setClass('ServerClass');
$server->handle();
$response = ob_get_clean();
echo $response;
As soon the property MustUnderstand = 1, then I become from the server the exception: Header not understood.
How to understand the header?
How to make the WSSE validation on the SoapServer side?
The solution is very tricky! I don't know why is this handled by this way from the SoapServer, but here is the solution:
class ServerClass {
public function Security($data) {
// ... do nothing
}
public function myFunction(){
// here the body function implementation
}
}
We need to define a function in our class, which is handling the soap request with the name of the header tag, which is holding the soap:mustUnderstand property. The function doesn't need to be implemented in some way.
That's all!
Mutatos' question / answer got me on the right track. I was working outside of a class structure so what worked for me was the following:
function Security($data)
{
$username = $data->UsernameToken->Username;
$password = $data->UsernameToken->Password;
//check security credentials here
}
$server = new SoapServer("schema/wsdls/FCI_BookingPullService.wsdl", array('soap_version' => SOAP_1_2));
$server->addFunction("Security");
$server->handle();
Essentially a function with the same name as the SOAP header "<wsse:Security>" (ignore the namespace) is being defined, then telling the server to use that to process the header with the 'addFunction' method.
Not ideal from a scope point of view, if that's an issue, try the class approach.
I am trying to create a simple PHP webservice as I am a newbie in this track. I decided to develop it using SOAP. I am using WAMP as a server and the problem is that I am unable to run the scripts nor get the WSDL file.
Here's server.php's code:
<?php
//call library
require_once ('lib/nusoap.php');
//using soap_server to create server object
$server = new soap_server;
//register a function that works on server
$server->register('get_message');
// create the function
function get_message($your_name)
{
if(!$your_name){
return new soap_fault('Client','','Put Your Name!');
}
$result = "Hello World ".$your_name .". Thanks for Your First Web Service Using PHP with SOAP";
return $result;
}
// create HTTP listener
$server->service($HTTP_RAW_POST_DATA);
exit();
?>
and here's a screenshot of the run:
Here's client.php's code:
<?php
require_once ('lib/nusoap.php');
//Give it value at parameter
$param = array( 'your_name' => 'Omar');
//Create object that referer a web services
$client = new soapclient('http://localhost/WebServiceSOAP/server.php');
//Call a function at server and send parameters too
$response = $client->call('get_message',$param);
//Process result
if($client->fault)
{
echo "FAULT: <p>Code: (".$client->faultcode."</p>";
echo "String: ".$client->faultstring;
}
else
{
echo $response;
}
?>
and here's a screenshot of the run:
running client.php
plus this error keeps bugging me:
Undefined variable: HTTP_RAW_POST_DATA
can u try this below code
$client = new soapclient('http://localhost/WebServiceSOAP/server.php');
to
$client = new SoapClient(
null,
array(
'location' => 'ADD YOUR LOCATION',
'uri' => 'ADD YOUR WSDL FILE ',
'trace' => 1,
'use' => SOAP_LITERAL,
)
);
You're trying to work with undefined variable $HTTP_RAW_POST_DATA. In PHP7 this hook is removed. You can read here
Instead of that I propose to do it like this:
$server->service(file_get_contents("php://input"));
Even though i am very new to php soap concept, i write a program to communicate a web server using SoapClient.
This is my wsdl link:
https://es.adpolice.gov.ae/TrafficInsurance/TrafficInsuranceServicesNew.asmx?wsdl
Service name : CreateVehicleInsurancePolicy
Link : https://es.adpolice.gov.ae/TrafficInsurance/TrafficInsuranceServicesNew.asmx?op=CreateVehicleInsurancePolicy
I hosted my program in in an ssl certified hosting.When it run it generating a response in soap body response(lngSerial).I wanted to print the response value from soap header (SoapHeaderOut).
that is intResponseCode,strArMsg,strEnMsg.
Please guide me.Below i am providing the script which i am using.
<?php
ini_set("soap.wsdl_cache_enabled", "0");
ini_set("soap.wsdl_cache_ttl", "0");
class SOAPStruct
{
function __construct($user, $pass)
{
$this->userName = $user;
$this->Password = $pass;
}
}
$service = new SoapClient("https://es.adpolice.gov.ae/TrafficInsurance/TrafficInsuranceServicesNew.asmx?wsdl");
$auth = new SOAPStruct('username','password');
$header = new SoapHeader("http://adpolice.gov.ae/TrafficInsurance/TrafficInsuranceServices.asmx","SoapHeaderIn",$auth,true);
$service->__setSoapHeaders($header);
$param = array('lngInsuranceCompanyCode'=> '1','intInsuranceKindCode'=>'1','lngTcf'=>'3070858641','strPolicyNo'=>'1055385883','dtExpiryDate'=>'2016-04-30T00:00:00','dtStartDate'=>'2015-03-31T00:00:00','strChassisNo'=>'6T1BE42RG465465','strRemarks'=>'demo','strUserCreated'=>'demo');
$response = $service->CreateVehicleInsurancePolicy($param)->CreateVehicleInsurancePolicyResult;
foreach ($response as $record) {
print_r($record);
print_r("<br>");
}
?>
As per PHP documentation you should be able to get to the SoapHeader response object by using the low level API instead of the magic method provided by WSDL. In your case it would be
$response = $service->__soapCall("CreateVehicleInsurancePolicy", $param, null, $headers, $response_headers);
Which should store the object that you want into $response_headers variable.
Old response: (pertains to actual HTTP headers of the response)
If you instantiate the SoapClient with trace set like so:
$service = new SoapClient("https://es.adpolice.gov.ae/TrafficInsurance/TrafficInsuranceServicesNew.asmx?wsdl", array("trace" => 1))
you should be able to get the Header response using the SoapClient::__getLastResponseHeaders
$response_headers = $service->__getLastResponseHeaders();
I have problem in creating web-service using cakephp .
this what i do to create this web-service .
I use NuSOAP - Web Services Toolkit for PHP for this.
I create a controller called WsController and import the library on it.
class WsController extends AppController{
var $uses = array();
function info() {
$this->layout= null;
$ns="http://www.techvoicellc.com/Tutorials//";
$server = new soap_server();
$server->configureWSDL('mostafa',$ns);
$server->wsdl->schemaTargetNamespace=$ns;
$server->wsdl->addComplexType('ArrayOfstring','complexType',
'array','','SOAP-ENC:Array',array()
,array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'string[]')),
'xsd:string');
$server->register('sum',
array('x' => 'xsd:integer','y' => 'xsd:integer'),
array('z' => 'xsd:integer'),
$ns,
"$ns#sum",
'rpc',
'encoded',
'documentation' // documentation
);
$server->service($HTTP_RAW_POST_DATA);
}
function sum($x,$y){
$z=$x+$y;
return new soapval('return','xsd:integer',$z);
}
}
and i create the clint in controller action like this
function index() {
$wsdl = 'http://localhost/asd/ws/info?wsdl';
$client = new nusoap_client ( $wsdl, true );
$this->client = new nusoap_client($wsdl, true);
$param1 = array ('x' => 2, 'y' => 1 );
$a = $client->call ( 'sum', $param1 );
echo $a;
}
it don't do any thins although that i create this in non cake project and its work very well
hope some one tell me what is the best practise to create web-service in cake php
This is quite Easy to develop web services in CakePHP. I have done it several times. Check the below steps.
class MyWebServicesController extends AppController {
var $name = 'MyWebServices';
var $layout = "ajax";
function index() {
$server = new SoapServer(null);
$server->setObject($this);
$server->handle();
exit(0);
}
public function addNumbers($a,$b) {
return $a+$b
}
}
Now your web service is hosted at http://webroot/MyWebServices
Now you can call addNumbers like below.
$client = new SoapClient(null, array('location' => "http://webroot/MyWebServices");
$sum = $client->addNumbers(1+2);
It is best to create restful web service. CakePHP has everything built in for REST. All you have to do is enable it and create json/xml views.
Here is a link with your starting point: http://book.cakephp.org/2.0/en/development/rest.html
Is there a reason you want SOAP web service?
It will be so much harder to create and test SOAP web service
SOAP will require external libraries
It will be harder for users to use the SOAP web service