Building a JSON-RPC Server in Go and a PHP Client with Common Pitfalls
This article explains how to quickly create a JSON-RPC server using Go, implement a PHP client for verification, and discusses typical issues such as parameter formatting that arise because Go's jsonrpc package does not fully follow the JSON-RPC specification.
JSON-RPC is a stateless, lightweight remote procedure call protocol that uses JSON for data exchange. Go provides both rpc and jsonrpc packages, the latter enabling cross‑platform communication. This article demonstrates how to quickly build a JSON‑RPC server in Go and a client in PHP, and records common pitfalls encountered.
1. Implementing the jsonrpc_server in Go
2.1 Import packages
import (
"net/rpc"
"net"
"log"
"net/rpc/jsonrpc"
"fmt"
)2.2 Define RPC methods
type Edwin int
func (this *Edwin) Add(args map[string]float64, res *float64) error {
*res = args["num1"] + args["num2"]
return nil
}
func (this *Edwin) Multi(args map[string]interface{}, res *float64) error {
*res = args["num1"].(float64) * args["num2"].(float64)
return nil
}2.3 Register handler and start listening
rpc.Register(new(Edwin))
l, err := net.Listen("tcp", ":11223")
if err != nil {
log.Fatalln("listen error:", err)
}
for {
conn, err := l.Accept()
if err != nil {
log.Fatalln("accept failed:", err)
}
fmt.Println("jsonrpc server start listen on 11223...")
go func(conn net.Conn) {
fmt.Println("a new connection is coming...")
jsonrpc.ServeConn(conn)
}(conn)
}3. Implementing the jsonrpc_client in PHP
class JsonRpc {
private $conn;
function __construct($host, $port) {
$this->conn = fsockopen($host, $port, $errno, $errStr, 2);
}
public function call($method, $params) {
$err = fwrite($this->conn, json_encode([
'method' => $method,
'params' => array($params),
'id' => 1,
]));
if (empty($err)) {
return false;
}
stream_set_timeout($this->conn, 0, 300);
$line = fgets($this->conn);
if ($line === false) {
return NULL;
}
fclose($this->conn);
return json_decode($line, true);
}
}
$client = new JsonRPC("127.0.0.1", 11223);
$client2 = new JsonRPC("127.0.0.1", 11223);
$ret = $client->Call("Edwin2.Multi", array("num1"=>14, "num2"=>20));
$ret2 = $client2->Call("Edwin2.Add", array("num1"=>14, "num2"=>20));
var_export($ret);
var_export($ret2);4. Verification and common issues
Running the Go server prints listening messages, and the PHP client returns results such as array('id'=>1,'result'=>280,'error'=>NULL). A common pitfall is passing 'params' => $params directly; Go's jsonrpc implementation expects params to be an array, otherwise unmarshalling fails with “json: cannot unmarshal object into Go value of type [1]interface {}”. This reveals that Go's jsonrpc does not fully follow the JSON‑RPC specification.
5. Internal processing flow
(Diagram omitted in text.)
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Beike Product & Technology
As Beike's official product and technology account, we are committed to building a platform for sharing Beike's product and technology insights, targeting internet/O2O developers and product professionals. We share high-quality original articles, tech salon events, and recruitment information weekly. Welcome to follow us.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
