/** * @param string $varBody * @return mixed|void */ public function send($varBody = '') { $data = $varBody; if (is_array($data)) { $data = http_build_query($data); } if ($this->option['method'] == 'POST') { if ($data == '') { $data = $this->__buildPostData(); //http_build_query($this->postdata); } $this->option['content'] = $data; if (!isset($this->httpheader['Content-Type'])) { if ($this->__isBinary) { $this->httpheader['Content-Type'] = 'Content-Type: multipart/form-data; boundary=' . $this->__boundary; } else { $this->httpheader['Content-Type'] = 'Content-Type: application/x-www-form-urlencoded'; } } $this->httpheader['Content-Length'] = 'Content-Length: ' . strlen($data); } $this->httpheader[] = 'Host: ' . $this->parsed_url['host']; //$this->httpheader[] = 'Referer: ' . 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; $this->httpheader[] = 'Connection: close'; if (!isset($this->httpheader['Accept'])) { if (isset($_SERVER['HTTP_ACCEPT'])) { $this->httpheader['Accept'] = 'Accept:' . $_SERVER['HTTP_ACCEPT']; } } if (!isset($this->httpheader['Accept-Language'])) { if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { $this->httpheader['Accept-Language'] = 'Accept-Language: ' . $_SERVER['HTTP_ACCEPT_LANGUAGE']; } } if ($this->isgzip == true) { $this->httpheader['Accept-Encoding'] = 'Accept-Encoding: gzip'; } $this->option['header'] = implode("\r\n", $this->httpheader); ZBlogException::SuspendErrorHook(); $socket = fsockopen(($this->scheme == 'https' ? 'ssl://' : '') . $this->parsed_url['host'], $this->port, $this->errno, $this->errstr, $this->timeout); ZBlogException::SuspendErrorHook(); if (!$socket) { return; } $url = $this->option['method'] . ' ' . ($this->parsed_url['path'] == '' ? '/' : $this->parsed_url['path']); if (isset($this->parsed_url["query"])) { $url .= "?" . $this->parsed_url["query"]; } fwrite($socket, $url . ' HTTP/1.0' . "\r\n"); fwrite($socket, $this->option['header'] . "\r\n"); fwrite($socket, "\r\n"); if (isset($this->option['content'])) { fwrite($socket, $this->option['content'] . "\r\n"); fwrite($socket, "\r\n"); } while (!feof($socket)) { $this->responseText .= fgets($socket, 128); } $this->responseHeader = substr($this->responseText, 0, strpos($this->responseText, "\r\n\r\n")); $this->responseText = substr($this->responseText, strpos($this->responseText, "\r\n\r\n") + 4); $this->responseHeader = explode("\r\n", $this->responseHeader); $i = $this->maxredirs; if ($this->maxredirs > 0) { if (strstr($this->responseHeader[0], ' 301 ') || strstr($this->responseHeader[0], ' 302 ') || strstr($this->responseHeader[0], ' 303 ') || strstr($this->responseHeader[0], ' 307 ')) { fclose($socket); $url = $this->getResponseHeader('Location'); $this->canreinit = false; $this->open('Get', $url); $this->setMaxRedirs($i - 1); $this->canreinit = true; return $this->send(); } } if ($this->getResponseHeader('Transfer-Encoding') == 'chunked') { if (!function_exists('http_chunked_decode')) { $this->responseText = $this->http_chunked_decode($this->responseText); } else { $this->responseText = http_chunked_decode($this->responseText); } } if ($this->getResponseHeader('Content-Encoding') == 'gzip') { if (!function_exists('gzdecode')) { $this->responseText = $this->gzdecode($this->responseText); } else { $this->responseText = gzdecode($this->responseText); } } if (isset($this->responseHeader[0])) { $this->statusText = $this->responseHeader[0]; $a = explode(' ', $this->statusText); if (isset($a[0])) { $this->responseVersion = $a[0]; } if (isset($a[1])) { $this->status = $a[1]; } unset($this->responseHeader[0]); } fclose($socket); }
/** * 解开应用包 * @param $xml * @return bool */ public static function UnPack($xml) { global $zbp; $xml = simplexml_load_string($xml); if (!$xml) { return false; } if ($xml['version'] != 'php') { return false; } $type = $xml['type']; $id = $xml->id; $dir = $zbp->path . 'zb_users/' . $type . '/'; ZBlogException::SuspendErrorHook(); if (!file_exists($dir . $id . '/')) { @mkdir($dir . $id . '/', 0755, true); } foreach ($xml->folder as $folder) { $f = $dir . $folder->path; if (!file_exists($f)) { @mkdir($f, 0755, true); } } foreach ($xml->file as $file) { $s = base64_decode($file->stream); $f = $dir . $file->path; @file_put_contents($f, $s); if (function_exists('chmod')) { @chmod($f, 0755); } } ZBlogException::ResumeErrorHook(); return true; }
/** * @param string $varBody */ public function send($varBody = '') { $data = $varBody; if (is_array($data)) { $data = http_build_query($data); } if ($this->option['method'] == 'POST') { if ($data == '') { $data = $this->__buildPostData(); //http_build_query($this->postdata); } $this->option['content'] = $data; if (!isset($this->httpheader['Content-Type'])) { if ($this->__isBinary) { $this->httpheader['Content-Type'] = 'Content-Type: multipart/form-data; boundary=' . $this->__boundary; } else { $this->httpheader['Content-Type'] = 'Content-Type: application/x-www-form-urlencoded'; } } $this->httpheader['Content-Length'] = 'Content-Length: ' . strlen($data); } $this->option['header'] = implode("\r\n", $this->httpheader); //$this->httpheader[] = 'Referer: ' . 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; if ($this->maxredirs > 0) { $this->option['follow_location'] = 1; $this->option['max_redirects'] = $this->maxredirs; } else { $this->option['follow_location'] = 0; $this->option['max_redirects'] = 0; } ZBlogException::SuspendErrorHook(); $http_response_header = null; $this->responseText = file_get_contents(($this->isgzip == true ? 'compress.zlib://' : '') . $this->url, false, stream_context_create(array('http' => $this->option))); $this->responseHeader = $http_response_header; ZBlogException::ResumeErrorHook(); if (isset($this->responseHeader[0])) { $this->statusText = $this->responseHeader[0]; $a = explode(' ', $this->statusText); if (isset($a[0])) { $this->responseVersion = $a[0]; } if (isset($a[1])) { $this->status = $a[1]; } unset($this->responseHeader[0]); } }
/** * 通过URL获取远程页面内容 * @param string $url URL地址 * @return string 返回页面文本内容,默认为null */ function GetHttpContent($url) { if (class_exists('Network')) { $ajax = Network::Create(); if (!$ajax) { return null; } $ajax->open('GET', $url); $ajax->enableGzip(); $ajax->setTimeOuts(60, 60, 0, 0); $ajax->send(); if ($ajax->status == 200) { return $ajax->responseText; } else { return null; } } $r = null; if (function_exists("curl_init") && function_exists('curl_exec')) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); if (ini_get("safe_mode") == false && ini_get("open_basedir") == false) { curl_setopt($ch, CURLOPT_MAXREDIRS, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); } if (extension_loaded('zlib')) { curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); } $r = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($code == 200) { return $r; } } elseif (ini_get("allow_url_fopen")) { ZBlogException::SuspendErrorHook(); $http_response_header = null; $r = file_get_contents((extension_loaded('zlib') ? 'compress.zlib://' : '') . $url); if (is_array($http_response_header) && in_array('HTTP/1.1 200 OK', $http_response_header)) { if ($r !== false) { return $r; } } ZBlogException::ResumeErrorHook(); } return null; }