public function yar_client_asyncAction() { Yar_Concurrent_Client::call("http://yaf.zhaoquan.com/rpc/yar", "getOne", array(), "RpcController::callback"); Yar_Concurrent_Client::call("http://yaf.zhaoquan.com/rpc/yar", "getList", array(array('cost' => 8)), "RpcController::callback"); Yar_Concurrent_Client::loop(); return false; }
public function apiProxy() { if (!is_array($this->oSetVO->oProxyRequest->data)) { throw new Exception('Request is not a array.'); } $haveYar = FALSE; foreach ($this->oSetVO->oProxyRequest->data as $id => $this->oRequestEntity) { switch ($this->oRequestEntity->method) { case Constants_ConcurrentEnum::METHOD_REST_GET: case Constants_ConcurrentEnum::METHOD_REST_POST: $this->restClient->setApi($this->oRequestEntity->api); $this->restClient->setMethod($this->oRequestEntity->method); $this->restClient->setData($this->oRequestEntity->params); $this->restClient->go(); $tmp = $this->restClient->getBody(); self::$responseResult[$this->oRequestEntity->identifier] = $tmp; break; case Constants_ConcurrentEnum::METHOD_RPC_YAR: $haveYar = TRUE; $params = array('identifier' => $this->oRequestEntity->identifier); $this->oRequestEntity->params = array(array_merge($params, $this->oRequestEntity->params)); Yar_Concurrent_Client::call($this->oRequestEntity->api, $this->oRequestEntity->function, $this->oRequestEntity->params, "Services_Concurrent_Transaction::callBack"); break; } } if ($haveYar) { Yar_Concurrent_Client::loop("Services_Concurrent_Transaction::callBack", "Services_Concurrent_Transaction::errorCallBack"); } return self::$responseResult; }
/** * 调用服务端接口 * @param $server Api server * @param $api 接口 * @param $params 参数 * @param $callBack 回调 * @param $openSign 开启签名 */ public function call($api, $params, $callBack = null, $openSign = true) { if ($openSign) { $params['sign'] = $this->getSign($params); } if ($callBack === null) { $client = new \Yar_Client($this->server); return $client->{$api}($params); } $this->callNum++; $this->callBack = $callBack; return \Yar_Concurrent_Client::call($this->server, $api, $params, array($this, 'ApiClientCallBack')); }
/** * 并发请求接口 * * @param string $server 请求的server * @param string $api 请求的方法 * @param array $params 请求参数 * @param null $callback 回调函数 两个参数:$retval-> 请求返回数据,$callinfo-> 请求返回信息 * * @return bool */ public function call($server, $api = '', $params = array(), $callback = null) { extract($this->parseLooseArgumentOrder(func_get_args()), EXTR_OVERWRITE); if (empty($server)) { $server = $this->server; } $params['token'] = S::getToken($params); if (is_callable($callback)) { try { \Yar_Concurrent_Client::call($server, $api, array($params), $callback); } catch (\Yar_Client_Exception $e) { print_r($e); } } return false; }
<?php /** * [CodeJm!] Author CodeJm[codejm@163.com]. * * * $Id: client.php 2014-08-28 15:58:19 codejm $ */ function callback($retval, $callinfo) { echo '<pre>'; var_dump($retval); echo '</pre>'; } function error_callback($type, $error, $callinfo) { error_log("error" . $error); } Yar_Concurrent_Client::call("http://127.0.0.1/user/api3/", "some_method", array("parameters1"), "callback"); Yar_Concurrent_Client::call("http://127.0.0.1/user/api3/", "some_method", array("parameters2")); // if the callback is not specificed, // callback in loop will be used Yar_Concurrent_Client::call("http://127.0.0.1/user/api3/", "some_method", array("parameters3"), "callback", "error_callback", array(YAR_OPT_PACKAGER => "json")); //this server accept json packager Yar_Concurrent_Client::call("http://127.0.0.1/user/api3/", "some_method", array("parameters4"), "callback", "error_callback", array(YAR_OPT_TIMEOUT => 1)); //custom timeout Yar_Concurrent_Client::loop("callback", "error_callback"); //send the requests,
protected function yarConcurrentRequest($url, $function, $p, $callback = 'callback') { $url = $this->_buildURL($url); Yar_Concurrent_Client::call($url, $function, $p, $callback); }
<?php /* $c = new Yar_Client("http://localhost:8000/api.php"); $result = $c->test("first", 'second'); var_dump($result); die; */ function callback($retval, $callinfo) { var_dump(['rtn' => $retval, 'callinfo' => $callinfo]); } /** * 并行时不可以使用全部使用localhost , 它不支持本地hosts 解析? */ Yar_Concurrent_Client::call("http://localhost:8000/api.php", "test", array("param1"), "callback"); Yar_Concurrent_Client::call("http://localhost:8000/api.php", "test", array("param2"), "callback"); Yar_Concurrent_Client::call("http://localhost:8000/api.php", "test", array("param3"), "callback"); Yar_Concurrent_Client::call("http://127.0.0.1:8000/api.php", "test", array("param4", "2rd"), "callback"); Yar_Concurrent_Client::loop(); //send
$client = new Yar_Client("http://localhost/server.php"); /* the following setopt is optinal */ $client->SetOpt(YAR_OPT_CONNECT_TIMEOUT, 1); /* call remote service */ $result = $client->some_method("parameter"); print_r($result . '<br>'); //Concurrent call function callback($retval, $callinfo) { print_r($retval . ' / callback / '); var_dump($callinfo); print_r('<br>'); } function error_callback($type, $error, $callinfo) { var_dump($type); var_dump($error); var_dump($callinfo); error_log($error); } Yar_Concurrent_Client::call("http://localhost/server.php", "some_method", array("Concurrent 1"), "callback"); // if the callback is not specificed, callback in loop will be used Yar_Concurrent_Client::call("http://localhost/server.php", "some_method", array("Concurrent 2")); //this server accept json packager Yar_Concurrent_Client::call("http://localhost/server.php", "some_method", array("Concurrent 3"), "callback", "error_callback", array(YAR_OPT_PACKAGER => "json")); //custom timeout Yar_Concurrent_Client::call("http://localhost/server.php", "some_method", array("Concurrent 4"), "callback", "error_callback", array(YAR_OPT_TIMEOUT => 1)); //send the requests, //the error_callback is optional Yar_Concurrent_Client::loop("callback", "error_callback");
/** * 执行远程调用任务 * @param $uri * @param $method * @param array $parameters * @param callable $callback * @return $this|mixed */ public function call($uri, $method, array $parameters = array(), callable $callback = null) { if (!$this->_sync) { $rpc = new \Yar_Client($uri); if (!empty($this->_opt) && is_array($this->_opt)) { foreach ($this->_opt as $key => $value) { $rpc->setOpt($key, $value); } } $result = call_user_func_array(array($rpc, $method), $parameters); if (!empty($callback) && is_callable($callback)) { $result = call_user_func($callback, $result); } return $result; } \Yar_Concurrent_Client::call($uri, $method, $parameters, $callback); return $this; }
<?php /** * [CodeJm!] Author CodeJm[codejm@163.com]. * * * $Id: client.php 2014-08-28 15:58:19 codejm $ */ function callback($retval, $callinfo) { echo '<pre>'; var_dump($retval); echo '</pre>'; } function error_callback($type, $error, $callinfo) { error_log("error" . $error); } Yar_Concurrent_Client::call('http://127.0.0.1/user/api3/', 'some_method', array('parameters1'), 'callback'); Yar_Concurrent_Client::call('http://127.0.0.1/user/api3/', 'some_method', array('parameters2')); // if the callback is not specificed, // callback in loop will be used Yar_Concurrent_Client::call('http://127.0.0.1/user/api3/', 'some_method', array('parameters3'), 'callback', 'error_callback', array(YAR_OPT_PACKAGER => 'json')); //this server accept json packager Yar_Concurrent_Client::call('http://127.0.0.1/user/api3/', 'some_method', array('parameters4'), 'callback', 'error_callback', array(YAR_OPT_TIMEOUT => 1)); //custom timeout Yar_Concurrent_Client::loop('callback', 'error_callback'); //send the requests,
public function callMulti($data = array()) { //remote调用执行开始时间 Runtime::execStart(); foreach ($data as $k => $v) { $url = isset($v['url']) ? $v['url'] : $this->apiUrl; $callback = isset($v['callback']) ? $v['callback'] : null; $errorCallback = isset($v['error_callback']) ? $v['error_callback'] : null; $parameters = array('service' => $v['service'], 'method' => $v['method'], 'args' => isset($v['args']) ? $v['args'] : array()); $this->rawData[$k + 1] = $parameters; $opt = isset($v['opt']) && is_array($v['opt']) ? $v['opt'] : array(YAR_OPT_PACKAGER => $this->packager); \Yar_Concurrent_Client::call($url, 'callService', array($parameters), $callback, $errorCallback, $opt); } \Yar_Concurrent_Client::loop(array($this, 'callback'), array($this, 'errorCallback')); \Yar_Concurrent_Client::reset(); $resultData = self::$result; self::$result = array(); if ((APP_ENV == 'dev' || APP_ENV == 'test') && DI::getDefault()['request']->hasQuery('debug')) { ApiLog::addLog($data, $resultData); } return $resultData; }