nusoap simple server - php

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)
?>

Related

how to convert a json response to a text output.?

I am using a platforme call ideamart to create some sms based applications.
They provide a api called subscription api.
deatils about ideamart subscription API
Guid to work with subscription API
I use below first code to request BaseSize details.is that code is correct.?
can I Display response using PHP echo() Function .? or any other way.?
<?php
include_once "definitions.php";
include_once "subscription.php";
$sub = new Subscription();
$AppId = "APP_00001";
$Password = "yuhst345";
$baseSize = $sub->getBaseSize($AppId,$Password);
?>
here is the getBaseSize function that includes in subscription.php
public function getBaseSize($applicationId, $password){
$arrayField = array(
"applicationId" => $applicationId,
"password" => $password);
$jsonObjectFields = json_encode($arrayField);
$resp=$this->sendBaseRequest($jsonObjectFields);
$response = json_decode($resp, true);
$statusDetail = $response['statusDetail'];
$statusCode = $response['statusCode'];
$status =$response['baseSize'];
return $status;
}
So, it looks like your getBaseSize() is returning an simply the base size. So, you should be able to do a
print $baseSize;
OR
echo $baseSize;
And you'll print out the string.

cannot access to Json with restler library

I create an rest api with php usring restler libary, its work well in my PC but when I uploaded the api to server, problems begin..
this function in my api
<?php
class user {
/**
#url GET /
*/
public function getAllInfo(){
$link = new PDO('mysql:host=localhost;dbname=---;charset=utf8','---','-----');
$handle = $link->prepare('select * from user');
$handle->execute();
$result = $handle->fetchAll(PDO::FETCH_OBJ);
if(empty($result)){
$err = new stdClass();
$err->error = "No user found";
return $err;
}
else{
return $result;
}
}
require_once 'restler.php';
$r = new Restler();
$r->setSupportedFormats('JsonFormat');
$r->addAPIClass('user');
$r->handle();
?>
when I access to this page nothing appear in browser
I thought the problem is this page can't access to require_once 'restler.php';
but I was wrong, because whan I print $r->handle(); like this print_r($r->handle());
this is what I see in browser
Luracast\Restler\EventDispatcher Object ( [listeners:Luracast\Restler\EventDispatcher:private] => Array ( ) [events:protected] => Array ( ) )
I don't know what is that or what I should do to print my json,
my database is full of data, its return json if my query doesn't have restler, but I need use restler with my code

Soap server not working in Laravel 5.2

I'm trying to create a soap server in laravel 5.2. This is my code:
Content of SoapController.php:
<?php namespace Giant\Http\Controllers;
class SoapController extends Controller {
public function __construct() {
parent::__construct();
ini_set('soap.wsdl_cache_enabled', 0);
ini_set('soap.wsdl_cache_ttl', 0);
ini_set('default_socket_timeout', 300);
ini_set('max_execution_time', 0);
}
public function server() {
$location = url('server'); // http://payment.dev/server
$namespace = $location;
$class = "\\Giant\\Http\\Controllers\\HelloWorld";
$wsdl = new \WSDL\WSDLCreator($class, $location);
$wsdl->setNamespace($namespace);
if (isset($_GET['wsdl'])) {
$wsdl->renderWSDL();
exit;
}
$wsdl->renderWSDLService();
$wsdlUrl = url('wsdl/server.wsdl');
$server = new \SoapServer(
url('server?wsdl'),
array(
'exceptions' => 1,
'trace' => 1,
)
);
$server->setClass($class);
$server->handle();
exit;
}
public function client() {
$wsdl = url('server?wsdl');
$client = new \SoapClient($wsdl);
try {
$res = $client->hello('world');
dd($res);
} catch (\Exception $ex) {
dd($ex);
}
}
}
class HelloWorld {
/**
* #WebMethod
* #desc Hello Web-Service
* #param string $name
* #return string $helloMessage
*/
public function hello($name) {
return "hello {$name}";
}
}
My wsdl file is: wsdl
And my routes:
Route::any('/server', 'SoapController#server');
Route::any('/client', 'SoapController#client');
And the result I get:
Internal Server Error
:(
I use piotrooo/wsdl-creator to generate wsdl. (There is no problem with that, It is working in laravel 4.2). And I have also tried nusoap and php2wsdl libraries.
My SoapClient is working well. Because it can get service from other soap servers in other urls, But I think my SoapServer can not work well.
I even get no errors in error-log file.
I just figured out wht was the problem:
The problem with log was that i was checking error-log in my www folder while laravel has its own log file. And using that i figured that i have problem with TokenMismatchException. Laravel's CsrfVerifyMiddleware would not letting me to request using soap.
I just added my url to "except" array inside CsrfVerifyMiddleware file.
Do not use two classes in one file
This is my experience from our project in which used Soap
This is SoapServerController . Paste wsdl file in root folder of your project
class SoapServerController extends Controller {
public function service() {
$server = new \SoapServer('http://' . request()->server('HTTP_HOST') . '/yourwsdlfile.wsdl');
$server->setClass('App\Http\Requests\somenamespace\SoapRequest');
$server->handle();
}
}
and in requests create class for requests like this:
class SoapRequest{
public function functionFromWsdl($args if you want) {
$parameters = (array) $args;
return with(new fooClass())->barMethod($parameters);
}
}
and route must be post:
Route::post('webservice','SoapServerController#service');
In laravel 5 all before statements have turned into middlewares (just like what is in django framework). And you need to implement using middlewares.

What's the syntax for oop nusoap_client call?

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'));

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