当前位置: 首页>>代码示例>>PHP>>正文


PHP HTTPClient::sendRequest方法代码示例

本文整理汇总了PHP中HTTPClient::sendRequest方法的典型用法代码示例。如果您正苦于以下问题:PHP HTTPClient::sendRequest方法的具体用法?PHP HTTPClient::sendRequest怎么用?PHP HTTPClient::sendRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HTTPClient的用法示例。


在下文中一共展示了HTTPClient::sendRequest方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: sendRequest

 /**
  * Wraps an event around the parent function
  *
  * @triggers HTTPCLIENT_REQUEST_SEND
  * @author   Andreas Gohr <andi@splitbrain.org>
  */
 function sendRequest($url, $data = '', $method = 'GET')
 {
     $httpdata = array('url' => $url, 'data' => $data, 'method' => $method);
     $evt = new Doku_Event('HTTPCLIENT_REQUEST_SEND', $httpdata);
     if ($evt->advise_before()) {
         $url = $httpdata['url'];
         $data = $httpdata['data'];
         $method = $httpdata['method'];
     }
     $evt->advise_after();
     unset($evt);
     return parent::sendRequest($url, $data, $method);
 }
开发者ID:ryankask,项目名称:dokuwiki,代码行数:19,代码来源:HTTPClient.php

示例2: sendRequest

 /**
  * Retries sending the request multiple times
  *
  * @param string $url
  * @param string $data
  * @param string $method
  * @return bool
  */
 function sendRequest($url, $data = '', $method = 'GET')
 {
     $this->tries = 2;
     // configures the number of retries
     $return = false;
     while ($this->tries) {
         $return = parent::sendRequest($url, $data, $method);
         if ($this->status != -100) {
             break;
         }
         $this->tries--;
     }
     return $return;
 }
开发者ID:richmahn,项目名称:Door43,代码行数:22,代码来源:httpclient_mock.php

示例3: macro_ImageFileSize

function macro_ImageFileSize($formatter, $value = '', $params = array())
{
    global $Config;
    if (empty($value)) {
        return '';
    }
    $sz = 0;
    // check if it is valid or not
    while (preg_match('/^((?:https?|ftp):\\/\\/.*(\\.(?:jpg|jpeg|gif|png)))(?:\\?|&)?/i', $value, $m)) {
        $value = $m[1];
        // check the file size saved by the fetch plugin
        $si = new Cache_text('fetchinfo');
        if ($si->exists($value) && ($info = $si->fetch($value)) !== false) {
            $sz = $info['size'];
            break;
        }
        $sc = new Cache_text('imagefilesize');
        if (empty($params['.refresh']) and $sc->exists($value) and $sc->mtime($value) < time() + 60 * 60 * 24 * 20) {
            $sz = $sc->fetch($value);
        } else {
            // dynamic macro
            if ($formatter->_macrocache and empty($params['call'])) {
                return $formatter->macro_cache_repl('ImageFileSize', $value);
            }
            if (empty($params['call'])) {
                $formatter->_dynamic_macros['@ImageFileSize'] = 1;
            }
            // do not fetch the size of image right now. just fetch the cached info by the fetch plugin
            if (empty($params['call']) and !empty($Config['fetch_imagesize']) and $Config['fetch_imagesize'] == 2) {
                return _("Unknown");
            }
            require_once dirname(__FILE__) . '/../lib/HTTPClient.php';
            $http = new HTTPClient();
            // support proxy
            if (!empty($Config['proxy_host'])) {
                $http->proxy_host = $Config['proxy_host'];
                if (!empty($Config['proxy_port'])) {
                    $http->proxy_port = $Config['proxy_port'];
                }
            }
            // set referrer
            $referer = '';
            if (!empty($Config['fetch_referer_re'])) {
                foreach ($Config['fetch_referer_re'] as $re => $ref) {
                    if (preg_match($re, $value)) {
                        $referer = $ref;
                        break;
                    }
                }
            }
            // default referrer
            if (empty($referer) and !empty($Config['fetch_referer'])) {
                $referer = $Config['fetch_referer'];
            }
            if (empty($referer)) {
                $referer = qualifiedUrl($formatter->link_url($formatter->page->urlname));
            }
            $http->nobody = true;
            $http->referer = $referer;
            // check HEAD support for the internal fetch plugin
            $method = 'GET';
            if (!empty($Config['fetch_imagesize_head']) && strstr($value, $Config['fetch_action'])) {
                $method = 'HEAD';
            }
            $http->sendRequest($value, array(), $method);
            if ($http->status != 200) {
                return _("Unknown");
            }
            if (isset($http->resp_headers['content-length'])) {
                $sz = $http->resp_headers['content-length'];
            }
            $sc->update($value, $sz);
        }
        break;
    }
    if ($sz > 0) {
        $unit = array('Bytes', 'KB', 'MB', 'GB');
        for ($i = 0; $i < 4; $i++) {
            if ($sz <= 1024) {
                break;
            }
            $sz = $sz / 1024;
        }
        return round($sz, 2) . ' ' . $unit[$i];
    }
    // set as dynamic macro or not.
    if ($formatter->_macrocache and empty($options['call'])) {
        return $formatter->macro_cache_repl('ImageFileSize', $value);
    }
    if (empty($options['call'])) {
        $formatter->_dynamic_macros['@ImageFileSize'] = 1;
    }
    return _("Unknown");
}
开发者ID:sedrion,项目名称:moniwiki,代码行数:94,代码来源:ImageFileSize.php

示例4: getPages

 function getPages($params)
 {
     global $DBInfo;
     $http = new HTTPClient();
     if (empty($this->json)) {
         $this->json = new Services_JSON();
     }
     $offset = 0;
     if (!empty($params['offset']) and is_numeric($params['offset']) and $params['offset'] > 0) {
         $offset = $params['offset'];
     }
     // set page_limit
     $pages_limit = isset($DBInfo->pages_limit) ? $DBInfo->pages_limit : 5000;
     // 5000 pages
     $total = $this->pageCount();
     $size = $pages_limit;
     if (!empty($params['all'])) {
         $size = $total;
     }
     $url = $this->index_url . '/text/_search';
     $data = '{ "fields" : "title", "from" : ' . $offset . ', "size" : ' . $size . ', "query": { "query_string" : {"query" : "*"} } }';
     if (!$http->sendRequest($url, $data, 'POST')) {
         return false;
     }
     if ($http->status != 200) {
         return false;
     }
     if ($http->resp_body === false) {
         return false;
     }
     $pages = array();
     $json = $this->json->decode($http->resp_body);
     foreach ($json->hits->hits as $hit) {
         $pages[] = $hit->_id;
     }
     $info['offset'] = $offset;
     $info['count'] = count($pages);
     if (isset($params['ret'])) {
         $params['ret'] = $info;
     }
     return $pages;
 }
开发者ID:ahastudio,项目名称:moniwiki,代码行数:42,代码来源:titleindexer.es.php

示例5: macro_WikimediaCommons


//.........这里部分代码省略.........
                break;
        }
    }
    $common = new Cache_Text('wikicommons');
    $key = $value . $src;
    if (isset($width)) {
        $key .= $width;
    }
    if (isset($height)) {
        $key .= '.h' . $height;
    }
    if (!empty($formatter->refresh) || ($images = $common->fetch($key)) === false) {
        if (!empty($width)) {
            $data['iiurlwidth'] = min(1280, $width);
        } else {
            if (!empty($height)) {
                $data['iiurlheight'] = min(1280, $height);
            } else {
                // default image width
                $data['iiurlwidth'] = 640;
            }
        }
        require_once dirname(__FILE__) . '/../lib/HTTPClient.php';
        $http = new HTTPClient();
        $save = ini_get('max_execution_time');
        set_time_limit(0);
        // support proxy
        if (!empty($DBInfo->proxy_host)) {
            $http->proxy_host = $DBInfo->proxy_host;
            if (!empty($DBInfo->proxy_port)) {
                $http->proxy_port = $DBInfo->proxy_port;
            }
        }
        $http->sendRequest($api_url, $data, 'POST');
        set_time_limit($save);
        // FIXME
        if ($http->status != 200) {
            return '';
        }
        $res = json_decode($http->resp_body);
        $images = $res->query->pages;
        $common->update($key, $images);
    }
    $image = current($images);
    $image_url = $image->imageinfo[0]->thumburl;
    $desc_url = $image->imageinfo[0]->descriptionurl;
    if (empty($styles['width']) && !empty($image->imageinfo[0]->thumbwidth)) {
        $styles['width'] = $image->imageinfo[0]->thumbwidth . 'px';
    }
    $attrs = array();
    $keys = array('width', 'height');
    foreach ($keys as $key) {
        if (isset($styles[$key])) {
            $attrs[] = $key . '="' . $styles[$key] . '"';
            unset($styles[$key]);
        }
    }
    $attr = '';
    if (count($attrs)) {
        $attr = ' ' . implode(' ', $attrs);
    }
    $style = '';
    foreach ($styles as $k => $v) {
        $style .= $k . ':' . $v . ';';
    }
    if (!empty($style)) {
开发者ID:ahastudio,项目名称:moniwiki,代码行数:67,代码来源:WikimediaCommons.php

示例6: macro_Fetch

function macro_Fetch($formatter, $url = '', $params = array())
{
    global $DBInfo;
    if (empty($url)) {
        $params['retval']['error'] = _("Empty URL");
        return false;
    }
    // check valid url
    if (!preg_match('@^((ftp|https?)://[^/]+)/@', $url, $m)) {
        return false;
    }
    $siteurl = $m[1];
    require_once "lib/HTTPClient.php";
    $sz = 0;
    $allowed = 'png|jpeg|jpg|gif';
    if (!empty($DBInfo->fetch_exts)) {
        $allowed = $DBInfo->fetch_exts;
    }
    // urlencode()
    $url = _urlencode($url);
    // set default params
    $maxage = !empty($DBInfo->fetch_maxage) ? (int) $DBInfo->fetch_maxage : 60 * 60 * 24 * 7;
    $timeout = !empty($DBInfo->fetch_timeout) ? (int) $DBInfo->fetch_timeout : 15;
    $vartmp_dir = $DBInfo->vartmp_dir;
    $buffer_size = 2048 * 1024;
    // default buffer size
    if (!empty($DBInfo->fetch_buffer_size) and $DBInfo->fetch_buffer_size > 2048 * 1024) {
        $buffer_size = $DBInfo->fetch_buffer_size;
    }
    // set referrer
    $referer = '';
    if (!empty($DBInfo->fetch_referer_re)) {
        foreach ($DBInfo->fetch_referer_re as $re => $ref) {
            if (preg_match($re, $url)) {
                $referer = $ref;
                break;
            }
        }
    }
    // default referrer
    if (empty($referer) and !empty($DBInfo->fetch_referer)) {
        $referer = $DBInfo->fetch_referer;
    }
    // check site available
    $si = new Cache_text('siteinfo');
    if ($si->exists($siteurl)) {
        if (!empty($params['refresh'])) {
            $si->remove($siteurl);
        } else {
            if (empty($params['refresh']) && ($check = $si->fetch($siteurl)) !== false) {
                $params['retval']['status'] = $check['status'];
                $params['retval']['error'] = $check['error'];
                return false;
            }
        }
    }
    $sc = new Cache_text('fetchinfo');
    $error = null;
    if (empty($params['refresh']) and $sc->exists($url) and $sc->mtime($url) < time() + $maxage) {
        $info = $sc->fetch($url);
        $sz = $info['size'];
        $mimetype = $info['mimetype'];
        $error = !empty($info['error']) ? $info['error'] : null;
        // already retrived and found some error
        if (empty($params['refresh']) and !empty($error)) {
            $params['retval']['status'] = $info['status'];
            $params['retval']['error'] = $error;
            $params['retval']['mimetype'] = $mimetype;
            $params['retval']['size'] = $sz;
            return false;
        }
    } else {
        // check connection
        $http = new HTTPClient();
        // get file header
        $http->nobody = true;
        $http->referer = $referer;
        $http->sendRequest($url, array(), 'GET');
        //if ($http->status == 301 || $http->status == 302 ) {
        //
        //}
        if ($http->status != 200) {
            if ($http->status == 404) {
                $params['retval']['error'] = '404 File Not Found';
            } else {
                $params['retval']['error'] = !empty($http->error) ? $http->error : sprintf(_("Invalid Status %d"), $http->status);
            }
            $params['retval']['status'] = $http->status;
            // check alive site
            if ($http->status == -210) {
                $si->update($siteurl, array('status' => $http->status, 'error' => $params['retval']['error']), 60 * 60 * 24);
                return false;
            }
            $sc->update($url, array('size' => -1, 'mimetype' => '', 'error' => $params['retval']['error'], 'status' => $params['retval']['status']), 60 * 60 * 24 * 3);
            return false;
        }
        if (isset($http->resp_headers['content-length'])) {
            $sz = $http->resp_headers['content-length'];
        }
        if (isset($http->resp_headers['content-type'])) {
//.........这里部分代码省略.........
开发者ID:NessunKim,项目名称:MW_Skins,代码行数:101,代码来源:fetch.php

示例7: do_wikidiff

function do_wikidiff($formatter, $params = array())
{
    global $Config;
    $supported = array('default' => '%0%2?action=raw', 'namuwiki' => '%1raw/%2');
    if (!empty($Config['wikidiff_sites'])) {
        $wikis = $Config['wikidiff_sites'];
    } else {
        $wikis = array('kowikipedia' => 'https://ko.wikipedia.org/wiki/', 'librewiki' => 'http://librewiki.net/wiki/', 'namuwiki' => 'https://namu.wiki/raw/');
    }
    if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['wiki']) && isset($wikis[$_POST['wiki']])) {
        require_once dirname(__FILE__) . '/../lib/HTTPClient.php';
        $wiki = $_POST['wiki'];
        if (isset($supported[$wiki])) {
            $format_url = $supported[$wiki];
        } else {
            $format_url = $supported['default'];
        }
        $url = $wikis[$wiki];
        $parsed = parse_url($url);
        if (isset($_POST['value'][0])) {
            $pagename = rawurlencode($_POST['value']);
        } else {
            $pagename = $formatter->page->urlname;
        }
        // translate table.
        $trs = array('%0' => $url, '%1' => $parsed['scheme'] . '://' . $parsed['host'] . '/', '%2' => $pagename);
        $request_url = strtr($format_url, $trs);
        $save = ini_get('max_execution_time');
        set_time_limit(0);
        $http = new HTTPClient();
        $http->timeout = 15;
        // set timeout
        // support proxy
        if (!empty($Config['proxy_host'])) {
            $http->proxy_host = $Config['proxy_host'];
            if (!empty($Config['proxy_port'])) {
                $http->proxy_port = $Config['proxy_port'];
            }
        }
        $http->sendRequest($request_url, array(), 'GET');
        set_time_limit($save);
        $formatter->send_header('', $params);
        if ($http->status != 200) {
            $params['.title'] = sprintf(_("Fail to connect %s"), $http->status);
            $diff = null;
        } else {
            $diff = $formatter->get_diff($http->resp_body);
            $params['.title'] = sprintf(_("Difference between this wiki and %s."), $wiki);
        }
        $formatter->send_title('', '', $params);
        if (isset($diff[0])) {
            echo "<div id='wikiDiffPreview'>\n";
            echo $formatter->processor_repl('diff', $diff, $params);
            echo "</div>\n";
        } else {
            if ($http->status != 200) {
                echo sprintf(_("Status: %s"), $http->status);
            } else {
                echo _("No difference found.");
            }
        }
        $formatter->send_footer('', $params);
        return;
    }
    $select = '<select name="wiki">';
    $select .= '<option>' . _("-- Select Wiki --") . '</option>';
    foreach ($wikis as $w => $url) {
        $select .= '<option value="' . $w . '">' . $w . '</option>' . "\n";
    }
    $select .= '</select>';
    $name = isset($_GET['value'][0]) ? $_GET['value'] : '';
    $default = _html_escape($formatter->page->name);
    $optional = '<br />' . _("Page name:") . ' <input type="text" name="value" placeholder="' . $default . '" value="' . _html_escape($name) . '" /><br />';
    //$optional .= _("Reverse order:")." <input type='checkbox' name='reverse' /> ";
    $params['.title'] = _("Show difference between wikis.");
    $button = _("Diff");
    $formatter->send_header('', $params);
    $formatter->send_title('', '', $params);
    echo <<<FORM
<form method='post'>
{$select}
{$optional}
<input type='submit' value='{$button}' />
<input type='hidden' name='action' value='wikidiff' />
</form>
FORM;
    $formatter->send_footer('', $params);
    return;
}
开发者ID:sedrion,项目名称:moniwiki,代码行数:89,代码来源:wikidiff.php

示例8: macro_Play


//.........这里部分代码省略.........
            } else {
                if (preg_match('@^https?://(?:videofarm|tvpot)\\.daum\\.net/(?:.*?(?:clipid=|vid=|v/))?([a-zA-Z0-9%$]+)@i', $media[$i], $m)) {
                    // like as http://tvpot.daum.net/v/GCpMeZtuBnk%24
                    if (preg_match('@[0-9]+$@', $m[1])) {
                        // clipid case
                        $aurl = $media[$i];
                        $clipid = $m[1];
                        require_once "lib/HTTPClient.php";
                        // fetch tvpot.daum.net
                        $sc = new Cache_text('daumtvpot');
                        $maxage = 60 * 60;
                        if (empty($params['refresh']) and $sc->exists($aurl) and $sc->mtime($aurl) < time() + $maxage) {
                            $info = $sc->fetch($aurl);
                        } else {
                            // no cached info found.
                            if ($formatter->_macrocache and empty($params['call'])) {
                                return $formatter->macro_cache_repl('Play', $value);
                            }
                            if (empty($params['call'])) {
                                $formatter->_dynamic_macros['@Play'] = 1;
                            }
                            // try to fetch tvpot.daum.net
                            $http = new HTTPClient();
                            $save = ini_get('max_execution_time');
                            set_time_limit(0);
                            $http->timeout = 15;
                            // support proxy
                            if (!empty($DBInfo->proxy_host)) {
                                $http->proxy_host = $DBInfo->proxy_host;
                                if (!empty($DBInfo->proxy_port)) {
                                    $http->proxy_port = $DBInfo->proxy_port;
                                }
                            }
                            $http->sendRequest($aurl, array(), 'GET');
                            set_time_limit($save);
                            if ($http->status != 200) {
                                return '[[Media(' . $aurl . ')]]';
                            }
                            if (!empty($http->resp_body)) {
                                // search Open Graph url info
                                if (preg_match('@og:url"\\s+content="http://tvpot\\.daum\\.net/v/([^"]+)"@', $http->resp_body, $match)) {
                                    $info = array('vid' => $match[1], 'clipid' => $clipid);
                                    $sc->update($aurl, $info);
                                }
                            } else {
                                return '[[Media(' . $aurl . ')]]';
                            }
                        }
                        $m[1] = $info['vid'];
                    }
                    if ($object_prefered) {
                        $classid = "classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'";
                        $movie = "http://videofarm.daum.net/controller/player/VodPlayer.swf";
                        $type = 'type="application/x-shockwave-flash"';
                        $attr = 'allowfullscreen="true" allowScriptAccess="always" flashvars="vid=' . $m[2] . '&playLoc=undefined"';
                        if (empty($mysize)) {
                            $attr .= ' width="500px" height="281px"';
                        }
                        $url[$i] = $movie;
                        $params = "<param name='movie' value='{$movie}'>\n" . "<param name='flashvars' value='vid=" . $m[1] . "&playLoc=undefined'>\n" . "<param name='allowScriptAccess' value='always'>\n" . "<param name='allowFullScreen' value='true'>\n";
                    } else {
                        $iframe = '//videofarm.daum.net/controller/video/viewer/Video.html?play_loc=tvpot' . '&amp;jsCallback=false&amp;wmode=transparent&amp;vid=' . $m[1] . '&amp;autoplay=false&amp;permitWideScreen=true';
                        $attr = 'frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen';
                        if (empty($mysize)) {
                            $attr .= ' width="500px" height="281px"';
                        } else {
开发者ID:ahastudio,项目名称:moniwiki,代码行数:67,代码来源:Play.php

示例9: macro_WikimediaCommons

function macro_WikimediaCommons($formatter, $value, $params = array())
{
    global $DBInfo, $Config;
    // FIXME urldecode for some cases
    $value = urldecode($value);
    if (($p = strpos($value, ',')) !== false) {
        $arg = substr($value, $p + 1);
        $value = substr($value, 0, $p);
        $arg = preg_replace('@\\s*,\\s*@', ',', $arg);
        $arg = preg_replace('@\\s*=\\s*@', '=', $arg);
        $args = explode(',', $arg);
    }
    // check full url
    if (preg_match('@^https?://upload\\.wikimedia\\.org/wikipedia/commons/(thumb/)?./../([^/]+\\.(?:gif|jpe?g|png|svg))(?(1)/(\\d+px)-\\2)@', $value, $m)) {
        $remain = substr($value, strlen($m[0]));
        $value = $m[2];
        if (!empty($m[3])) {
            $width = intval($m[3]);
        }
    }
    $styles = array();
    foreach ($args as $arg) {
        $k = $v = '';
        if (($p = strpos($arg, '=')) !== false) {
            $k = substr($arg, 0, $p);
            $v = substr($arg, $p + 1);
        } else {
            continue;
        }
        $k = strtolower($k);
        switch ($k) {
            case 'width':
            case 'height':
                if (preg_match('@^(\\d+)(px|%)?$@', $v, $m)) {
                    if (isset($m[2]) && $m[2] == '%') {
                        $styles[$k] = $v;
                    } else {
                        $styles[$k] = $v;
                        ${$k} = intval($m[1]);
                    }
                }
                break;
            case 'align':
                $v = strtolower($v);
                if (in_array($v, array('left', 'right', 'center'))) {
                    $addClass = ' img' . ucfirst($v);
                }
                break;
        }
    }
    $common = new Cache_Text('wikicommons');
    $key = $value;
    if (isset($width)) {
        $key .= $width;
    }
    if (isset($height)) {
        $key .= '.h' . $height;
    }
    if (!empty($formatter->refresh) || ($images = $common->fetch($key)) === false) {
        $api_url = 'https://commons.wikimedia.org/w/api.php';
        $data = array('action' => 'query', 'titles' => 'Image:' . $value, 'prop' => 'imageinfo', 'iiprop' => 'extmetadata|url', 'format' => 'json', 'rawcontinue' => '1');
        if (!empty($width)) {
            $data['iiurlwidth'] = min(1280, $width);
        } else {
            if (!empty($height)) {
                $data['iiurlheight'] = min(1280, $height);
            } else {
                // default image width
                $data['iiurlwidth'] = 640;
            }
        }
        require_once dirname(__FILE__) . '/../lib/HTTPClient.php';
        $http = new HTTPClient();
        $save = ini_get('max_execution_time');
        set_time_limit(0);
        $http->sendRequest($api_url, $data, 'POST');
        set_time_limit($save);
        // FIXME
        if ($http->status != 200) {
            return '';
        }
        $res = json_decode($http->resp_body);
        $images = $res->query->pages;
        $common->update($key, $images);
    }
    $image = current($images);
    $image_url = $image->imageinfo[0]->thumburl;
    $desc_url = $image->imageinfo[0]->descriptionurl;
    if (empty($styles['width'])) {
        $styles['width'] = $image->imageinfo[0]->thumbwidth . 'px';
    }
    $style = '';
    foreach ($styles as $k => $v) {
        $style .= $k . ':' . $v . ';';
    }
    if (!empty($style)) {
        $style = ' style="' . $style . '"';
    }
    $copyright = $image->imageinfo[0]->extmetadata->Copyrighted->value;
    $description = $image->imageinfo[0]->extmetadata->ImageDescription->value;
//.........这里部分代码省略.........
开发者ID:hellojo011,项目名称:moniwiki,代码行数:101,代码来源:WikimediaCommons.php

示例10: macro_Pull

function macro_Pull($formatter, $pagename = '', $params = array())
{
    global $DBInfo;
    if (empty($pagename)) {
        $params['retval']['error'] = _("Empty PageName");
        return false;
    }
    if (empty($DBInfo->pull_url)) {
        $params['retval']['error'] = _("Empty \$pull_url");
        return false;
    }
    if (strpos($DBInfo->pull_url, '$PAGE') === false) {
        $url = $DBInfo->pull_url . _rawurlencode($pagename);
    } else {
        $url = preg_replace('/\\$PAGE/', _rawurlencode($pagename), $DBInfo->pull_url);
    }
    $url .= '?action=raw';
    require_once "lib/HTTPClient.php";
    $sz = 0;
    // set default params
    $maxage = !empty($DBInfo->pull_maxage) ? (int) $DBInfo->pull_maxage : 60 * 60 * 24 * 7;
    $timeout = !empty($DBInfo->pull_timeout) ? (int) $DBInfo->pull_timeout : 15;
    $maxage = (int) $maxage;
    // check connection
    $http = new HTTPClient();
    $sc = new Cache_text('mirrorinfo');
    $error = null;
    $headers = array();
    while ($sc->exists($pagename) and time() < $sc->mtime($pagename) + $maxage) {
        $info = $sc->fetch($pagename);
        if ($info == false) {
            break;
        }
        $sz = $info['size'];
        $etag = $info['etag'];
        $lastmod = $info['last-modified'];
        $error = !empty($info['error']) ? $info['error'] : null;
        // already retrived and found some error
        if (empty($params['refresh']) and !empty($error)) {
            return false;
        }
        // conditional get
        $headers['Cache-Control'] = 'maxage=0';
        $headers['If-Modified-Since'] = $lastmod;
        if (empty($DBInfo->pull_no_etag)) {
            $headers['If-None-Match'] = $etag;
        }
        // do not refresh for no error cases
        if (empty($error)) {
            unset($params['refresh']);
        }
        break;
    }
    // get file header
    $http->nobody = true;
    if (!empty($headers)) {
        $http->headers = array_merge($http->headers, $headers);
    }
    $http->sendRequest($url, array(), 'GET');
    if ($http->status == 304) {
        // not modified
        $params['retval']['status'] = 304;
        return true;
    }
    if ($http->status != 200) {
        $params['retval']['error'] = sprintf(_("Invalid Status %d"), $http->status);
        $params['retval']['status'] = $http->status;
        return false;
    } else {
        if (!empty($params['check'])) {
            $params['retval']['status'] = 200;
            return true;
        }
    }
    if (isset($http->resp_headers['content-length'])) {
        $sz = $http->resp_headers['content-length'];
    }
    $etag = '';
    $lastmod = '';
    if (isset($http->resp_headers['etag'])) {
        $etag = $http->resp_headers['etag'];
    }
    if (isset($http->resp_headers['last-modified'])) {
        $lastmod = $http->resp_headers['last-modified'];
    }
    $sc->update($pagename, array('size' => $sz, 'etag' => $etag, 'last-modified' => $lastmod));
    // size info
    if (is_numeric($sz)) {
        $unit = array('Bytes', 'KB', 'MB', 'GB');
        $tmp = $sz;
        for ($i = 0; $i < 4; $i++) {
            if ($tmp <= 1024) {
                break;
            }
            $tmp = $tmp / 1024;
        }
        $hsz = round($tmp, 2) . ' ' . $unit[$i];
    } else {
        $params['retval']['error'] = _("Can't get file size info");
        $params['retval']['mimetype'] = $mimetype;
//.........这里部分代码省略.........
开发者ID:reviforks,项目名称:moniwiki,代码行数:101,代码来源:pull.php


注:本文中的HTTPClient::sendRequest方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。