php - SoapClient only send First Parameter - php

so i want to ask about php SoapClient class. I have a running soap service using java that receive 2 parameter String name and String year. This is my php code
<?php
$soapClient = new SoapClient('http://localhost:9000/getBook?wsdl');
echo $soapClient->getBook("manifesto", "1999");
getBook will return name+year;
When I run that code on web, I always get manifestonull. It seems that my Java SOAP service didn't receive the year parameter.
But, when I try to use this piece of code on php interactive shell, I got manifesto1999 (My java soap service receive the second params!!)
Does anyone know how to fix this error? thank you so much

turn out, you need to set soap version to SOAP_1_1
$options = array(
'soap_version'=>SOAP_1_1
);
It works for me

Related

Passing tns:ArrayOfString to SOAP client

I'm trying to use the api from this service:
http://messagingws.payamservice.ir/SendSMS.asmx?WSDL
I'm using the php's SOAP client like this:
$client = new SoapClient("http://messagingws.payamservice.ir/SendSMS.asmx?WSDL");
$params=array();
$params['PortalCode']='code';
$params['UserName']='user';
$params['PassWord']='pass';
$params['Mobiles']=array('09123456789');
$params['Messages']=array('test');
$params['FlashSMS']=false;
$params['ServerType']=1;
$response = $client->__soapCall('MultiSMSEngine', array($params));
but I get "empty message" error, when I try with single mode it works correctly so it seems the problem is with Mobiles & Messages parameters that are tns:ArrayOfString
How can I correctly pass an array as tns:ArrayOfString for SOAP ?
thanks
First, you can call the method such as $client->MultiSMSEngine($params) instead of $client->__soapCall('MultiSMSEngine', array($params));.
If you keep your form of call, try $client->__soapCall('MultiSMSEngine', $params);
Finally, to ease you the call to any SOAP Web Service, I strongly advise you to use a WSDL to php generator such as PackageGenerator

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.

Convert SOAP XML response to PHP object or array

I have found this piece of PHP code:
<?php
$client = new SoapClient("http://localhost/code/soap.wsdl");
// Soap call with HelloWorld() method
$something = $client->HelloWorld(array('option1' => 'attribute1'));
// Convert object to array
$array = (array)$something;
I just want to know where soap.wsdl resides or from where we can download soap.wsdl file for PHP.
The location of the WSDL-file is going to depend on the SOAP API you're calling. Check their documentation.
The example code you provided of implies they're working locally, and you're hosting the WSDL on your own, local machine.
Research Edit
Apparently you can use the PHP soap client in non-WSDL mode. This is accomplished by passing in null as the param instead of the location of the wsdl file.
Also relevant, is this other SO result on non-WSDL mode

PHP & SoapClient: parameters missing from SOAP request

I am trying to fix an issue where it seems that SoapClient is not inserting the parameters into the SOAP request.
The simple test fails to a web service running on the same server as the website, which is PHP 5.3.13 on Windows 8 Server and IIS 7.1. This had been working as recently as two days ago, but is now failing. I am not aware of any changes to the server installation other than updates.
function simpleTest(){
$client = new SoapClient("http://server-sql:78/PCIWCTest/Service1.svc?wsdl", array( "trace" => 1 ));
$result = $client->__soapCall("GetData",
array('GetData'=> array('parameters'=> 'Hello')),
array('soapaction' => 'http://server-sql:78/PCIWCTest/GetData'));
$requXML = $client->__getLastRequest();
$respXML = $client->__getLastResponse();
debug($requXML);
debug($respXML);
}
The resulting request ($requXML) is:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/"><SOAP-ENV:Body><ns1:GetData/></SOAP-ENV:Body></SOAP-ENV:Envelope>
I am assuming that there should be the string 'Hello' between the
<SOAP-ENV:Body><ns1:GetData/></SOAP-ENV:Body>
tags, right?
I have been working with many different methods during troubleshooting like soap __call with the same results. Server has been restarted. I've tried all I can think and find to try.
Has anybody seen this before or have an idea?
In this case, I discovered that the parameter I was sending was not named exactly as the parameter in the Web Service code. By changing the above:
array('GetData'=> array('parameters'=> 'Hello')),
to:
array('GetData'=> array('value'=> 'Hello')),
the call worked normally and the parameter was no longer blank! I don;t know if this behavior is unique to this situation, but in my limited experience, I have not seen a situation where it mattered what the parameter was named on the other end.
At any rate, issue is solved. Hope this helps someone!

php soap empty output

I'm an experienced PHP programmer but I have actually no clue about SOAP. Now I must use it because my customer needs an automatic generating of DHL batch labels. I need some simple and effective help.
So I send a raw XML request to DHL, I have copied the message from their sample programm but I get always an empty result (no error). My PHP code goes like:
require_once('nusoap/lib/nusoap.php');
$endpoint = "https://test-intraship.dhl.com/intraship.57/jsp/Login_WS.jsp";
$client = new nusoap_client($endpoint, false);
$msg = $client->serializeEnvelope("
<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"
xmlns:cis=\"http://dhl.de/webservice/cisbase\" xmlns:de=\"http://de.ws.intraship\">
<soap:Header>
<cis:Authentification><cis:user>bzalewski</cis:user>
(...)
");
$result=$client->send($msg, $endpoint);
echo $result;
As said, the message is just copied so it must be OK.
I tried alternatively with another endpoint: http://test-intraship.dhl.com/ws/1_0/ISService/DE.wsdl, but also no result (no error).
Please help.
When using soap_client you do not need to pass raw XML. Instead you look at the WSDL and decide which web service function you want to call, and what parameters it needs. Then you create a soap client object, by passing the wsdl url and whether you want tracing or not (it helps to debug and stuff). Then use this soap client object to call whichever web service function you want to call. If there are parameters needed for the function call, pass them as an array. I have posted a sample code below which uses the WSDL you provided and calls its getVersion function. Note that this function does not need arguments so I am not passing anything. Hope this helps you get started..
<?
$client = new SoapClient('http://test-intraship.dhl.com/ws/1_0/ISService/DE.wsdl', array('trace' => 1));
$res = $client->getVersion();
print_r($res);
?>
This returns following value from the DHL web service:
stdClass Object
(
[Version] => stdClass Object
(
[majorRelease] => 1
[minorRelease] => 0
[build] => 14
)
)
Does the web server respond with a status 200? You said you get an empty response right?
Use this free GUI tool (http://webservicestudio.codeplex.com/) to make webservice call and visualize. You can easily load up the WSDL and start making calls.
By the way working 2 jobs and study is good stuff man! Keep it up.

Categories