我正在尝试在cakePHP 2中创建一个web服务,但我找到的所有信息都是cakePHP 1。 经过几天的搜索,我已经完成了这项工作,但它并没有奏效。 我创建了以下控制器:
class SoapsController extends AppController {
var $components = array('RequestHandler');
//listes des model utilisé
public $uses =array('Pompe', 'Serie', 'Fluide','Control');
function service() {
$this->layout = false;
$this->autoRender = false;
Configure::write('debug', 0);
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$server = new SoapServer('http://localhost/pps/soaps/wsdl');
$server->setClass("Soap");
$server->handle();
}
function wsdl() {
$this->layout = false;
Configure::write('debug', 0);
$this->RequestHandler->respondAs('xml');
}
}
将wsld文件放在view / soaps / wsdl.ctp中。 当我用客户呼叫网络服务时,它不起作用。 你能帮我这么做吗?
经过一番尝试后我更改了我的代码,但是当我用客户端调用webservice时我还有其他错误 我创建了一个messagescontroller
class MessagesController extends AppController {
public $uses = null; //for demostration purposes we do not need a model
function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow();
}
public $components = array(
'Soap' => array(
'wsdl' => 'myWSDLFile', //the file name in the view folder
'action' => 'service', //soap service method / handler
)
);
public function soap_wsdl(){
//will be handled by SoapComponent
}
public function soap_service(){
//will be handled by SoapComponent
}
/**
* A soap call 'soap_foo' is handled here
*
* @param Object $in The input parameter 'foo'
* @return Object
*/
public function soap_foo($in){
$obj = new stdClass();
$obj->out = 'foo response';
return $obj;
}
}
视图中的文件WSDL位置/ Elements / myWSDLFile.wsdl
创建messageModel
class Message extends AppModel {
public $useTable = false; // Ce model n'utilise pas une table de la base de données
}
在Controller / Component / SoapComponent.php中添加类soapComponent
App::import('core', 'AppHelper');
/**
* Soap component for handling soap requests in Cake
*
* @author Marcel Raaijmakers (Marcelius)
* @copyright Copyright 2009, Marcel Raaijmakers
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
class SoapComponent extends Component{
var $name = 'Soap';
var $components = array('RequestHandler');
var $controller;
var $__settings = array(
'wsdl' => false,
'wsdlAction' => 'wsdl',
'prefix' => 'soap',
'action' => array('service'),
);
public function initialize($controller, $settings = array()){
if (Configure::read('debug') != 0){
ini_set('soap.wsdl_cache_enabled', false);
}
$this->controller = $controller;
if (isset($settings['wsdl']) && !empty($settings['wsdl'])){
$this->__settings['wsdl'] = $settings['wsdl'];
}
if (isset($settings['prefix'])){
$this->__settings['prefix'] = $settings['prefix'];
}
if (isset($settings['action'])){
$this->__settings['action'] = is_array($settings['action']) ? $settings['action'] : array($settings['action']);
}
parent::initialize($controller);
}
public function startup(){
if (isset($this->controller->params['soap'])){
if ($this->__settings['wsdl'] != false){
//render the wsdl file
if ($this->action() == $this->__settings['wsdlAction']){
Configure::write('debug', 0);
$this->RequestHandler->respondAs('xml');
$this->controller->ext = '.wsdl';
$this->controller->render(null, false, DS . 'elements' . DS . $this->__settings['wsdl']); //only works with short open tags set to false!
} elseif(in_array($this->action(), $this->__settings['action'])) {
//handle request
$soapServer = new SoapServer($this->wsdlUrl());
$soapServer->setObject($this->controller);
$soapServer->handle();
//stop script execution
$this->_stop();
return false;
}
}
}
}
/**
* Return the current action
*
* @return string
*/
public function action(){
return (!empty($this->__settings['prefix'])) ? str_replace( $this->__settings['prefix'] . '_', '', $this->controller->action) : $this->controller->action;
}
/**
* Return the url to the wsdl file
*
* @return string
*/
public function wsdlUrl(){
return AppHelper::url(array('controller'=>Inflector::underscore($this->controller->name), 'action'=>$this->__settings['wsdlAction'], $this->__settings['prefix'] => true), true);
}
}
最后通过添加
更改路径文件Router::connect('/soap/:controller/:action/*', array('prefix'=>'soap', 'soap'=>true));
当我尝试将我的网络服务与客户端一起使用时如下
<?php
ini_set('soap.wsdl_cache_enabled', 0); //enable when in production mode, this does save a lot of time
$soapClient = new SoapClient('http://localhost/pps-soap/soap/messages/wsdl');
$param = new StdClass();
$param->in = 'param';
$foo = $soapClient->soap_foo($param);
var_dump($foo); //an object of StdClass with an 'out' field and the value 'foo response'
?>
我有一个肥皂错误,PHP致命错误:SOAP-ERROR:解析WSDL:无法加载
'http://localhost/pps-soap/soap/messages/wsdl'
我完全迷失了!!需要帮助
答案 0 :(得分:3)
在cakePHP中使用Soap服务。请执行以下步骤。 我们有2个选择。 1.使用WSDL 2.使用location和uri参数。我使用了uri参数:
在Cotroller中:
App::uses('Controller', 'Controller');
App::uses('Post', 'Model'); // Dont forgot to add this
class PostsController extends AppController {
var $components = array('RequestHandler');
function myservice() {
$this->layout = false;
$this->autoRender = false;
Configure::write('debug', 2);
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$server = new SoapServer(null, array(
'location' => 'http://localhost:81/webcake/posts/myservice', // Required for non wsdl mode
'uri' => 'http://localhost:81/webcake/posts/myservice', // Required for non wsdl mode
)
);
$server->setClass('Post');
$server->handle();
}
function testClient() {
$client = new SoapClient(null, array('location' => "http://localhost:81/webcake/posts/service",
'uri' => "http://localhost:81/webcake/posts/service"));
echo $client->myTest(5,5);
}
在模特:
class Post extends AppModel {
var $useDbConfig = 'soap';
var $useTable = false;
function myTest($x,$y) {
return $x+$y;
}
In oder to see the results open url: Ex: http://localhost:81/webcake/posts/testClient
答案 1 :(得分:0)
对我有用的是: 创建SoapController:
<?php
namespace App\Controller;
use Cake\Core\Configure;
use Cake\Event\Event;
use Cake\Log\Log;
use Cake\View\ViewBuilder;
class SoapController extends AppController {
var $components = array('RequestHandler');
public function initialize () {
parent::initialize();
$this->Auth->allow(["wsdl"]);
$this->Auth->allow(["service"]);
}
public function beforeFilter (Event $event) {
parent::beforeFilter($event);
if (Configure::read('Security')) {
$this->Security->config('unlockedActions', ['service']);
$this->Security->config('unlockedActions', ['wsdl']);
}
}
public function wsdl () {
try {
$builder = new ViewBuilder();
$builder->autoLayout(false);
$builder->template('Soap/wsdl');
$view = $builder->build();
$viewContent = $view->render();
header('Content-Type: text/xml');
echo $viewContent;
} catch (\Exception $e) {
Log::error("SOAP Server error: %s\n", $e->getMessage());
}
exit();
}
public function service () {
$this->viewBuilder()->layout = false;
$this->autoRender = false;
ini_set("soap.wsdl_cache_enabled", "0");
$server = new SoapServer('http://localhost/soap/wsdl');
$server->setClass("App\Controller\{yourClassWithSoapFunctions}");
$server->handle();
}
}
创建一个.ctp文件:src / Template / Soap / wsdl.ctp,并在其中包含您的SOAP XML配置。
创建一个控制器:yourClassWithSoapFunctions.php并包含您希望SOAP服务器包含的函数。
您现在应该可以发出SOAP请求。