/** * 使用socket下载 * * @param unknown_type $url * @param unknown_type $file * @return multitype:boolean unknown */ public static function downloadUseSocket($url, $file) { Wind::import('WIND:http.transfer.WindHttpSocket'); $http = new WindHttpSocket($url); WindFolder::mkRecur(dirname($file)); $data = $http->send(); WindFile::write($file, $data); $http->close(); return array(true, $file); }
/** * url forward 兼容处理 * 获取真正的请求链接,并初始化socket句柄 * * @param array $options */ private function followLocation() { if (!$this->_redirects) { return; } if ($this->_maxRedirs <= 0) { return; } $maxRedirs = $this->_maxRedirs; $newurl = $this->url; do { /* @var $socket WindHttpSocket */ $socket = new WindHttpSocket($newurl, $this->timeout); $socket->setResponseHasBody(false); $socket->setResponseHasHeader(true); $header = $socket->send(); $code = $socket->getStatus(); $socket->close(); if ($code == 301 || $code == 302) { preg_match('/Location:(.*?)\\n/', $header, $matches); $newurl = trim(array_pop($matches)); } else { $code = 0; } } while ($code && --$maxRedirs); if ($newurl == $this->url) { return; } $this->url = $newurl; $this->httpHandler = $this->createHttpHandler(); }
/** * 使用socket请求 * * @param unknown_type $url * @param unknown_type $tmpdir * @return Ambigous <multitype:boolean string , mixed, boolean, NULL, string, string> */ public static function requestAcloudUseSocket($url, $tmpdir = '') { Wind::import('WIND:http.transfer.WindHttpSocket'); $http = new WindHttpSocket($url); if ($tmpdir !== '') { WindFolder::mkRecur($tmpdir); $_tmp = $tmpdir . '/tmp.' . Pw::getTime(); $data = $http->send(); WindFile::write($_tmp, $data); $realname = basename($url); $http->close(); chmod($_tmp, 0766); if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { copy($_tmp, $tmpdir . DIRECTORY_SEPARATOR . $realname); } else { rename($_tmp, $tmpdir . DIRECTORY_SEPARATOR . $realname); } $result = array(true, $tmpdir . '/' . $realname); } else { $http->setRedirects(true); $result = $http->send(); $result = trim(stristr($result, "\r\n"), "\r\n"); if (false !== ($pos = strpos($result, "\r\n"))) { $result = substr($result, 0, $pos); } $result && ($result = WindJson::decode($result)); } return $result; }