title: PHP simple JSON-RPC
body: <p>I am trying to create JSON-RPC HTTP service. I am using POST method.</p>
  
  <p><strong>rest.php</strong></p>
  
  <pre><code>&lt;?php
  header('Content-Type: application/json');
  $input = file_get_contents("php://input");
  $json = json_decode($input);
  
  $output = $json-&gt;num + 10;
  echo json_encode($output);
  ?&gt;
  </code></pre>
  
  <p><strong>Input:</strong></p>
  
  <pre><code>{"num": 10 }
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>20
  </code></pre>
  
  <p>Now I am trying to implement more methods, so then I can use input like this:</p>
  
  <pre><code>{ 
      "method":"add", 
      "params": {
          "num1": 15, 
          "num2": 10
      }
  }
  </code></pre>
  
  <p>So I add function to <strong>rest.php</strong>:</p>
  
  <pre><code>public function add($n1, $n2) {
      return $n1 + $n2;
  }
  </code></pre>
  
  <p>Q: How to determine called method and execute it in <strong>rest.php</strong> with new input?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <pre><code>&lt;?php
  
  header('Content-Type: application/json');
  $input = file_get_contents("php://input");
  $json = json_decode($input);
  
  foreach ($json-&gt;params as $param) {
      $params[] = $param;
  }
  
  $output = call_user_func_array($json-&gt;method, $params);
  echo json_encode($output);
  
  function add($n1, $n2) {
      return $n1 + $n2;
  }
  </code></pre>