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; }
<?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 yarLoop() { return Yar_Concurrent_Client::loop(); }
<?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
/** * @return mixed */ public function loop() { return \Yar_Concurrent_Client::loop(); }
/** * 发送并行请求 * @param null $callback * @param callable $error_callback * @return $this */ public function loop($callback = null, callable $error_callback = null) { \Yar_Concurrent_Client::loop($callback, $error_callback); $this->_sync = false; //关闭并行 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; }