File: examples/exampleViaPHP.php

Recommend this page to a friend!
  Classes of Chris Jeffries  >  PHP JSON RPC 2.0 Server  >  examples/exampleViaPHP.php  >  Download  
File: examples/exampleViaPHP.php
Role: Example script
Content type: text/plain
Description: Example using the PHP CURL Client
Class: PHP JSON RPC 2.0 Server
Handle to HTTP requests in JSON RPC v2.0 format
Author: By
Last change:
Date: 2 years ago
Size: 1,273 bytes
 

Contents

Class file image Download
<?php
function process() {
    if(isset(
$_POST['btn'])) {
        require_once(
'jsonrpcClient.class.php');
       
$request = json_decode($_POST['txt']);

        if (
json_last_error() > 0) {
            echo
'<p style="color:red;">'.json_last_error_msg() . '</p>';
        } else {
           
$cli = new jsonrpcClient();
            if(!isset(
$request->params )) {
               
$request->params = (object) [];
            }
           
$result = $cli->request('http://localhost/test/JSON_RPC/index.php', $request->method, $request->id, json_encode($request->params));
           
//$result = json_encode($result, JSON_PRETTY_PRINT);
           
echo "
        <script>
            alert(`
{$result}`);
        </script>
        "
;
        }
    }
}
?>
<!DOCTYPE HTML>
<html>
    <head>
        <title>JSON-RPC tester via PHP CURL</title>
    </head>
    <body>

        <h1>JSON-RPC tester via PHP CURL</h1>
        <p>This tester sends data back to the server which then uses
        the JSON-RPC PHP CLient to fire off a request to the server.
        It inserts the server response in the page that is sent back
        to the browser.
        </p>
        <form method=post>
            <textarea id=txt name=txt cols=50 rows=10>{
    "id":1,
    "method":"test",
    "params":{
        "p1":"one",
        "p2":"two"
    }
}
            </textarea>
            <br />
            <input type=submit name=btn id=btn value = 'Send!'>
        </form>
        <pre>
        <?php process(); ?>
</pre>
    </body>
</html>



For more information send a message to info at phpclasses dot org.