__handlePropertyHandler(__CLASS__); } /** * @Inject * @var ClientFactory */ private $clientFactory; function get_host($url, &$path) { $pos = strpos($url, "/", 8); if (!$pos) { $path = "/"; return $url; } $path = substr($url, $pos); return substr($url, 0, $pos); } public function get_content_length($url, $time_out = 60) { if ($url == "") { return 0; } try { $options = ['base_uri' => "", 'handler' => HandlerStack::create(new CoroutineHandler()), 'timeout' => $time_out, 'swoole' => ['timeout' => $time_out, 'socket_buffer_size' => 1024 * 1024 * 2]]; $path = ""; $host = $this->get_host($url, $path); if ($host == "") { return 0; } $options["base_uri"] = $host; $client = $this->clientFactory->create($options); $response = $client->get($path); if ($response->getStatusCode() != 200) { return 0; } $str_size = $response->getHeader('Content-Length'); $size = intval($str_size[0]); return $size; } catch (ClientException $e) { //获取协程中出现的异常。 $response_e = $e->getResponse(); } return 0; } public function get_web_result($url, $time_out = 10) { if ($url == "") { return ""; } $path = ""; $host = $this->get_host($url, $path); if ($host == "") { return ""; } $options = ['base_uri' => $host, 'handler' => HandlerStack::create(new CoroutineHandler()), 'timeout' => $time_out, 'swoole' => ['timeout' => $time_out, 'socket_buffer_size' => 1024 * 1024 * 2]]; try { //var_dump($this->clientFactory); $client = $this->clientFactory->create($options); $response = $client->get($path); if ($response->getStatusCode() == 200) { $strContent = $response->getBody()->getContents(); //echo "$strContent\r\n"; return $strContent; } } catch (ClientException $e) { //获取协程中出现的异常。 var_dump($e->getResponse()); } return ""; } public function post_web_result($url, $post_data, $time_out = 10) { if ($url == "") { return ""; } $path = ""; $host = $this->get_host($url, $path); if ($host == "") { return ""; } $options = ['base_uri' => $host, 'handler' => HandlerStack::create(new CoroutineHandler()), 'timeout' => $time_out, 'swoole' => ['timeout' => $time_out, 'socket_buffer_size' => 1024 * 1024 * 2]]; try { $client = $this->clientFactory->create($options); $response = $client->request('POST', $url, ['form_params' => $post_data]); //debug //$response = $client->request('POST', $url, ['form_params' => $post_data,'debug' => true]); if ($response->getStatusCode() == 200) { $strContent = $response->getBody()->getContents(); //echo "$strContent\r\n"; return $strContent; } } catch (ClientException $e) { //获取协程中出现的异常。 //var_dump($e->getResponse()); } return ""; } public function post_json_result($url, $post_data, $time_out = 10) { if ($url == "") { return ""; } $path = ""; $host = $this->get_host($url, $path); if ($host == "") { return ""; } $options = ['base_uri' => $host, 'handler' => HandlerStack::create(new CoroutineHandler()), 'timeout' => $time_out, 'swoole' => ['timeout' => $time_out, 'socket_buffer_size' => 1024 * 1024 * 2]]; try { $client = $this->clientFactory->create($options); if (is_array($post_data)) { $response = $client->request('POST', $url, ['body' => json_encode($post_data), "headers" => ['Accept' => 'application/json']]); } else { $response = $client->request('POST', $url, ['body' => $post_data, "headers" => ['Accept' => 'application/json']]); } //debug //$response = $client->request('POST', $url, ['form_params' => $post_data,'debug' => true]); if ($response->getStatusCode() == 200) { $strContent = $response->getBody()->getContents(); //echo "$strContent\r\n"; return $strContent; } } catch (ClientException $e) { //获取协程中出现的异常。 var_dump($e->getResponse()); } return ""; } }