一个非常简单的RPC服务

时间:2022-10-06 14:07:38

1.servicefunctions.php

<?php

class ServiceFunctions  {

    public    static  function getDisplayName($f,$l) {
$name='';
$name.=$f;
$name.=$l;
return $name;
}
public static function countWords($c){
return count($c);
}
} ?>

2.index.php

<?php
require 'servicefunctions.php'; if(isset($_GET['method'])) {
switch($_GET['method']) {
case 'countWords':
$response = ServiceFunctions::countWords($_GET['words']);
break;
case 'getDisplayName':
$response = ServiceFunctions::getdisplayName($_GET['first_name'], $_GET['last_name']);
break;
default:
$response = "Unknown Method";
break;
}
} else {
$response = "Unknown Method";
} header('Content-Type: application/json');
echo json_encode($response);
?>