0
我需要建立一個PHP簡單的服務器和一個JavaScript腳本中的小腳本誰可以訪問它。問題是我能找到的所有例子都不起作用。我只需要沒有數據庫的基本結構。只有一個將「hello world」字符串返回給javascript客戶端或任何工作示例的方法(我可以從這一點開始工作)。我不知道使用Soap,nuSoap還是Rest服務器會更好。從Javascript消耗PHP web服務
舉例說:
我有這樣的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信息。我怎樣才能做到這一點?
如果您發現例子,他們不工作,使他們的工作。簡化他們,如果他們做比你想要的更多。 – ryantxr
當然,但是我創建的例子只有服務器端,並且不知道它們是否工作,因爲我不知道如何構建客戶端。 – Noark