X-Git-Url: http://git.onelab.eu/?p=plcapi.git;a=blobdiff_plain;f=php%2Fphpxmlrpc%2Fdemo%2Fserver%2Fdiscuss.php;fp=php%2Fphpxmlrpc%2Fdemo%2Fserver%2Fdiscuss.php;h=ac65209bd60f29a2aba902974d9c7a0596325b45;hp=0000000000000000000000000000000000000000;hb=21d187714285d9818fd94509b015ba069facb7ef;hpb=9bd41316bc9541bbedfe45377089d4e4927129b1 diff --git a/php/phpxmlrpc/demo/server/discuss.php b/php/phpxmlrpc/demo/server/discuss.php new file mode 100644 index 0000000..ac65209 --- /dev/null +++ b/php/phpxmlrpc/demo/server/discuss.php @@ -0,0 +1,99 @@ +decode($req); + $msgID = $n[0]; + $name = $n[1]; + $comment = $n[2]; + + $dbh = dba_open("/tmp/comments.db", "c", "db2"); + if ($dbh) { + $countID = "${msgID}_count"; + if (dba_exists($countID, $dbh)) { + $count = dba_fetch($countID, $dbh); + } else { + $count = 0; + } + // add the new comment in + dba_insert($msgID . "_comment_${count}", $comment, $dbh); + dba_insert($msgID . "_name_${count}", $name, $dbh); + $count++; + dba_replace($countID, $count, $dbh); + dba_close($dbh); + } else { + $err = "Unable to open comments database."; + } + // if we generated an error, create an error return response + if ($err) { + return new PhpXmlRpc\Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err); + } else { + // otherwise, we create the right response + return new PhpXmlRpc\Response(new PhpXmlRpc\Value($count, "int")); + } +} + +$getComments_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcString)); + +$getComments_doc = 'Returns an array of comments for a given ID, which +is the sole argument. Each array item is a struct containing name +and comment text.'; + +function getComments($req) +{ + $err = ""; + $ra = array(); + $encoder = new PhpXmlRpc\Encoder(); + $msgID = $encoder->decode($req->getParam(0)); + $dbh = dba_open("/tmp/comments.db", "r", "db2"); + if ($dbh) { + $countID = "${msgID}_count"; + if (dba_exists($countID, $dbh)) { + $count = dba_fetch($countID, $dbh); + for ($i = 0; $i < $count; $i++) { + $name = dba_fetch("${msgID}_name_${i}", $dbh); + $comment = dba_fetch("${msgID}_comment_${i}", $dbh); + // push a new struct onto the return array + $ra[] = array( + "name" => $name, + "comment" => $comment, + ); + } + } + } + // if we generated an error, create an error return response + if ($err) { + return new PhpXmlRpc\Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err); + } else { + // otherwise, we create the right response + return new PhpXmlRpc\Response($encoder->encode($ra)); + } +} + +$srv = new PhpXmlRpc\Server(array( + "discuss.addComment" => array( + "function" => "addComment", + "signature" => $addComment_sig, + "docstring" => $addComment_doc, + ), + "discuss.getComments" => array( + "function" => "getComments", + "signature" => $getComments_sig, + "docstring" => $getComments_doc, + ), +));