What's the syntax for oop nusoap_client call? - php

I don't find the syntax for an object oriented nusoap_client call:
What's the correct syntax if I registered a class method to the server. I know the code for the server but fail to implement the correct client.
I have this server:
<?php
require_once "lib/nusoap.php";
require_once 'SampleData.php';
class SoapServer {
protected $server;
public function __construct() {
$this->server = new soap_server();
$server->register("SampleData.getSampleData");
$server->service($HTTP_RAW_POST_DATA);
}
}
?>
How do I call this from my SoapClient?
$result = $this->client->call("SampleData.getSampleData", array("category" => "sample"));
Seems not to work.

I would avoid using SoapServer as a class name, may conflict with the standard extension (PHP Manual SoapServer).
Why are you wrapping soap_server with SoapServer anyway? Instead try:
$server = new soap_server();
$server->register("SampleData.getSampleData");
$server->service($HTTP_RAW_POST_DATA);
Client call should be like this:
$client = new soapclient('URL');
$result = $client->call("SampleData.getSampleData", array("category" => "sample"));
print_r($result);

//Define the client
$client = new soapclient('http:webservice.com');
//call the method
$result=$client->call('method',array('param1,param2'));

Related

Zend xmlrpc error

I have this error with Zend (v1) XmlRpc client :
Uncaught exception 'Zend_XmlRpc_Client_FaultException' with message 'Failed to parse response'.
It's the error status 651.
The call never reaches the class/method requested, it seems that the call is not fired like it was blocked or something. I'm in debug on the method called and it's not triggered.
PHP version is 5.4.
EDIT
Here is the code :
Caller class :
require_once 'library/Zend/XmlRpc/Client.php';
class FrontService
{
private $client;
public function __construct($xmlRpc)
{
$this->client = new Zend_XmlRpc_Client($xmlRpc);
}
public function call($name, $params = array())
{
return $this->client->call($name, $params);
}
}
Call to the class :
$this->_fs = new FrontService(HM_Config::getParam("amf-url"));
$editos = $this->_fs->call('getEdito',$params_home);
Code called :
include_once realpath(dirname(__FILE__) .'/..')
.'/application/bootstrap.php';
require_once '_config.php';
require_once 'DirectDbConnectionV2.php';
class FrontGateway extends DirectDbConnectionV2
{
public static $smStatic;
function __construct()
{
mysql_query("SET NAMES 'utf8'");
$this->sm = self::$smStatic;
$this->log = new Log();
$this->log->set_file('amfDbConnection');
$this->log->write('construct bordel');
}
}
FrontGateway::$smStatic = $sm;
$controllerManager = $sm->get('EditoWebsiteMVC\ControllerManager');
$controllerManager->run('EditoWebsite\Controller\UIGateway', 'xmlRpc');
Code that should be executed :
namespace EditoWebsite\Controller;
use EditoWebsiteMVC\AbstractController;
use EditoWebsiteMVC\ViewRender\CLI as CLIViewRender;
use EditoWebsiteMVC\ViewRender\HTMLTemplate as HTMLTemplateViewRender;
use Zend_XmlRpc_Server as XmlRpcServer;
class UIGatewayController extends AbstractController
{
public function xmlRpcAction()
{
$svr = new XmlRpcServer();
$svr->setClass('FrontGateway');
echo $svr->handle();
exit;
}
}
The code in the getEdito method from the DirectDbConnectionV2 is never reached.
Is there something I need to enable on the server ? or a port that I need to open ?
EDIT EDIT
I should mention that the code is working on another server that I've access to, is there anything I should compare / check to maybe solve that issue ?
Thanks a lot
I don't think there's enough info to even make a guess, but let me try.
I've faced similar issue, where requests sent never reached the server. It turned out to be that Zend_Http_Client_Adapter_Socket adapter is binding to an IPv6 and due to routing issues request never got to the server.
In the end, what solved the issue was:
$client->getAdapter()->setStreamContext(array(
'socket' => array('bindto' => '0:0'),
));
where $client is an instance of Zend_Http_Client.
Again, it's just a shot in the dark, but could be worth trying. :)
Edit:
In your case, you should add following to FrontService constructor:
$this->client->getHttpClient()->getAdapter()->setStreamContext(array(
'socket' => array('bindto' => '0:0'),
));
Edit Edit:
Since back on 1.9.0 there's no getAdapter on Zend_Http_Client, you have to create adapter yourself and pass it to http client:
public function __construct($xmlRpc)
{
$this->client = new Zend_XmlRpc_Client($xmlRpc);
$adapter = Zend_Http_Client_Adapter_Socket();
$adapter->setStreamContext(array(
'socket' => array('bindto' => '0:0'),
));
$this->client->getHttpClient()->setAdapter($adapter);
}
Cheers.

SOAP server in Symfony 2 project

Im trying to implement WebService using SOAP in Symfony 2 framework. On server side im setting class to my server (setClass() method) becouse i need to make more operations on one instance of class.
If i used setObject for soapCalls, it works good,
use path\to\Test;
public function indexAction()
{
$server = new \SoapServer(null, array('uri' => "http://test-uri.cz/"));
$server->setObject($this->get('my_service'));
$response = new Response();
$response->headers->set('Content-Type', 'text/xml');
ob_start();
$server->handle();
if (ob_get_length() > 0) {
$response->setContent(ob_get_clean());
}
return $response;
}
but doesn`t work with setClass method.
use path\to\Test;
public function indexAction()
{
$server = new \SoapServer(null, array('uri' => "http://test-uri.cz/"));
$server->setClass('Test');
$response = new Response();
$response->headers->set('Content-Type', 'text/xml');
ob_start();
$server->handle();
if (ob_get_length() > 0) {
$response->setContent(ob_get_clean());
}
return $response;
}
Can somebody gives me any hints?
If you want to use SoapServer::setClass, you must state each parameters of the constructor of your service, and state the class name with a full namespaced string :
$server->setClass('Acme\YourBundle\SoapManager', $arg0, $arg1, $arg2 /*, ... */);
Finally it works... I had bad namespace and in SOAP server is need to use setPersistence() method after setClass().

nusoap simple server

Hi i am using this code for nusoap server but when i call the server in web browser it shows message "This service does not provide a Web description" Here is the code
<?
//call library
require_once ('lib/nusoap.php');
//using soap_server to create server object
$server = new soap_server;
//register a function that works on server
$server->register('hello');
// create the function
function hello($name)
{
if(!$name){
return new soap_fault('Client','','Put your name!');
}
$result = "Hello, ".$name;
return $result;
}
// create HTTP listener
$server->service($HTTP_RAW_POST_DATA);
exit();
?>
An help ...
Please change your code to,
<?php
//call library
require_once('nusoap.php');
$URL = "www.test.com";
$namespace = $URL . '?wsdl';
//using soap_server to create server object
$server = new soap_server;
$server->configureWSDL('hellotesting', $namespace);
//register a function that works on server
$server->register('hello');
// create the function
function hello($name)
{
if (!$name) {
return new soap_fault('Client', '', 'Put your name!');
}
$result = "Hello, " . $name;
return $result;
}
// create HTTP listener
$server->service($HTTP_RAW_POST_DATA);
exit();
?>
You didnt Define namespace..
Please see simple example here :-
http://patelmilap.wordpress.com/2011/09/01/soap-simple-object-access-protocol/
The web browser is not calling the Web service - you could create a PHP client :
// Pull in the NuSOAP code
require_once('lib/nusoap.php');
// Create the client instance
$client = new soapclient('your server url');
// Call the SOAP method
$result = $client->call('hello', array('name' => 'StackOverFlow'));
// Display the result
print_r($result);
This should display Hello, StackOverFlow
Update
To create a WSDL you need to add the following :
$server->configureWSDL(<webservicename>, <namespace>);
You can also use nusoap_client
<?php
// Pull in the NuSOAP code
require_once('lib/nusoap.php');
// Create the client instance
$client = new nusoap_client('your server url'); // using nosoap_client
// Call the SOAP method
$result = $client->call('hello', array('name' => 'Pingu'));
// Display the result
print_r($result)
?>

How to execute a web service using php

I need to execute a web service from a php page
The web service is located in the following url
https://www.agemni.com/AgemniWebservices/service1.asmx
The Web Service uses a SOAP protocol to exchange messages.
The WSDL info can be located at https://www.agemni.com/AgemniWebservices/service1.asmx?WSDL
The function in that service that we need to use is ValidateEntity
//ValidateEntity("username", "password", "companyID", 2, keys, values)
So , how can i execute this web service and get result from my php page
A simple example, hope it helps...
$service1 = new SoapClient('https://www.agemni.com/AgemniWebservices/service1.asmx');
//here you instanciate your object with those properties
$entity = new Entity();
$entity->strUsername = 'José';
$entity->strPassword = '123';
$entity->strCompanyName = 'Somethin';
$entity->0 //because your type is int
$res = $service1->ValidateEntity($entity);//here you send the information to your service's method, if I'm not mistaken, it must be a object
$res->ValidateEntityResult;//this is the return of your service.
As I said, it is really simple but works.
See soap calls help from php.net:
http://www.php.net/manual/en/soapclient.soapcall.php
You need to use PHP's SOAP libraries...
http://www.php.net/manual/en/soapclient.soapcall.php
For https WebServices you need to enable openssl extension. The WS use .net it means that the class use type hinting so you need to create the ValidateEntity class, here's the code:
$ws = new soapclient('https://www.agemni.com/AgemniWebservices/service1.asmx?wsdl');
class ValidateEntity {
public $strUsername,
$strPassword,
$strCompanyName,
$objecttype;
}
$parameters = new ValidateEntity();
$parameters->strUsername = 'username';
$parameters->strPassword = 'password';
$parameters->strCompanyName = 'company';
$parameters->objecttype = 1;
echo '<pre>';
print_r($ws->ValidateEntity($parameters));
echo '</pre>';

Register class methods with nusoap

I've a php class and i want to use it with Nusoap. Can I register the class method's that already exists inside the nusoap with the register command?
Sample :
Here we register a function that we defined inside this script. But if we've a class that we maybe develop months ago and we want to use it as a webservice using the WSDL. Is there a way to register the methods of that class so that Nusoap creates a WSDL of it's stucture (methods inside)?
require_once("nuSOAP/lib/nusoap.php");
$server = new soap_server();
$namespace = "http://localhost/nusoapSCRIPT/index.php";
$server->wsdl->schemaTargetNamespace = $namespace;
$server->configureWSDL("SAMPLE");
$server->register('HelloWorld');
function HelloWorld()
{
return "Hello, World!";
}
Well here's how i solve this... maybe someone can try an approach in another way.
[File : index.php]
require_once "nuSOAP/lib/nusoap.php";
require_once "myClass.class.inc.php";
$namespace = "http://localhost/html/nusoap/index.php";
// create a new soap server
$server = new soap_server();
// configure our WSDL
$server->configureWSDL("Class Example");
// set our namespace
$server->wsdl->schemaTargetNamespace = $namespace;
// register the class method and the params of the method
$server->register("myClass.ShowString"
,array('name'=>'xsd:string')
,array('return'=>'xsd:string')
,$namespace,false
,'rpc'
,'encoded'
,'Sample of embedded classes...'
);
//
// Get our posted data if the service is being consumed
// otherwise leave this data blank.
$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA'])
? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
// pass our posted data (or nothing) to the soap service
$server->service($POST_DATA);
exit();
and the class code.
[File 'myClass.class.inc.php']
class myClass{
public function __construct(){
}
public function ShowString($mens){
return "\n##Remote Class :".__CLASS__."\n##Remote Method : ".__METHOD__."\n## mSG :{$mens}";
}
}
I also create a soap client in c# and it consumes correctly the soap service.
Hope this help!
You can call the method using getProxy()
$proxy->className__methodname

Categories