【问题标题】:PHP webservice consumed from Javascript从 Javascript 使用的 PHP Web 服务
【发布时间】:2017-08-21 23:45:40
【问题描述】:

我需要建立一个 php 简单服务器和一个可以访问它的 javascript 小脚本。问题是我能找到的所有例子都不起作用。我只需要没有数据库的基本结构。只有将“hello world”字符串返回到 javascript 客户端或任何工作示例的方法(我可以从那时开始工作)。我不知道使用 Soap、nuSoap 还是 Rest 服务器哪个更好。

添加示例:

我有这个ws:

<?php

require_once('lib/nusoap.php');

class predictiveUrlsPreloadService {

    public function getUrls($type) {
        switch ($type) {
            case 'GA':
                return 'Google Analytics code';
                break;
            case 'AA':
                return 'Adobe Analytics code';
                break;
            default:
                break;
        }
    }
}

$server = new soap_server();
$server->configureWSDL("foodservice", "http://www.greenacorn-websolutions.com/foodservice");

$server->register("predictiveUrlsPreloadService.getUrls",
    array("type" => "xsd:string"),
    array("return" => "xsd:string"),
    "http://localhost/predictiveUrlsPreloadService/service.php",
    "http://localhost/predictiveUrlsPreloadService/service.php#getUrls",
    "rpc",
    "encoded",
    "Descripci");

@$server->service(file_get_contents("php://input"));

这个 PHP 客户端正在工作:

<script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous">
</script>

<?php

require_once('lib/nusoap.php');

$wsdl = "http://localhost/predictiveUrlsPreloadService/wsdl.wsdl";

$client = new nusoap_client($wsdl, true);
$error  = $client->getError();

if ($error) {
    print_r($error);
}

$result = $client->call("predictiveUrlsPreloadService.getUrls", array("type" => 'GA'));

if ($client->fault) {
    print_r($result);
} else {
    $error = $client->getError();
    if ($error) {
        print_r($error);
    } else {
        echo $result;
    }
}

?>

所有这一切都与它的 wsdl。我只需要知道如何使用 javascript 或 ajax 进行调用,如下所示:

$.ajax({
    url: 'http://localhost/predictiveUrlsPreloadService/service.php', 
    type: "predictiveUrlsPreloadService.getUrls",
    data: {
      'type' : 'AA'
    }, 
    success:  function (response) {
    console.dir(response);
  }
});

但它只返回 ws 信息。我该怎么做?

【问题讨论】:

  • 如果您找到示例但它们不起作用,请让它们起作用。如果他们做的比你想要的多,请简化它们。
  • 当然可以,但是我创建的示例只有服务器端并且不知道它们是否有效,因为我不知道如何构建客户端。

标签: javascript php ajax web-services soap


【解决方案1】:

这比我想象的要简单。

这是 PHP 中的 nusoap 服务器:

<?php
    require_once('nusoap.php');

    $server = new soap_server();
    $server->configureWSDL("PredictiveUrlPreloadXML", "urn:PredictiveUrlPreloadXMLwsdl");
    $server->wsdl->schemaTargetNamespace = "urn:PredictiveUrlPreloadXMLwsdl";

    function getUrls($type) {
        switch ($type) {
            case 'GA': 
                return 'GA code';
                break;
            case 'AA':
                return 'AA code';
                break;
            default:
                return null;
                break;
        }
    }

    $server->register(
        'getUrls',                                     
        array(                                     
            'type'          => 'xsd:string',
        ),               
        array(
            'return'        => 'xsd:string'
        ),             
        'urn:PredictiveUrlPreloadXMLwsdl',              
        'urn:PredictiveUrlPreloadXMLwsdl#getUrls',
        'rpc',                                     
        'encoded',                                      
        'Desc'    
    );

    @$server->service(file_get_contents("php://input"));

?>

这是 javascript 客户端:

<script>

    var soapMessage =
        '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
            '<soap:Body>'+
                '<getUrls xmlns="urn:PredictiveUrlPreloadXMLwsdl">'+
                    '<type>GA</type>'+
                '</getUrls>'+
            '</soap:Body>'+
        '</soap:Envelope>';

    $.ajax({
        url: "http://localhost/predictive_url_preload/ws.php",
        type: "POST",
        dataType: "xml", 
        contentType: "text/xml",
        data: soapMessage, 
        success: function(data, status, req) {
           //TODO
        },
        error: function (data, status, req) {
            console.log(req);
        }
    });

</script>

【讨论】:

    猜你喜欢
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多