📜 ⬆️ ⬇️

Zend_Soap - How to Cook

Recently, I faced a question: to organize a SOAP Server for some actions. I wondered about the idea of ​​implementing this with Zend_Soap, it turned out to be extremely simple.

For example, we need 4 files:

  1. server.php - actually SOAP server itself
  2. wsdl.php - WSDL
  3. class.php - class with functions
  4. client.php - client, as an example of work

')

server.php


require_once 'Zend/Loader.php' ;
Zend_Loader::registerAutoload();

include ( 'class.php' );

$server = new Zend_Soap_Server( "http://localhost/soap/wsdl.php" );
$server->setClass( 'Server' );
$server->handle();
</code>

* This source code was highlighted with Source Code Highlighter .



wsdl.php


require_once 'Zend/Loader.php' ;
require_once 'class.php' ;
Zend_Loader::registerAutoload();

$wsdl = new Zend_Soap_AutoDiscover();
$wsdl->setUri( 'http://localhost/soap/server.php' );
$wsdl->setClass( 'Server' );
$wsdl->handle();


* This source code was highlighted with Source Code Highlighter .


server.php - As you can see, it does not carry any logic, like wsdl.php . Both of them refer to the class.php class, in which the call functions are already stored. Everything else does Zend_Soap for you.

class.php



<?php
class Server {

private $_db;

/** <br/>
* . <br/>
* . <br/>
*/ <br/>

function __construct() {
try {
$ this ->_db = new Zend_Db_Adapter_Oracle(array(
'username' => 'demo' ,
'password' => 'demo' ,
'dbname' => '(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(COMMUNITY=tcp.telekom.lv)(PROTOCOL=TCP)(Host=127.0.0.1)(Port=1521)))(CONNECT_DATA=(SID=XE)))'
));
} catch (Zend_Exception $e) {
return array( 'code' => 200,
'error' => 'Database connection error' );
}
}

/** <br/>
* Get Simple Demo Action <br/>
* <br/>
* @param array $userData <br/>
* @param array $requestData <br/>
* @return array <br/>
*/ <br/>

public function getSimpleAction($userData = array(), $requestData = array()) {

/** <br/>
* $userData = array('username' => '', 'password' => '') <br/>
* $requestData = array('value1' => '', 'value2' => '') <br/>
*/ <br/>

if (!$ this ->_checkCredentials($userData)) {
return array( 'code' => 500,
'error' => ' .' );
}

$array = array();

try {
/** <br/>
* <br/>
* Zend_Exception - . <br/>
* <br/>
* throw new Zend_Exception('Error :(', 100); <br/>
* <br/>
* $array , <br/>
* . <br/>
* <br/>
* $array['ok'] = 'true'; <br/>
*/ <br/>
} catch (Zend_Exception $e) {
return array( 'code' => 101,
'error' => 'Exception: ' . $e->getMessage());
}

return $array;
}

private function _checkCredentials($userData) {

/** <br/>
* <br/>
* return true/false <br/>
*/ <br/>

return true ;
}

/** <br/>
* . <br/>
*/ <br/>

function __destruct() {
$ this ->_db->closeConnection();
}
}


* This source code was highlighted with Source Code Highlighter .


The most important thing is not to forget about the comments to the main functions:

/**
* Get Simple Demo Action <br/>
* <br/>
* @param array $userData <br/>
* @param array $requestData <br/>
* @return array <br/>
*/
<br/>

* This source code was highlighted with Source Code Highlighter .


Describe the type of the parameter, be it string, array, or int. Or any other allowed.

client.php



require_once 'Zend/Loader.php' ;
Zend_Loader::registerAutoload();

$client = new Zend_Soap_Client( "http://localhost/soap/wsdl.php" );
$userdata = array( 'username' => 'demoUsername' , 'password' => 'demoPassword' );
$requestData = array( 'parameter' => 'value' );
print_r($client->getSimpleAction($userdata, $requestData));


* This source code was highlighted with Source Code Highlighter .


With the help of the client, we simply check the performance.

That's all, a small briefing on Zend_Soap is passed. In principle, on such a basis, you can build practically any SOAP applications.

Source: https://habr.com/ru/post/57571/


All Articles