/** * HttpClient对象 * @return HttpClient */ public function driver() { if (null === $this->_driver) { $this->_driver = \HttpClient::factory(); } return $this->_driver; }
protected function get_data($url) { $rs = \HttpClient::factory()->get($this->base_url . $url . '?v=' . \Core::VERSION); if (200 == $rs->code()) { echo $rs->data(); } else { echo '<div style="padding:6px;">请求数据失败,请稍后再试</div>'; if (\IS_DEBUG) { echo '<br>code:' . $rs->code(); echo '<br><br>' . $rs->data(); } } }
public static function geta() { $uri = 'http://tq.21004.com/'; $data = iconv('GBK', 'UTF-8', \HttpClient::factory()->get($uri)->data()); $d = array(); if (preg_match_all('#<h3>([^<]+)</h3>(.*)<\\/div>#Uis', $data, $m)) { foreach ($m[0] as $k => $v) { $d[$m[1][$k]] = array(); preg_match_all('#target="_blank">([^<]+)<\\/a>#Uis', $m[2][$k], $m2); foreach ($m2[0] as $k2 => $v2) { $d[$m[1][$k]][\PinYin::get($m2[1][$k2])] = $m2[1][$k2]; } } } }
/** * 获取当前类型的数据 * * @param OOP_ORM_Data $obj * @param $data * @param $compiled_data * @return mixed */ public function &get_data(OOP_ORM_Data $obj, &$data, &$compiled_data, &$compiled_raw_data) { # 处理URL变量 $this->url = $this->config['resource']; if (preg_match_all('#{{([a-z0-9_]+)}}#i', $this->url, $m)) { foreach ($m[1] as $v) { $this->url = str_replace('{{' . $v . '}}', $obj->{$v}, $this->url); } } # 获取缓存 if (isset($this->config['cache'])) { /** * @var $cache Cache */ list($cache, $key) = $this->get_cache_instance_and_key($obj); $tmp_data = $cache->get($key); } else { $tmp_data = null; } # 获取内容 if (null === $tmp_data || false === $tmp_data) { $tmp_data = HttpClient::factory()->get($this->url)->data(); if (false === $tmp_data) { return false; } # 处理数据类型 if (isset($this->config['field_type'])) { OOP_ORM_DI::_check_field_type($this->config['field_type'], $tmp_data); } # 设置缓存 if (isset($this->config['cache'])) { $cache->set($key, $tmp_data, isset($this->config['cache']['expired']) ? $this->config['cache']['expired'] : 3600, isset($this->config['cache']['expire_type']) ? $this->config['cache']['expire_type'] : null); } } # 处理格式化数据 if (isset($this->config['format'])) { OOP_ORM_DI::_do_de_format_data($this->config['format'], $tmp_data); } $compiled_data[$this->key] = $tmp_data; $compiled_raw_data[$this->key] = $tmp_data; return $compiled_data[$this->key]; }
/** * 管理页面首页控制器 */ public function action_default() { $id = $this->arguments[0]; if (!$id) { $this->show_error('缺少参数'); } # APP URL $url = $this->base_url . 'get_app_info?id=' . $id; /* # 获取APP信息 $data = \HttpClient::factory()->get($url)->data(); $data = @\unserialize($data); */ $data = array('title' => '测试', 'url' => 'http://myqee.com/app_test/Archive.zip', 'type' => 'plug-in', 'hash' => 'd7646fa39b991f02f99251a477dd3ee2'); $file_dat = \HttpClient::factory()->get($data['url'])->data(); # 保存文件 \file_put_contents(\DIR_TEMP . $data['hash'], $file_dat); echo 'ok'; }
/** * 获取HttpClient配置类型的数据 * * @param array $config * @return array */ protected static function _get_data_httpget($config) { if (isset($config['method']) && \strtolower($config['method']) == 'post') { $method = 'post'; } else { $method = 'get'; } if (isset($config['type'])) { $httpget = \HttpClient::factory($config['type']); } else { $httpget = \HttpClient::factory(); } if ($method == 'post') { if (isset($config['timeout'])) { return $httpget->post($config['url'], $config['arguments'], $config['timeout']); } else { return $httpget->post($config['url'], $config['arguments']); } } else { $config['url'] .= (\strpos($config['url'], '?') ? '&' : '?') . \http_build_query($config['arguments'], '', '&'); if (isset($config['timeout'])) { return $httpget->get($config['url'], $config['timeout']); } else { return $httpget->get($config['url']); } } }
/** * 执行当前任务 * * @return bool|array */ public function run() { if ($this->accept()) { // 获取执行 try { $callback = $this->job['callback']; $arguments = $this->job['arguments']; if (substr($callback, 0, 7) === 'http://' || substr($callback, 0, 7) === 'https://') { # url if ($arguments) { $query = http_build_query($arguments, '', '&'); if (strpos($callback, '?') !== false) { $url = $callback . '&' . $query; } else { $url = $callback . '?' . $query; } } else { $url = $callback; } $rs = HttpClient::factory()->get($url); if ($rs->code() === 200) { $rs = $rs->data(); } else { throw new Exception('queue fail. url:' . $url); } } elseif (strpos($callback, '::') === false) { # exec $callback = escapeshellcmd($callback); if ($arguments) { $p = ''; foreach ((array) $arguments as $item) { $p .= ' ' . escapeshellarg($item); } $callback .= $p; } $rs = trim(shell_exec($callback)); $pos = strrpos($rs, "\n"); if (false === $pos) { $pos = strrpos($rs, "\r"); } if (false === $pos) { $rs2 = $rs; } else { $rs2 = substr($rs, $pos); } if ($rs2 !== 'success') { throw new Exception('queue callback not success. cmd: ' . $callback); } } else { $rs = call_user_func($callback, $arguments, $this); if (false === $rs) { throw new Exception('queue callback get false'); } } // 更新状态 $value = array('status' => Queue::COMPLETE, 'result' => (string) $rs); $where = array('id' => $this->id()); Queue::driver()->update(Queue::$table_name, $value, $where); $this->job['result'] = (string) $rs; $this->job['status'] = Queue::COMPLETE; return true; } catch (Exception $e) { // 更新状态 $delay = 10; $value = array('job_time' => time() + $delay, 'status' => Queue::RETRY); $where = array('id' => $this->id()); Queue::driver()->value_increment('retry_count', 1)->update(Queue::$table_name, $value, $where); throw $e; } } else { return false; } }