Collecting/Processing headers in PHP Soap Server - php

I'm creating a web service using PHP5's native SOAP Methods. Everything went fine until I tried to handle authentication using SOAP Headers.
I could easily find how to add the username/password to the SOAP headers, client-side:
$myclient = new SoapClient($wsdl, $options);
$login = new SOAPHeader($wsdl, 'email', 'mylogin');
$password = new SOAPHeader($wsdl, 'password', 'mypassword');
$headers = array($login, $password);
$myclient->__setSOAPHeaders($headers);
But I can't find anywhere the methods for collecting and processing these headers server-side. I'm guessing there has to be an easy way to define a method in my SoapServer that handles the headers...

With a modern PHP version it is NOT necessary to add anything to the WSDL as the headers are part of the SOAP Envelope specification.
The user contributed example cited by Paul Dixon does not work simply because the header is not UserToken as written in the comment, the header is Security, so that's is the name the class method should have. Then you get a nice stdClass object with a UserToken stdClass object property that has Username and Password as properties.
Example code (to be inserted in a PHP class that implements the SOAP service:
public function Security( $header ){
$this->Authenticated = true; // This should be the result of an authenticating method
$this->Username = $header->UsernameToken->Username;
$this->Password = $header->UsernameToken->Password;
}
Works like a charm for Username/Password based WSSE Soap Security

SoapClient uses the username and password to implement HTTP authentication. Basic and Digest authentication are support (see source)
For information on implementing HTTP authentication in PHP on the server side, see this manual page.
If you don't want to use HTTP authentication, see this user-contributed sample on the SoapServer manual page which shows how you could pass some credentials in a UsernameToken header.

You can try reading RAW POST data.
if ( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
$xml = file_get_contents('php://input');
print( htmlspecialchars( $xml ) );
// XML processing
}
In $xml you will have the whole SOAP XML request.
SoapServer does not have methods for reading SOAP headers.
--
edit: contributed example from manual does not seem to work, header handling method never gets called

You have to use a current version of PHP. With PHP 5.2.4 I had the same problem, but with 5.2.17 or 5.3.8 the callback for SOAP header handling (described in the user-contributed samle on php.net) gets called and everything works pretty fine.

Related

How to pass SOAP Request Properties in PHP (as shown in SoapUI)

In SOAP UI I am able to pass a few request properties, specifically Username, Password and WSS-Password Type. They are marked in the screenshot below by a red box:
I've tried passing these values in PHP as the second parameter of the SoapClient function like so:
$soap = new SoapClient('https://rev-int.api.us.fleetmatics.com/Vehicle/SageQuest/VehicleService.svc?wsdl',
array("Username" => "blah#example.com",
"Password" => "notarealpassword",
"WSS-Password Type" => "PasswordText"));
But I get the security error: An error occurred when verifying security for the message.
The WSDL I'm calling itself is here, though my question is about passing the request properties this way in general, really: https://rev-int.api.us.fleetmatics.com/Vehicle/SageQuest/VehicleService.svc?wsdl
It is likely that a security header with username and password is sent along with the message itself, but you can not see this header in the normal window in SoapUI.
Click "http log" in the bottom panel of SoapUI to see the whole communication. Look thoroughly for Header ithems.
If any header ithems, they must be set before your function SOAP call in PHP, but after you've constructed the client.
Use __ setSoapHeaders() to set headers in PHP
I was also getting that security error.
I used the soapui tool as well. But i had better success with the Chrome App called Boomerang.
You need to choose WSS PasswordText under Auth and then everything just seems to work.
Then you can even copy the xml that Boomerang generates into PHP and it works as well.

Invoking WCF service with PHP (with federated security)

I’m trying to invoke a WCF service (.NET) from PHP. It’s a little more complicated than just using a SoapClient since the service uses a WS2007FederationHttpBinding to authenticate.
Here’s the code I’m using at the moment. I haven’t even added credentials as I’m not sure how, but regardless, I’m not even at the point where I’m getting access denied errors.
$wsdl = "https://slc.centershift.com/sandbox40/StoreService.svc?wsdl";
$client = new SoapClient($wsdl,array(
//'soap_version'=>SOAP_1_2 // default 1.1, but this gives 'Uncaught SoapFault exception: [HTTP] Error Fetching http headers'
));
$params = array();
$params['SiteID'] = 123;
$params['GetPromoData'] = false;
$ret = $client->GetSiteUnitData(array('GetSiteUnitData_Request'=>$params));
print_r($ret);
Which WSDL should I be pointing to?
https://slc.centershift.com/Sandbox40/StoreService.svc?wsdl
Seems to be very short, but includes a reference to (note the wsdl0) https://slc.centershift.com/Sandbox40/StoreService.svc?wsdl=wsdl0
https://slc.centershift.com/Sandbox40/StoreService.svc?singleWsdl
Seems to have everything in it.
Do I need to specify SOAP 1.2? When I do, I get a connection timeout ([HTTP] Error Fetching http headers). When I don’t, the default of SOAP 1.1 is used and I get a [HTTP] Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'. Is this because I’m not authenticated yet, or because I’m using the wrong SOAP version?
How to authenticate in PHP? Here’s the corresponding .NET/C# code. Do I need to somehow put these as SOAP headers? Or am I thinking about it all wrong, and I need to do some kind of authentication before I even call the method (from what I read, I’m supposed to get a token back and then use it for all future method calls – I think I see an example of this in an answer here on Stack Overflow.
If I call $client->__getFunctions(), using either WSDL and either SOAP version, I’m getting a valid list of all functions, so I assume either of these is fine and my real issue is the authentication.
Other programmers I’ve talked to had spent time trying to get this to work, but gave up and instead implemented a proxy in .NET. They pass their parameters from PHP to their own unsecured .NET service, which in turn calls this secure service. It works, but seems crazily inefficient to me, and counter-productive, as the purpose of WCF is to support all types of clients (even non-HTTP ones!).
I’ve read How to: Create a WSFederationHttpBinding on MSDN, but it didn’t help.
You can use this URL for WSDL https://slc.centershift.com/Sandbox40/StoreService.svc?singleWsdl. This WSDL has all definitions.
You have to use 1.2 because this webservice works with SOAP 1.2 version. I tried it with 1.1 and 1.2 and both of them gived error. 1.1 is version error, 1.2 is timeout error. I think there is an error at this test server. I used it with svcutil to generate code but it gived error too. Normaly it should get information and generate the code example to call service.
Normally you can add authenticate parameters with SoapHeader or directly add to options in SoapClient consruct (if service authentication is basic authentication). I write below code according to your screenshot. But it gives timeout after long wait.
$wsdl = "https://slc.centershift.com/sandbox40/StoreService.svc?wsdl";
$client = new SoapClient($wsdl,array('trace' => 1,'soap_version' => SOAP_1_2));
$security = array(
'UserName' => array(
'UserName'=>'TestUser',
'Password'=>'TestPassword',
'SupportInteractive'=>false
)
);
$header = new SoapHeader('ChannelFactory','Credentials',$security, false);
$client->__setSoapHeaders($header);
$params = array();
$params['SiteID'] = 100000000;
$params['Channel'] = 999;
try {
$ret = $client->GetSiteUnitData($params);
print_r($ret);
}catch(Exception $e){
echo $e->getMessage();
}
__getFunctions works, because it prints functions defined in WSDL. There is no problem with getting WSDL information at first call. But real problem is communication. PHP gets WSDL, generates required SOAP request then sends to server, but server is not responding correctly. SOAP server always gives a response even if parameters or request body are not correct.
You should communicate with service provider, I think they can give clear answer to your questions.
Having worked with consuming .NET WS from PHP before I believe you would need to create objects from classes in PHP that matches the names that .NET is expecting. The WSDL should tell you the types it is expecting. I hope this assist with your path forward!
If the SOAP call works from a C# application, you could use Wireshark (with the filter ip.dst == 204.246.130.80) to view the actual request being made and then construct a similar request from php.
Check this answer to see how you can do a custom SOAP call.
There's also the option of doing raw curl requests, since it might be easier to build your xml body, but then you would have to parse the response yourself with simplexml.

CodeIgniter Web Services Client

I'm a newbie at CI, and I want to retrieve XML data from web services WebLogic, the server that is located at: http://services.insw.go.id/web-services/nsw?operation.invoke=getListGA . I want to to get the XML response from the server. How should I do this?
I made this function on controllers (resttest.php)
public function getRest()
{
$this->rest->initialize(array('server' => 'http://services.insw.go.id'));
$lartas = $this->rest->get('web-services/nsw',array('operation.invoke' => 'getListGA'),'xml');
die(var_dump($lartas));
}
Sometimes I get an error like "array(0) { }" and if I refresh, I get all HTML view, the same as when I browse to: http://services.insw.go.id/web-services/nsw?operation.invoke=getListGA
Am I wrong, or missing some step, or do you have any suggestion about how to change this code?
It looks as if your webservice is using SOAP (simple object access protocol). This is not REST. You'll want to use PHP's built in Soap extension with the SoapClient class. This way it's easy to post a XML "request" to that page which will return xml results rather than a html view (I assume).
Check Soap the soap extension is loaded on your server
Read about the SoapClient http://php.net/manual/en/class.soapclient.php
See if that webservice offers a WSDL (web service description language) file.
Create an instance of a soap client, using the wsdl and call the function you require.
Simple example from PHP.net
$client = new SoapClient("http://localhost/code/soap.wsdl");
$something = $client->HelloWorld(array());
echo $something->HelloWorldResult;
To get a xml response, you do not need Codeigniter. Specifically it provide WSDL. At http://services.insw.go.id/web-services/nsw you can find the example as
String wsdlUrl = "http://services.insw.go.id:80/web-services/nsw?WSDL";
So the WSDL API would be http://services.insw.go.id:80/web-services/nsw?WSDL
Then you can check this page to see how to install soap for your php.
Then you can get a xml response by the following code:
$client = new SoapClient('http://services.insw.go.id:80/web-services/nsw?WSDL');
//var_dump($client->__getFunctions());
$response = $client->getListGA();
echo $response;
these code do not need Codeigniter.
Note: $client->__getFunctions() will show you all functions that the WSDL support and the parameters the functions need.
Good luck

Intercept and analyze responses from Zend_Rest_Server

I used Zend Framework 1.11 to make a REST web service in PHP using the Zend_Rest_server class but I wasn't able to intercept and analyze the responses from Zend_Rest_server instances before these are sent to the clients.
To make the REST web service I use this snippet of code:
$server = new Zend_Rest_Server();
$server->setClass('Ws_dummy', 'dummy');
$server->handle();
Is there a method to log responses because I need to analyze them and I wasn't able to find a way to solve this need.
Thank you in advance for any help you can provide.
P.S. For example in SOAP web services I can do this:
$server->setReturnResponse(true);
$response = $server->handle();
or
$server->handle();
$response = $server->getLastResponse();
and analyze the responses
You can log Requests like this:
$writer = new Zend_Log_Writer_Stream('/path/to/logfile');
$logger = new Zend_Log($writer);
$logger->info( Zend_Debug::dump( $_REQUEST, 'Request-Dump', false );
You should do that before the Rest_Server handles the Request.
If you got further Questions - just ask :-)
Edit (added some useful information):
It may be helpful to understand that Zend_Debug::dump() method wraps the PHP function var_dump(). If the output stream is detected as a web presentation, the output of var_dump() is escaped using htmlspecialchars() and wrapped with (X)HTML pre-tags.
Edit #2:
You can return the Response of Zend_Rest_Server with:
$server->returnResponse(true);
before $server->handle().
Edit #3:
Be aware:
If I've read everything right you need to send the Headers by urself if you are returning the Response.
You can get/+set the Headers with:
$headers = $server->getHeaders();
foreach( $headers as $header ) header( $header );

How to get a SOAP post in PHP?

OK n00b here with SOAP,
Would like some clarification on how to use SOAP.
Question:
I have a Java JSP that posts a WSDL (Looks like XML format) to my PHP script, but how do I get this in the PHP script? The URL for the WSDL will be different every time.
I'm sure it's very simple but just don't see how or am I not understanding this correctly?
You can try something like this:
try {
if (!($xml = file_get_contents('php://input'))) {
throw new Exception('Could not read POST data.');
}
} catch (Exception $e) {
print('Did not successfully process HTTP request: '.$e->getMessage());
exit;
}
This will read the body of the POST request to the $xml variable and print an error if there is one.
Do you mean that the JSP sends the WSDL in a POST request to the PHP script?
If so, have a look at the $_POST array. If you specify exactly how the JSP sends it, I can probably help you more.
Anyway, once you have the WSDL url in a variable in your PHP script, you can have at it with the SoapClient class.
Assuming the best scenario:
$soapClient = new SoapClient($wsdlUrl, $soapOptions);
$soapClient->callYourMethod();
But you're likely to hit a lot of brick walls when using SOAP. Here's the documentation for SoapClient.
Edit:
So, the WSDL is POST-ed. Then, you could access it either by using $HTTP_RAW_POST_DATA if the XML string was sent as the HTTP body, or by using the $_FILES superglobal if the XML string was send as a part of a multipart request.
Something like this:
$wsdl = $HTTP_RAW_POST_DATA;
$wsdlUrl = 'data:text/xml;base64,' . base64_encode($wsdl);
$soapClient = new SoapClient($wsdlUrl);
Anyway, $HTTP_RAW_POST_DATA is only available if the php.ini setting always_populate_raw_post_data is turned on. Also, if the request was multipart, this setting is ignored, $HTTP_RAW_POST_DATA is not populated but you get access to the posted parts using $_FILES. And you may, indeed, use php://input instead of $HTTP_RAW_POST_DATA.
Also, data URIs may only be used when allow_url_fopen is turned on in php.ini.

Categories