/** * @param RequestInterface $request */ private function sendRequest(RequestInterface $request) { try { $this->lastResponse = $this->httpClient->send($request); } catch (ClientException $e) { $this->lastResponse = $e->getResponse(); } }
/** * Perform all queued HTTP transfers * * @return API self */ function send() : API { $this->__log->debug(__FUNCTION__ . ": start loop"); while (count($this->__client)) { $this->__client->send(); } $this->__log->debug(__FUNCTION__ . ": end loop"); return $this; }
public function testSend() { $requestMock = new Request('http://www.google.de', Request::METHOD_GET, array('foo' => 'bar'), array(), array(), array(), 'This is a test content'); $responseMock = new Response('This is a test content', 200); $mockAdapter = $this->getMockBuilder('apiTalk\\Adapter\\AdapterInterface')->setMethods(array('send'))->getMock(); $mockAdapter->expects($this->once())->method('send')->with($requestMock)->will($this->returnValue($responseMock)); $client = new Client($mockAdapter); $response = $client->send($requestMock); $this->assertEquals($responseMock, $response, 'Response is not equal!'); }
function notify($evrid, $status) { $ws = new Client("ws://127.0.0.1:33333/"); try { // $ws->connect(); $ws->send($evrid . "|" . $status, 'text'); } catch (Exception $e) { error_log($e->getMessage()); } }
/** * Fetch the provider's endpoint URL for the supplied resource * * @param string $url The provider's endpoint URL for the supplied resource * * @return string * * @throws \InvalidArgumentException If no valid link was found */ protected function fetchEndpointForUrl($url) { $client = new Client($url); $body = $client->send(); $regexp = str_replace('@formats@', implode('|', $this->supportedFormats), self::LINK_REGEXP); if (!preg_match_all($regexp, $body, $matches, PREG_SET_ORDER)) { throw new \InvalidArgumentException('No valid oEmbed links found on page.'); } foreach ($matches as $match) { if ($match['Format'] === $this->preferredFormat) { return $this->extractEndpointFromAttributes($match['Attributes']); } } return $this->extractEndpointFromAttributes($match['Attributes']); }
/** * Method returns the response for the requested resource * @param string $pURI * @param array $pOptions * @return mixed */ protected function requestResource($pURL, array $pOptions) { $options = array_replace_recursive($this->settings['defaults'], $pOptions); $format = $options['format']; $method = $options['method']; $this->setHeaders($options); $parameters['headers'] = $this->headers; if (isset($options['body'])) { $parameters['body'] = $this->formatBody($options); } $request = $this->client->createRequest($method, $pURL, $parameters); try { $response = $this->client->send($request); $this->event->fire('forrest.response', array($request, $response)); return $this->responseFormat($response, $format); } catch (RequestException $e) { $this->assignExceptions($e); } }
function syncBidWinner($job) { global $redis, $elephant; $workString = $job->workload(); // error_log( $workString); $work = json_decode($workString); if (!isset($work->productid)) { return false; } $redis->set("PROD:" . $work->productid . ":BIDDER", $work->nickname); date_default_timezone_set('Asia/Shanghai'); $dt = Date('Y-m-d H:i:s'); $json = '{"FROM":"SyncBidWinner","PRODID":"' . $work->productid . '","WINNER":"' . (empty($work->nickname) ? "无" : $work->nickname) . '"}'; error_log("[" . $dt . "] socket broadcast : " . $json); $socket = new Client('ws://127.0.0.1:33333'); $socket->connect(); $socket->send($json); $elephant->initialize(); // $elephant->emit('broadcast', $json); $elephant->emit('broadcast', json_decode($json, true)); $elephant->close(); }
/** * Similar to wrapXmlrpcMethod, but will generate a php class that wraps * all xmlrpc methods exposed by the remote server as own methods. * For more details see wrapXmlrpcMethod. * * For a slimmer alternative, see the code in demo/client/proxy.php * * Note that unlike wrapXmlrpcMethod, we always have to generate php code here. It seems that php 7 will have anon classes... * * @param Client $client the client obj all set to query the desired server * @param array $extraOptions list of options for wrapped code. See the ones from wrapXmlrpcMethod plus * - string method_filter regular expression * - string new_class_name * - string prefix * - bool simple_client_copy set it to true to avoid copying all properties of $client into the copy made in the new class * * @return mixed false on error, the name of the created class if all ok or an array with code, class name and comments (if the appropriatevoption is set in extra_options) */ public function wrapXmlrpcServer($client, $extraOptions = array()) { $methodFilter = isset($extraOptions['method_filter']) ? $extraOptions['method_filter'] : ''; $timeout = isset($extraOptions['timeout']) ? (int) $extraOptions['timeout'] : 0; $protocol = isset($extraOptions['protocol']) ? $extraOptions['protocol'] : ''; $newClassName = isset($extraOptions['new_class_name']) ? $extraOptions['new_class_name'] : ''; $encodePhpObjects = isset($extraOptions['encode_php_objs']) ? (bool) $extraOptions['encode_php_objs'] : false; $decodePhpObjects = isset($extraOptions['decode_php_objs']) ? (bool) $extraOptions['decode_php_objs'] : false; $verbatimClientCopy = isset($extraOptions['simple_client_copy']) ? !$extraOptions['simple_client_copy'] : true; $buildIt = isset($extraOptions['return_source']) ? !$extraOptions['return_source'] : true; $prefix = isset($extraOptions['prefix']) ? $extraOptions['prefix'] : 'xmlrpc'; $namespace = '\\PhpXmlRpc\\'; $reqClass = $namespace . 'Request'; $decoderClass = $namespace . 'Encoder'; $req = new $reqClass('system.listMethods'); $response = $client->send($req, $timeout, $protocol); if ($response->faultCode()) { error_log('XML-RPC: ' . __METHOD__ . ': could not retrieve method list from remote server'); return false; } else { $mList = $response->value(); if ($client->return_type != 'phpvals') { $decoder = new $decoderClass(); $mList = $decoder->decode($mList); } if (!is_array($mList) || !count($mList)) { error_log('XML-RPC: ' . __METHOD__ . ': could not retrieve meaningful method list from remote server'); return false; } else { // pick a suitable name for the new function, avoiding collisions if ($newClassName != '') { $xmlrpcClassName = $newClassName; } else { $xmlrpcClassName = $prefix . '_' . preg_replace(array('/\\./', '/[^a-zA-Z0-9_\\x7f-\\xff]/'), array('_', ''), $client->server) . '_client'; } while ($buildIt && class_exists($xmlrpcClassName)) { $xmlrpcClassName .= 'x'; } /// @todo add function setdebug() to new class, to enable/disable debugging $source = "class {$xmlrpcClassName}\n{\npublic \$client;\n\n"; $source .= "function __construct()\n{\n"; $source .= $this->buildClientWrapperCode($client, $verbatimClientCopy, $prefix, $namespace); $source .= "\$this->client = \$client;\n}\n\n"; $opts = array('return_source' => true, 'simple_client_copy' => 2, 'timeout' => $timeout, 'protocol' => $protocol, 'encode_php_objs' => $encodePhpObjects, 'decode_php_objs' => $decodePhpObjects, 'prefix' => $prefix); /// @todo build phpdoc for class definition, too foreach ($mList as $mName) { if ($methodFilter == '' || preg_match($methodFilter, $mName)) { // note: this will fail if server exposes 2 methods called f.e. do.something and do_something $opts['new_function_name'] = preg_replace(array('/\\./', '/[^a-zA-Z0-9_\\x7f-\\xff]/'), array('_', ''), $mName); $methodWrap = $this->wrapXmlrpcMethod($client, $mName, $opts); if ($methodWrap) { if (!$buildIt) { $source .= $methodWrap['docstring']; } $source .= $methodWrap['source'] . "\n"; } else { error_log('XML-RPC: ' . __METHOD__ . ': will not create class method to wrap remote method ' . $mName); } } } $source .= "}\n"; if ($buildIt) { $allOK = 0; eval($source . '$allOK=1;'); if ($allOK) { return $xmlrpcClassName; } else { error_log('XML-RPC: ' . __METHOD__ . ': could not create class ' . $xmlrpcClassName . ' to wrap remote server ' . $client->server); return false; } } else { return array('class' => $xmlrpcClassName, 'code' => $source, 'docstring' => ''); } } } }
<?php include './config.php'; if ($argc < 5) { die("Usage: {$argv[0]} [fromHost] [toHost] [port] [message to send.]\n"); } $fromHost = $argv[1]; $toHost = $argv[2]; $port = $argv[3]; $msg = ""; for ($x = 3; $x < $argc; $x++) { if ($x == $argc - 1) { $msg .= $argv[$x]; } else { $msg .= $argv[$x] . " "; } } try { $myClient = new Client($fromHost, $toHost, $port); echo $myClient->showStatus(); $myClient->startUp(); echo $myClient->showStatus(); $myClient->send($msg); $myClient->disconnect(); echo $myClient->showStatus(); } catch (Exception $e) { die("Fatal error: {$e->getMessage()}\n"); }
public static function post($url, $postData = NULL, $queryString = NULL, array $options = array()) { $c = new Client($url, 'POST', $options); $c->setPostData($postData); $c->setQueryString($queryString); return $c->send(); }
public function testClientSendData() { $this->assertTrue(self::$client->send('Hello world'), 'Клиент не может отправить данные'); }
} public function onError($client) { echo "Connect failed" . PHP_EOL; } public function onClose($client) { echo "Connection close" . PHP_EOL; } public function onConnect($client) { echo "Connection open" . PHP_EOL; } public function send($data) { $this->client->send($data); } public function onReceive(swoole_client $client, $data) { $data = $this->client->recv(); print_r($data); } private function isConnected() { return $this->client->isConnected(); } } $client = new Client('127.0.0.1', 9501); $data = json_encode(array('code' => '4001', 'data' => array('id' => 1))); $client->send($data);