Soap 服务器在 Laravel 5.2 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37412965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:51:39  来源:igfitidea点击:

Soap server not working in Laravel 5.2

phpweb-serviceslaravelsoapwsdl

提问by Ali Farhoudi

I'm trying to create a soap server in laravel 5.2. This is my code:
Content of SoapController.php:

我正在尝试在 laravel 5.2 中创建一个肥皂服务器。这是我的代码:
内容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

我的 wsdl 文件是:wsdl

And my routes:

而我的routes

Route::any('/server', 'SoapController@server');
Route::any('/client', 'SoapController@client');

And the resultI get:

我得到的结果

Internal Server Error

:(
I use piotrooo/wsdl-creatorto 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.

:(
我使用piotrooo/wsdl-creator生成wsdl。(没有问题,它在laravel 4.2中工作)。我也试过nusoap和php2wsdl库。
我的SoapClient运行良好。因为它可以得到服务来自其他 url 中的其他肥皂服务器,但我认为我的 SoapServer 无法正常工作。
我什至在错误日志文件中没有错误。

采纳答案by Ali Farhoudi

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.

我刚刚发现问题出在哪里:
日志的问题是我正在检查 www 文件夹中的错误日志,而 laravel 有自己的日志文件。并使用它我认为我有 TokenMismatchException 问题。Laravel 的 CsrfVerifyMiddleware 不会让我请求使用肥皂。
我刚刚将我的 url 添加到 CsrfVerifyMiddleware 文件中的“except”数组。

回答by Sirbito X

In laravel 5 all before statements have turned into middlewares (just like what is in django framework). And you need to implement using middlewares.

在 laravel 5 中,所有 before 语句都变成了中间件(就像 django 框架中的一样)。并且您需要使用中间件来实现。

回答by Faradox

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

不要在一个文件中使用两个类 这是我在使用 Soap 的项目中的经验 这是 SoapServerController 。将 wsdl 文件粘贴到项目的根文件夹中

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(); } }

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

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

Route::post('webservice','SoapServerController@service');