PHP & SoapClient: parameters missing from SOAP request - php

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!

Related

php - SoapClient only send First Parameter

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

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.

API request works from local machine, not on server

I have an interesting situation when calling the Shopify API. I use the standard procedure for calling the url and get the data, like this:
define('SHOPIFY_SHOP', 'myteststore.myshopify.com');
define('SHOPIFY_APP_API_KEY', 'xxxx');
define('SHOPIFY_APP_PASSWORD', 'yyy');
$shop_url = 'https://'.SHOPIFY_APP_API_KEY.':'.SHOPIFY_APP_PASSWORD.'#'.SHOPIFY_SHOP;
$response = Requests::get($shop_url.'/admin/products.json');
And I correctly get the response, parse the data and all works great. Now, when I put it to the actual server (Ubuntu 12.04), I noticed a weird message from the Spotify API:
[API] Invalid API key or access token (unrecognized login or wrong password)
I tried creating a new app, but still its the same. So the same file and the same set works on my machine, but not on the server. (only difference in the file is the path to requests library, require_once './Requests/library/Requests.php'; for Linux and require_once '..\Requests\library\Requests.php'; for Windows) As stated, I use the requests library and I assume there has to be some trick where the library (or something else) rewrites the URl and it doesn't get to Shopify correctly.
I tried using CURL with the URL directly, and it works that way as well. Can anyone point me what might be causing this?
Update: I moved to another library which solved the issue, but would like to know what was causing this since I had great experience with Requests up to this point.
I'm starting to use the same lib, and I stumbled upon something relevant right after finding this question:
https://github.com/rmccue/Requests/issues/142#issuecomment-147276906
Quoting relevant part:
This is an intentional part of the API design; in a typical use case,
you won't necessarily need data sent along with a request. Building
the URL for you is just a convenience.
Requests::get is a helper function designed to make GET requests
lightweight in the code, which is why there's no $data parameter
there. If you need to send data, use Requests::request instead
$response = Requests::request( 'http://httpbin.org/get', $headers, $data, Requests::GET, $options );
// GET is the default for type, and $options can be blank, so this can be shortened:
$response = Requests::request( 'http://httpbin.org/get', $headers, $data );
I couldn't figure why is this happening, it appears the Requests library is stripping the parameters from GET requests, so I moved to unirest library and this solved the issue.

complete noob, SOAP PHP, request with authentication, store in mySQL

OK, so I'm completely lost here! Been staring myself blind at the screen for two days and all the explanations/tutorials I can find on the internet assumes you've already worked with SOAP before.
Using SOAPui I was at least able to try out the request and see the result :) But I need to replicate this in PHP... e.g, make request in PHP using authentication credentials and then store the response in a mySQL table.
This is how the request looks like
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:DoThis>
<tem:licensId>[MYLICENSID]</tem:licensId>
<tem:licenskey>[MYLICENSKEY]</tem:licenskey>
<tem:guid>[AGUID]</tem:guid>
</tem:DoThis>
</soapenv:Body>
</soapenv:Envelope>
So I need to verify/authenticate the request (i guess)? But how do I do this with PHP?
Once I get the request to work, how will the response be presented? I would like to insert it into an existing mySQL DB. That is to say that this is my "end game"/the purpose of this. Retrieving information from external system, compare it to existing information in DB, and if new information exists, replace current or add new.
I tried
$wsdl = 'http://URLTOSERVICE/Export.svc?wsdl';
$client =
new SoapClient(
$wsdl,
array(
"tem:licensid" => "[MYLICENSID]",
"tem:licenskey" => "[MYLICENSKEY]",
"tem:guid" => "[AGUID]"
)
);
print_r($client->DoThis());
But the response is about as interesting as looking in to a blank paper for 2 days... i.e, returns nothing.
Like I said in the topic title and at the beginning. I have no experience with SOAP. So please, if some kind soul out there takes the time to answer, keep in mind that I need clear and precise explanations :)
And thank you to anyone taking the time to help me understanding this...
can you indicate the WSDL url? I would suggest you to try a WSDL to php converter to see more clearly how to call the SOAP Web service.
To set credentials as the request you indicate, you must use the Soapheader class, http://php.net/manual/fr/class.soapheader.php, and call the __setSoapheaders, http://www.php.net/manual/fr/soapclient.setsoapHeaders.php, before calling the operation.
Let me know if you need any help or a WSDL to php converter.

How to send complex types from PHP SoapClient to ASP.NET SOAP server?

Hello I'm having problems sending arrays, structs and arrays of structs from PHP to an ASP.NET SOAP server...
Anyone have a sollution for this? I've googled for days and any sollution worked for me. Perphaps I'm forgetting something...
There are examples of my code:
$client = new SoapClient($options);
$pCriteria = new stdClass();
$pCriteria->type=1;
$pCriteria->capacity=4;
//Test 1 (fail):
$resp = $client->GetRooms(array("pCriteria"=>$pCriteria));
//Test 2 (fail):
$resp = $client->GetRooms(array("pCriteria"=>new SoapVar($pCriteria, SOAP_ENC_OBJECT, "TCriteria", "http://www.w3.org/2001/XMLSchema")));
print_r($resp);
I don't know how to code functions that require an array of TCriteria (TCriteria[], TCriteria_Array type) either... i've tried sending the raw array, a SoapVar with SOAP_ENC_ARRAY encoding and TCriteria_Array type, ... but it does not work (the SOAP server becomes unavaiable and needs to be restarted).
I've tried creating classes for the complex types too, instead of stdClass, but not working.
I don't know where's the problem. The server admins cannot help me and I haven't found any sollution over internet. I'm a bit desperate hehe.
Can you help me please? Can you provide samples of code with the three cases (array of simple data, array of struct and struct) ? Thanks!
I had a similar situation with a PHP Soap Client communicating with a .NET Soap Server using WSDL 2.0. Here's one thing I discovered: When passing the information to the server, you must explicitly define the variable as a SoapVar object. So in your example above, change it to:
$pCriteria->type = new SoapVar(1, XSD_INT, 'xsd:int');
Passing an array is similar, essentialy you pass an array of SoapVars:
$pCriteria->type = array(new SoapVar(1, XSD_INT, 'xsd:int'), new SoapVar(2, XSD_INT, 'xsd:int', new SoapVar(3, XSD_INT, 'xsd:int'));`enter code here`
Also, you can use several built-in functions of the SoapClient to get some additional feedback on possible errors.
$client->__getLastRequest() //To display the XML that you sent to the server
$client->__getLastResponse() //to display the XML that is sent in response to your request
If you can get a copy of the expected WSDL format you can use the response from the above commands to determine what is going wrong. Usually you can access this from the URL that you pass to the SoapClient. So, for example, if the WSDL services URL is http://example.com/webservices/wvrgroupservice.asmx?WSDL, enter http://example.com/webservices/wvrgroupservice.asmx to view the functions and expected XML from that server.

Categories