How to call a EJB session bean from PHP? - php

Is there any way to call an EJB session bean from PHP? Are there any specific functions to do that?

Not really. If you can make CORBA calls, most container support CORBA as a protocol to talk to a remote EJB, but I wouldn't recommend it.
You'd have better luck exposing the EJB Session Bean call as a SOAP Web Service, or simply facade it with a Servlet and invoke that as an ad hoc web service.
Now, if you're running PHP within a Java EE server (Resin I believe can run PHP), then you might be able to invoke a Java call that can call an EJB Method.
But, frankly, the web service or ad hoc web facade is likely your best, and quickest path to success, assuming you're allowed to write them.

There are some libraries that do Java/Php bridge implementation, such as PHP/Java Bridge.
So if you were using IBM WebSphere (source):
<?php
// Get the provider URL and Initial naming factory
// These properties were set in the script that started the Java Bridge
$system = new Java("java.lang.System");
$providerUrl = $system->getProperty("java.naming.provider.url");
$namingFactory = $system->getProperty("java.naming.factory.initial");
$envt = array(
"javax.naming.Context.PROVIDER_URL" => $providerUrl,
"javax.naming.Context.INITIAL_CONTEXT_FACTORY" => $namingFactory,);
// Get the Initial Context
$ctx = new Java("javax.naming.InitialContext", $envt);
// Find the EJB
$obj = $ctx->lookup("WSsamples/BasicCalculator");
// Get the Home for the EJB
$rmi = new Java("javax.rmi.PortableRemoteObject");
$home = $rmi->narrow($obj, new Java("com.ibm.websphere.samples.technologysamples.ejb.stateless.basiccalculatorejb.BasicCalculatorHome"));
// Create the Object
$calc = $home->create();
// Call the EJB
$num = $calc->makeSum(1,3);
print ("<p> 1 + 3 = $num </p>");
?>

Related

how to access .net webservice classes with php?

I have a .net web service which provide multiple methods and public classes.
I want to use these classes in php.
How can I do it?
this is my web service: http://10.2.3.74/AdaptorRepeater/BillPatientService.asmx?WSDL
for example I want access to AdmissionVO class and its properties
$client = new soapclient('http://10.2.3.74/AdaptorRepeater/BillPatientService.asmx?WSDL');
$header = new SoapHeader('http://sepas.behdasht.gov.ir/','HeaderMessageVO',$HeaderMessageVO,false);
$client->__setSoapHeaders($header);
$send = $client->SavePatientBillSecure($params);
I want for example this :
$client->AdmissionVO->MedicalRecordNumber = '1234';
Using SOAP Call You can access any Dot.Net Web service
You need to have Dot net web service WSDL path
now
<?php
//turn off WSDP caching if not in a production environment
$ini = ini_set("soap.wsdl_cache_enabled","0");
//instantiate the SOAP client
$client = new SoapClient("http://example.com/dotnet.asmx?WSDL");
echo $client->GetAllContactNames()->GetAllContactNamesResult;
//...optional additional code goes here...
?>

Creating a web service in PHP

I would like to create a web service in PHP which can be consumed by different consumers (Web page, Android device, iOS device).
I come from a Microsoft background so am confortable in how I would do it in C# etc. Ideally I would like to be able to provide a REST service which can send JSON.
Can you let me know how I can achieve this in PHP?
Thanks
Tariq
I developed a class that is the PHP native SoapServer class' REST equivalent.
You just include the RestServer.php file and then use it as follows.
class Hello
{
public static function sayHello($name)
{
return "Hello, " . $name;
}
}
$rest = new RestServer(Hello);
$rest->handle();
Then you can make calls from another language like this:
http://myserver.com/path/to/api?method=sayHello&name=World
(Note that it doesn't matter what order the params are provided in the query string. Also, the param key names as well as the method name are case-insensitive.)
Get it here.
I would suggest you go for Yii it is worth of learning. You can easily establish it in this.
Web Service. Yii provides CWebService and CWebServiceAction to simplify the work of implementing Web service in a Web application. Web service relies on SOAP as its foundation layer of the communication protocol stack.
Easiest way in PHP is to use GET/POST as data-in and echo as data-out.
Here's a sample:
<?php if(empty($_GET['method'])) die('no method specified');
switch($_GET['method']){
case 'add': {
if(empty($_GET['a']) || empty($_GET['b'])) die("Please provide two numbers. ");
if(!is_numeric($_GET['a']) || !is_numeric($_GET['b'])) die("Those aren't numbers, please provide numbers. ");
die(''.($_GET['a']+$_GET['b']));
break;
}
}
Save this as test.php and go to http://localhost/test.php?method=add&a=2&b=3 (or wherever your webserver is) and it should say 5.
PHP does have native support for a SOAP server ( The SoapServer class manual shows it) and I've found it pretty simple to use.
Creating a REST style API is pretty easy if you use a framework. I don't want to get into a debate about which framework is better but CakePHP also supports output as XML and I'm pretty sure others will as well.
If you're coming from a Microsoft background just be careful about thinking about "datasets". They are a very specific Microsoft thing and have been a curse of mine in the past. It's probably not going to be an issue for you, but you may want to just see the differences between Microsoft and open implementations.
And of course PHP has a native json_encode() function.
You can check out this nice RESTful server written for Codeigniter, RESTful server.
It does support XML, JSON, etc. responses, so I think this is your library.
There is even a nice tutorial for this on the Tutsplus network -
Working with RESTful Services in CodeIgniter
You can also try PHP REST Data Services https://github.com/chaturadilan/PHP-Data-Services
You can use any existing PHP framework like CodeIgniter or Symfony or CakePHP to build the webservices.
You can also use plain PHP like disscussed in this example

Troubles with Zend Rest Client when trying to call a Web Service developed in ASP and sending a parameter as array

I have this call to a Web Service that is developed with ASP:
$endpoint = Zend_Registry::get('config')->endpoint->services->myService;
$client = new Zend_Rest_Client($endpoint);
$client->userId($adminUserId);
$client->otherIds($otherIds);
$result = $client->get();
But when I try to call the service the parameter 'otherIds' is not been taken by the WS.
That is because the first called function apparently (as I grok from the source code) is chosen for the "method" parameter. The Zend REST server seems to take this format. I suggest for other servers to feed a dummy method to the client, so the first call should be
$client->Dummy();
After that, the arguments are set.

AMFPHP how to get object in php that is dispatched by flex?

i am working on AMFPHP and i had this problem
-> i am using XAMPP server with AMFPHP gate way for flex and this is working well for returning values from PHP
->the problem is how can i get the object in php which i passed to the pap class using flex code is here:-
connection.objectEncoding = ObjectEncoding.AMF3;
connection.connect("http://localhost/flashservices/gateway.php");
var obj:Employ = new Employ(inputs.ename.text,inputs.occu.text,inputs.adder.text,inputs.ph.text );
var responder = new Responder(recvdata,recverror);
connection.call("Employ.employrecord.employdata.insertrecord",responder,obj);
the obj is a employ class object and i want to get it in php class but how "need help"
thanx in advance
AMFPHP will handle the heavy lifting of converting the Flash/Flex data structures into PHP equivalents. At most you should just define a service handler and provide the appropriate arguments for the method so AMF can pass the client-side data to the PHP handler.
There's a decent example of both sides of the process here.

How to easily consume a web service from PHP

Is there available any tool for PHP which can be used to generate code for consuming a web service based on its WSDL? Something comparable to clicking "Add Web Reference" in Visual Studio or the Eclipse plugin which does the same thing for Java.
In PHP 5 you can use SoapClient on the WSDL to call the web service functions. For example:
$client = new SoapClient("some.wsdl");
and $client is now an object which has class methods as defined in some.wsdl. So if there was a method called getTime in the WSDL then you would just call:
$result = $client->getTime();
And the result of that would (obviously) be in the $result variable. You can use the __getFunctions method to return a list of all the available methods.
I've had great success with wsdl2php. It will automatically create wrapper classes for all objects and methods used in your web service.
I have used NuSOAP in the past. I liked it because it is just a set of PHP files that you can include. There is nothing to install on the web server and no config options to change. It has WSDL support as well which is a bonus.
This article explains how you can use PHP SoapClient to call a api web service.
Say you were provided the following:
<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:int="http://thesite.com/">
<x:Header/>
<x:Body>
<int:authenticateLogin>
<int:LoginId>12345</int:LoginId>
</int:authenticateLogin>
</x:Body>
</x:Envelope>
and
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<authenticateLoginResponse xmlns="http://thesite.com/">
<authenticateLoginResult>
<RequestStatus>true</RequestStatus>
<UserName>003p0000006XKX3AAO</UserName>
<BearerToken>Abcdef1234567890</BearerToken>
</authenticateLoginResult>
</authenticateLoginResponse>
</s:Body>
</s:Envelope>
Let's say that accessing http://thesite.com/ said that the WSDL address is:
http://thesite.com/PortalIntegratorService.svc?wsdl
$client = new SoapClient('http://thesite.com/PortalIntegratorService.svc?wsdl');
$result = $client->authenticateLogin(array('LoginId' => 12345));
if (!empty($result->authenticateLoginResult->RequestStatus)
&& !empty($result->authenticateLoginResult->UserName)) {
echo 'The username is: '.$result->authenticateLoginResult->UserName;
}
As you can see, the items specified in the XML are used in the PHP code though the LoginId value can be changed.
Well, those features are specific to a tool that you are using for development in those languages.
You wouldn't have those tools if (for example) you were using notepad to write code. So, maybe you should ask the question for the tool you are using.
For PHP: http://webservices.xml.com/pub/a/ws/2004/03/24/phpws.html
HI I got this from this site : http://forums.asp.net/t/887892.aspx?Consume+an+ASP+NET+Web+Service+with+PHP
The web service has method Add which takes two params:
<?php
$client = new SoapClient("http://localhost/csharp/web_service.asmx?wsdl");
print_r( $client->Add(array("a" => "5", "b" =>"2")));
?>

Categories