Consuming a VB.NET web service from PHP - php

I'm relatively new to PHP, and totally new to VB.NET / Web Services / SOAP / XML, and i'm having trouble to make my PHP communicate with the VB.NET web service.
This is my PHP script:
<?php
$client = new SoapClient("http://10.0.0.2/wsteste/Service1.asmx?wsdl");
$param = array("usuario" => "name", "senha" => "test");
$response = $client->__soapCall("HelloWorld", $param);
print_r($response);
?>
And here is the VB.NET asmx.
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Service1
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function HelloWorld(ByVal usuario As String, ByVal senha As String) As String
Return usuario & " - " & senha
End Function
End Class
And here is what printed on the browser:
stdClass Object ( [HelloWorldResult] => - )
It was supposed to return name - test, wasn't it?

I think that the PHP SOAP Client is passing the parameters without the names. So usuario nor senha means nothing to the HelloWorld method.
I would try something like
$client->HelloWorld(array("usuario"=>"name", "senha"=>"test"));
Haven tested though.
EDIT
From this question Call asp.net web service from PHP with multiple parameters
Pass your params like this
$params->usuario = 'name';
$params->senha = 'test';
$client->HelloWorld($params);

Related

Issue with String response in Retrofit

I am using Retrofit in my android app to communicate with my server. In one of my server calls I am expecting a String response from server. So, I declare a callback which expects a string value. Callback<String>. In the php, I echo a string. Say echo "test"; When I hit the url in browser, the echo works as expected test. But in my android app, failure callback is called.
I tried changing the php to echo "\"test\"";
On browser : "test"
On android : success callback is called.
I solved it by declaring a variable.
Php:
$result = "test";
echo $result;
Browser: test
Android : success callback is called.
My question is, is this how Retrofit works? Or am I doing anything wrong? Also, to solve this is there any way other than declaring a variable?
Callback<String> does not make a lot of sense in the context of retrofit. By default retrofit operates using GSON.
What you are actually waiting for from the server is json deserialized into a POJO (simple java object).
Lets say you have a data model (POJO) like:
public class User {
public final String name;
}
Then you'd use a callback like this Callback<User>. And from the server you should do: echo '{ "name" : "Simon" }';
In your success callback you will have an instance of User class with the name field set to 'Simon'.
More on this here: http://square.github.io/retrofit/

WCF - "Incomplete" WSDL with reference to another WSDL

I am using SoapClient to send some data from a PHP site to a .Net WCF service.
This is my (shortened for clarity) code:
$wsdl = '/var/www/libraries/MyWsdl.xml';
$myClient = new SoapClient($wsdl);
and later, the actual call:
try {
$res = $myClient->Foo($someParameter);
}
catch(SoapFault $e){
//...
}
catch(Exception $e){
//...
}
This works great when everything is online, and the error handling works if the destination server is down on the time Foo is called.
Problem is that the SoapClient constructor fails, if the destination server is down, even though i've provided it with a static XML file with the WSDL (in oppose to a URL like "http://www.destination.com/MyService?wsdl").
I believe this is happening because the WSDL contains a reference to another WSDL:
<wsdl:import namespace="http://MyCompany.Services" location="http://www.destination.com/MyService?wsdl=wsdl0"/>
This other WSDL contains the definitions of the call parameters.
So, how can I "Inline" the second "sub-WSDL" inside the original one?
Will this allow me to create a SoapClient without initiating a connection to the destination server?
This is my service definition:
[ServiceContract(Namespace = "http://MyCompany.Services")]
public interface IMyService
{
[OperationContract]
[XmlSerializerFormat(Style = OperationFormatStyle.Rpc, Use = OperationFormatUse.Literal)]
string Foo(string myParameter);
}
You can use the single WSDL-file extension from here: http://wcfextras.codeplex.com/
Note that when using .net4.5 there should be no need for it, as the default metadata endpoint now also generates single WSDL-files in addition to the linked versions.
To do that simply add "?singleWSDL” to the URI (source: http://msdn.microsoft.com/en-us/library/dd456789(v=vs.110).aspx)

using functions from internal SOAP interface via php

I received a manual to internal SOAP interface of my partner. It says:
MyPARTNER web services are provided in the form of a SOAP interface. The service is available in this URL: https://justsomeurl.com:435/soap
then some bla bla about authorization etc. and then a part about Accessible Methods:
pull()
The PULL method is used for pulling data from the database. The method
receives a unique data based parameter under an internal name
requestXML. This parameter contains data in a structured XML format.
String pull(String requestXML)
The XML contains data required to make the request, and the response
data is sent back.
then some other methods, error codes, it's not important here...
The problem is that I'm totally unexperienced in SOAP so I don't know how to use this interface via PHP. I've tried to find some examples, tutorials and I am now little bit more informed about SOAP and its functionality but still haven't found any advice about how to use interface like this...
thanx for any help
Php comes with PHP SOAP libraries, that usually are included and enabled after a common php installation.
Yuo are asked to biuld the client part of the webservice pattern. Your partner should provide you the .wsdl of the web service. The wsdl describes the avialble method, the parameters they need and what they return.
Tipically parameters and return values are array structures
This could be a skeleton for your code:
//build a client for the service
$client = new SoapClient("partner.wsdl");
//$client is now a sort of object where you can call functions
//prepare the xml parameter
$requestXML = array("parameter" => "<xml>Hello</xml>");
//call the pull function this is like
$result = $client->__soapCall("pull", $requestXML );
//print the value returned by the web service
print_r($result);
Here follows a non-wsdl example
First the location paramater is the address the SOAP request will be sent to.
The uri parameter is the target namespace of the SOAP service. This is related to xml namespaces.
A sample code for you could be:
//for URI specification you should watch your partners documentation. maybe also a fake uri (like mine) could work
//build a client for the service
$client = new SoapClient(null, array(
'location' =>
"https://justsomeurl.com:435/soap",
'uri' => "urn:WebServices",
'trace' => 1 ));
// Once built a non-wsdl web service works as a wsdl one
//$client is now a sort of object where you can call functions
//prepare the xml parameter
$requestXML = array("parameter" => "<xml>Hello</xml>");
//call the pull function this is like
$result = $client->__soapCall("pull", $requestXML );
//print the value returned by the web service
print_r($result);
Here a useful link: http://www.herongyang.com/PHP/SOAP-Use-SOAP-Extension-in-non-WSDL-Mode.html

Passing Variables from php to a WCF Web service, variables are blank

I have a WCF Service, the interfaces work fine when connecting with a c# application but when I connect using a PHP application all variables passed to the service are null.
This is the PHP code used to connect to the service and send the data.
$SelectedFolder = $_REQUEST['folder'];
var_dump($SelectedFolder);
try
{
$client = new SoapClient('http://localhost:8663/Service.svc?wsdl');
$Files = $client->GetAllLatestVersionsString($SelectedFolder);
}
The var dump displays the following
string 'Pictures/Sample/' (length=16)
This is the service code
[OperationContract]
List<VersionedFileDataModel> GetAllLatestVersionsString(string partUri);
I've tried passing a static value instead of a variable and both times the value received by the service is null.
Thanks in Advance for any help,
Matt
Fixed it, I was required to pass the variables through using an array of params, for the example I posted I had to change the code to this.
try
{
$client = new SoapClient('http://localhost:8663/Service.svc?wsdl');
$params->partUri = $SelectedFolder;
$Files = $client->GetAllLatestVersionsString($params);
}

Any Javascript and PHP AJAX Interface generation suggestions, like thrift?

I am basically looking for Apache Thrift, but to talk between JavaScript over Ajax and PHP.
I know Thirft generates both, but to my knowledge the JavaScript code must talk over JSONProtocol, of which the protocol isn't yet wrote in PHP.
Are there any other alternatives that can suggested?
If you are unfamiliar with Thrift, this is a simple(ish) definition of what i need:
Consider this as the generic interface definition language (IDL), where I setup a User object, an AuthenticationResult result object, and method named UserCommands.Authenticate();
struct User {
1: number id,
2: string firstName,
3: string lastName
}
struct AuthenticationResult {
1: number currentTime,
2: User user
}
service UserCommands {
AuthenticationResult Authenticate(1:string username, 2:string password)
}
I run a program or something, it creates JS and PHP libraries based on the above.
Then, in JS, I could call (with helpful typehinting).
var myAuthResult = UserCommands.Authenticate('myUser', 'myPass');
alert ("My first name is : " + myAuthResult.user.firstName);
And in PHP, I would setup a method in a UserCommands class like this:
function Authenticate($username, $password) {
$myUser = new User();
$myUser->firstName = "Fred";
$myUser->lastName = "Thompson";
$myAuthResult = new AuthenticationResult ();
$myAuthResult->currentTime = date("U");
$myAuthResult->user = $myUser;
return $myAuthResult;
}
The benefits are that PHP can return native objects and JS can expect to receive its own native objects.
Type hinting for available methods are provided through out, with expected params and return results.
Any thoughts would be appreciated!
First of all, there're json_encode and json_decode functions in php.
Secondly, there's a serialize/unserialize for native php types
I don't understand, though, which you mean under "... of which the protocol isn't yet wrote in PHP."
Also, there's a Haxe langauge, which can be "compiled" into both PHP and JavaScript (and some other languages)

Categories