How to read .Net web service in PHP? - php

I have successfully read WSDL form SoapClient. I tried following to pass value to web method and read results:
$client = new SoapClient("restaurant.asmx?wsdl");
$param = array('search_keywords'=>$search_text);
$result = $client->__soapCall('getRestaurantsASP',$param);
print_r($result);
I got this error:
Fatal error: Uncaught SoapFault exception: [soap:Server]
System.Web.Services.Protocols.SoapException: Server was unable to process request. --->
System.Data.SqlClient.SqlException: Invalid object name 'basic_info_table'. at
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean
breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, Boolean breakConnection) at
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler,
SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject
stateObj) at System.Data.SqlClient.SqlDataReader.ConsumeMetaData() at
System.Data.SqlClient.SqlDataReader.get_MetaData() at
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior
runBehavior, String resetOptionsString) at
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,
RunBehavior runBehavior in E:\webareas\gs937\aspservice.php on line 73
How can I send parameters to web method and retrieve values?

As suggested by DaveRandom, problem seems to be in service itself rather problem is in SQL query used for service.
Try using SoapUI (you can download it from http://sourceforge.net/projects/soapui/files/) and execute the service method from there.
SoapUI is a neutral client to test the webservices.
Hope it will help.

I had to add:
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
<add name="HttpSoap"/>
</protocols>
</webServices>
in Web.config file to get things working.

Related

SoapClient call returns 500 Internal Server Error

I am trying to connect to an API using PHP and its built-in SoapClient. I have checked against the url I was given through the ill-formatted documents the client gave and $client->__getFunctions() returns a list of three functions. HelloWorld($name), which responds with Hello ~name~, shows me that I am communicating with the server through the SoapClient call and the URL is correct.
However, when I try to access one of the other methods that __getFunctions() gives me, even after copy/pasting the XML from the docs and putting in my own credentials, I am still being given an Internal Server Error faultstring and 500 as faultcode from the SoapFault object.
I am sure that it is my own XML string that is causing the issue but I cannot for the life of me figure out how. Reaching out to the API provider directly hasn't proven helpful. This is my first time dealing with Soap/Web Services so I am unsure of where to go from here.
I did wget http//xxx.xxx.xxx?wsdl and it returned me what looks like a valid XML response, the same one I get when I go directly to the url in the browser. What should I be looking into in order to solve this issue? All of the past API's I've dealt with have been JSON/RESTful so I feel out of my element trying to debug PHP errors.
Edit
I have slowly deleted parts of my method call and parts of my XML string, trying to trigger a different error or something in order to find what I need to fix. What I have found is that by not passing in my XML string, I get a valid response from the $client->FunctionCall(...). It's an "this isn't right" message but it's a message! In fact, passing that function ANYTHING for the xml parameter causes the 500 http faultcode/faultstring. Does this mean that my XMl is poorly formatted or does it mean that there is an issue on their end handling requests?
Second Edit
If I make my $client decleration as follows, I get the faultstring Could not connect to host
$opts = array(
'ssl' => array('ciphers'=>'RC4-SHA')
);
$client = new SoapClient($CREDS['orderingWSDL'], array (
"encoding"=>"ISO-8859-1",
'stream_context' => stream_context_create($opts),
'exceptions'=>true,
));
I am getting more confused the longer I try to fix this.
Sometimes a 500 status coming from a SOAP service could be a SoapFault exception being thrown. To help your troubleshooting, you'll want to be able to inspect both your request XML, and the response XML.
Put your code in try/catch blocks, and use $client->__getLastRequest() and $client->__getLastResponse() to inspect the actual XML.
Example:
$client = new SoapClient('http//xxx.xxx.xxx?wsdl', array('soap_version'=>SOAP_1_1,'trace' => 1,'exceptions' => true));
try {
$response = $client->someFunction();
var_dump($response);
} catch (Exception $e) {
var_dump($e->getMessage());
var_dump($client->__getLastRequest());
var_dump($client->__getLastResponse());
}

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.

PHP SoapClient - how to get request to server and response

I try to create script for communication with WS-Security (for the first time) in PHP:
require ('WSSoapClient.php');
$soapclient = new WSSoapClient('https://katastr.cuzk.cz/trial/dokumentace/ws22/wsdp/vyhledat_v22.wsdl');
$soapclient->__setUsernameToken('username', 'password');
$params = array('katastrUzemiKod'=>693936, 'kmenoveCislo'=>1385);
$response = $soapclient->__soapCall('najdiParcelu', $params);
var_dump($response);
And this script failed:
PHP Fatal error: Uncaught SoapFault exception: [wsse:InvalidSecurity]
Error on verifying message against security policy Error code:1000 in
/home/jura/bin/WSSoapClient.php:75
Is any way to see, what this script exactly sent to server and what was the response?
The problem is the $soapclient->__setUsernameToken('username', 'password') function is expecting a 3rd parameter of either 'PasswordDigest' or 'PasswordText'.
This is badly handled in the WSSoapClient class. It should really throw an exception if the passwordType argument is missing. What it tries to do is this:
// $this->generateWSSecurityHeader() returns an empty string if 3rd parameter missing.
$this->__setSoapHeaders($this->generateWSSecurityHeader());
If PHP is in strict mode like mine you would get these warnings:
PHP Warning: SoapClient::__setSoapHeaders(): Invalid SOAP header...
Warning: SoapClient::__setSoapHeaders(): Invalid SOAP header...
You need to figure out the password type and pass that as your 3rd argument in the setUsernameToken() function.

Amazon s3 | MalformedXML Status Code: 400

I am receiving the following error when using the putObject() function of the aws-sdk for php:
PHP Fatal error: Uncaught Aws\S3\Exception\MalformedXMLException: AWS Error Code: MalformedXML,
Status Code: 400, AWS Request ID: 7E36EB414B2A9436, AWS Error Type: client,
AWS Error Message: The XML you provided was not well-formed or did not validate against our published schema,
User-Agent: aws-sdk-php2/2.5.4 Guzzle/3.8.1 curl/7.26.0 PHP/5.5.19-1~dotdeb.1
thrown in /sites/sitename/vendor/aws/aws-sdk-php/src/Aws/Common/Exception/NamespaceExceptionFactory.php on line 91
Even though I am receiving the error, the process continues to run successfully and the files are moved to s3. I know this more than likely has something to do with the parameters that I am including in the function but I'm not sure what else needs to be there as the aws api for php only shows the 'bucket' and 'key' parameters as being required. My configuration is as follows:
$config = array();
$config['Bucket'] = IMAGE_BUCKET;
$config['Key'] = $imageName;
$config['SourceFile'] = $sourceImageDir;
$config['ACL'] = 'public-read';
$config['ContentLength'] = filesize($sourceImageDir);
$response = $s3->putObject($config);
If I knew what format the service was requiring I'm sure I could grab the request that I'm sending after it's turned into an xml string and compare it with that, but I can't seem to find it. If it exists. I would assume there is such a thing?
Thanks to the comment from Michael I was able to track the issue down further. It turns out that the issue was actually coming from a call to deleteObjects(). These commands are being executed within a shell script on the cli and it turns out there was another call preceeding the last call to putObject(). It was trying to send empty arrays to the s3 service...which would obviously return the given MalformedXML Status code.

twitter api returning xml on one server, empty string on another

I have a php Twitter app which lets you mark tweets as favorite.
I'm doing something like this:
$fav = $twitter->createFavorite("xml", $get_id);//handles api call (using curl)
$fav_result = new SimpleXMLElement($fav);
On my localhost and on one online server all goes well: the tweet is marked as favorite, and the api call returns xml. On another online server, the tweet is also marked as favorite, but php gives an error: Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML'
On the second server, I seem to get an empty string as return value. When I look at the http status codes, when all is well I get a 200, but when things go wrong I get a status code of 0.
When I check the curl_error it says "Failed to open/read local data from file/application"
I think it has to do something with my server configuration. Does anyone have an idea what might be causing this?
I found the solution here: http://www.milk-hub.net/blog/2008/08/26/curl_error_26
Since no separate postvars are sent, you have to explicitly set the to an empty string:
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, '');

Categories