Examples

Example #1 Yar Server Example

<?php

/* assume this page can be accessed by http://example.com/operator.php */

class Operator {

    
/**
     * Add two operands
     * @param interge 
     * @return interge
     */
    
public function add($a$b) {
        return 
$this->_add($a$b);
    }

    
/**
     * Sub 
     */
    
public function sub($a$b) {
        return 
$a $b;
    }

    
/**
     * Mul
     */
    
public function mul($a$b) {
        return 
$a $b;
    }

    
/**
     * Protected methods will not be exposed
     * @param interge 
     * @return interge
     */
    
protected function _add($a$b) {
        return 
$a $b;
    }
}

$server = new Yar_Server(new Operator());
$server->handle();
?>

Example #2 Access the server in borwser(GET request)

The above example will output something similar to:

Yar Server Info

Example #3 Yar Client Example

<?php
$client 
= new yar_client("http://example.com/operator.php");

/* call directly */
var_dump($client->add(12));

/* call via call */
var_dump($client->call("add", array(32)));


/* __add can not be called */
var_dump($client->_add(12));
?>

The above example will output something similar to:

int(3)
int(5)
PHP Fatal error:  Uncaught exception 'Yar_Server_Exception' with message 'call to api Operator::_add() failed' in *

Example #4 Yar Concurrent Client Example

<?php
function callback($ret$callinfo) {
    echo 
$callinfo['method'] , " result: "$ret "\n";
}

/* register async call to remote services */
Yar_Concurrent_Client::call("http://example.com/operator.php""add", array(12), "callback");
Yar_Concurrent_Client::call("http://example.com/operator.php""sub", array(21), "callback");
Yar_Concurrent_Client::call("http://example.com/operator.php""mul", array(22), "callback");

/* sent all request and wait for response */
Yar_Concurrent_Client::loop();
?>

The above example will output something similar to:

mul result: 4
sub result: 1
add result: 3

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

Was genau bedeutet "Vibe Coding"? Ein tiefgehender Blick für Entwickler

In der Welt der Softwareentwicklung gibt es unzählige Wege, wie man an ein Projekt herangeht. Manche schwören auf strikte Planung, andere auf bewährte Algorithmen und wieder andere lassen sich von etwas ganz anderem leiten: ihrem Gefühl. ...

admin

Autor : admin
Category: Software & Web-Development

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial