2017-08-21 8 views
0

PHPのシンプルなサーバーとそれにアクセスできるjavascriptの小さなスクリプトを構築する必要があります。問題は、私が見つけることができるすべての例が機能しないことです。私はデータベースなしで基本的な構造が必要です。 "hello world"という文字列をjavascriptのクライアント側に返すメソッドまたは実際の例です(私はその時点から作業できます)。 Soap、nuSoapまたはRestサーバーを使用する方が良いかどうかはわかりません。JavascriptからPHPウェブサービスを利用しました

例を追加しました:そのWSDLと

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

?> 

本のすべて:

<?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クライアント作業:

私はこのWSを持っています。

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

をそれだけで、WSの情報を返します。そして、私はこれだけのようなJavaScriptやAjaxで呼び出しを作る方法を知っておく必要があります。どうやってやるの?

+0

あなたが例を発見したと、彼らはそれらを動作させる、動作しない場合。彼らがあなたが望む以上に多くのことをするならば、それらを単純化してください。 – ryantxr

+0

もちろん、私はクライアント側を構築する方法がわからないため、サーバー側のみを作成し、動作するかどうかわかりません。 – Noark

答えて

0

私は思ったよりも簡単でした。

これは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> 
関連する問題