Ejemplo n.º 1
0
 /**
  * @return string
  */
 public function getAsHeaderString()
 {
     $cookieParams = ["cookies" => [urlencode($this->getName()) => urlencode($this->getValue())], "extras" => [], "flags" => 0, "expires" => $this->getExpires()->getTimestamp(), "path" => $this->getPath(), "domain" => $this->getDomain()];
     if ($this->getSecure()) {
         $cookieParams['flags'] += HTTP_COOKIE_SECURE;
     }
     if ($this->getHttpOnly()) {
         $cookieParams['flags'] += HTTP_COOKIE_HTTPONLY;
     }
     return \http_build_cookie($cookieParams);
 }
Ejemplo n.º 2
0
/**
 * Test Http functions.
 */
function test_functions()
{
    http_cache_last_modified();
    http_chunked_decode();
    http_deflate();
    http_inflate();
    http_build_cookie();
    http_date();
    http_get_request_body_stream();
    http_get_request_body();
    http_get_request_headers();
    http_match_etag();
    http_match_modified();
    http_match_request_header();
    http_support();
    http_negotiate_charset();
    http_negotiate_content_type();
    http_negotiate_language();
    ob_deflatehandler();
    ob_etaghandler();
    ob_inflatehandler();
    http_parse_cookie();
    http_parse_headers();
    http_parse_message();
    http_parse_params();
    http_persistent_handles_clean();
    http_persistent_handles_count();
    http_persistent_handles_ident();
    http_get();
    http_head();
    http_post_data();
    http_post_fields();
    http_put_data();
    http_put_file();
    http_put_stream();
    http_request_body_encode();
    http_request_method_exists();
    http_request_method_name();
    http_request_method_register();
    http_request_method_unregister();
    http_request();
    http_redirect();
    http_send_content_disposition();
    http_send_content_type();
    http_send_data();
    http_send_file();
    http_send_last_modified();
    http_send_status();
    http_send_stream();
    http_throttle();
    http_build_str();
    http_build_url();
}
Ejemplo n.º 3
0
 protected function createHandle(array $options = array())
 {
     if (isset($options[CURLOPT_COOKIE]) && is_array($options[CURLOPT_COOKIE])) {
         $options[CURLOPT_COOKIE] = http_build_cookie($options[CURLOPT_COOKIE]);
     }
     if (isset($options[CURLOPT_POSTFIELDS]) && is_array($options[CURLOPT_POSTFIELDS])) {
         $options[CURLOPT_POSTFIELDS] = http_build_query($options[CURLOPT_POSTFIELDS]);
     }
     $handle = curl_init();
     curl_setopt_array($handle, $options + $this->defaults);
     curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
     return $handle;
 }
Ejemplo n.º 4
0
 /**
  * cookie 转字符串
  * @param $cookieArr
  * @return string
  *
  */
 protected function cookieArr2Str($cookieArr)
 {
     if (function_exists('http_build_cookie')) {
         return http_build_cookie($cookieArr);
     } else {
         $cookieList = [];
         foreach ($cookieArr as $key => $value) {
             $sItem = $key . '=' . $value;
             $cookieList[] = $sItem;
         }
         return implode(';', $cookieList);
     }
 }
Ejemplo n.º 5
0
 /**
  * Renders the HTTP_Interaction to a string, producing
  *
  *  - Protocol
  *  - Headers
  *  - Body
  *
  * @return  string
  */
 public function render()
 {
     if (!$this->_header->offsetExists('content-type')) {
         // Add the default Content-Type header if required
         $this->_header['content-type'] = Kohana::$content_type . '; charset=' . Kohana::$charset;
     }
     // Set the content length
     $this->headers('content-length', (string) $this->content_length());
     // If Kohana expose, set the user-agent
     if (Kohana::$expose) {
         $this->headers('user-agent', 'Gleez CMS ' . Gleez::VERSION . ' (' . Gleez::CODENAME . ')');
     }
     // Prepare cookies
     if ($this->_cookies) {
         if (extension_loaded('http')) {
             $this->_header['set-cookie'] = http_build_cookie($this->_cookies);
         } else {
             $cookies = array();
             // Parse each
             foreach ($this->_cookies as $key => $value) {
                 $string = $key . '=' . $value['value'] . '; expires=' . date('l, d M Y H:i:s T', $value['expiration']);
                 $cookies[] = $string;
             }
             // Create the cookie string
             $this->_header['set-cookie'] = $cookies;
         }
     }
     $output = $this->_protocol . ' ' . $this->_status . ' ' . Response::$messages[$this->_status] . "\r\n";
     $output .= (string) $this->_header;
     $output .= $this->_body;
     return $output;
 }
Ejemplo n.º 6
0
 /**
  * @return string
  */
 private function _getCookieHeaders()
 {
     $ret = '';
     foreach ($this->cookies as $cn => $c) {
         if ($c['val'] === null) {
             continue;
         }
         $cData = array('cookies' => array($cn => $c['signed'] ? $this->_cookieParser->sign($c['val']) : $c['val']), 'expires' => date('r', time() + (isset($c['expire']) ? $c['expire'] : Maestro::gi()->get('cookie expire'))), 'path' => '/');
         if ($this->secure) {
             $cData['secure'] = true;
         }
         $ret .= http_build_cookie($cData) . ', ';
     }
     return substr($ret, 0, -2);
 }
Ejemplo n.º 7
0
 public function render()
 {
     if (!$this->_header->offsetExists('content-type')) {
         $this->_header['content-type'] = JsonApiApplication::$content_type . '; charset=' . JsonApiApplication::$charset;
     }
     $this->headers('content-length', (string) $this->content_length());
     if (JsonApiApplication::$expose) {
         $this->headers('user-agent', JsonApiApplication::version());
     }
     if ($this->_cookies) {
         if (extension_loaded('http')) {
             $cookies = version_compare(phpversion('http'), '2.0.0', '>=') ? (string) new \http\Cookie($this->_cookies) : http_build_cookie($this->_cookies);
             $this->_header['set-cookie'] = $cookies;
         } else {
             $cookies = array();
             foreach ($this->_cookies as $key => $value) {
                 $string = $key . '=' . $value['value'] . '; expires=' . date('l, d M Y H:i:s T', $value['expiration']);
                 $cookies[] = $string;
             }
             $this->_header['set-cookie'] = $cookies;
         }
     }
     $output = $this->_protocol . ' ' . $this->_status . ' ' . Response::$messages[$this->_status] . "\r\n";
     $output .= (string) $this->_header;
     $output .= $this->_body;
     return $output;
 }
Ejemplo n.º 8
0
 /**
  * Build the header string of the cookies.
  */
 public static function build($cookies, $key)
 {
     $c = $cookies->getAll();
     if (0 === count($c)) {
         return '';
     }
     $headers = '';
     foreach ($c as $ck) {
         foreach ($ck['cookies'] as $name => $val) {
             $ck['cookies'][$name] = \photon\crypto\Sign::dumps($val, $key);
         }
         $headers .= 'Set-Cookie: ' . http_build_cookie($ck) . "\r\n";
     }
     return $headers;
 }
Ejemplo n.º 9
0
 /**
  * 返回cookie参数
  * 
  * @param $params
  * @return type 
  */
 protected function getCookieString($params)
 {
     if (is_array($params)) {
         return http_build_cookie($params);
     }
     return $params;
 }
Ejemplo n.º 10
0
 /**
  * 渲染当前响应对象
  *
  * @return string
  */
 public function render()
 {
     Base::getLog()->debug(__METHOD__ . ' render response object');
     if (!$this->headers('content-type')) {
         $this->headers('content-type', Base::$contentType . '; charset=' . $this->charset);
     }
     $this->headers('content-length', (string) $this->contentLength);
     if (Base::$expose) {
         $this->headers('user-agent', Base::version());
     }
     if ($this->cookies) {
         if (extension_loaded('http')) {
             $this->headers('set-cookie', http_build_cookie($this->cookies));
         } else {
             $cookies = [];
             foreach ($this->cookies as $key => $value) {
                 $string = $key . '=' . $value['value'] . '; expires=' . date('l, d M Y H:i:s T', $value['expiration']);
                 $cookies[] = $string;
             }
             $this->headers('set-cookie', $cookies);
         }
     }
     $output = $this->protocol . ' ' . $this->status . ' ' . Arr::get(Http::$text, $this->status) . "\r\n";
     $output .= $this->message->headerLines;
     $output .= $this->message->body;
     return $output;
 }
Ejemplo n.º 11
0
/**
 * CURL多并发
 * 
 *  $urls = array(
 * 
 *      //post数据
 * 
 *      'post'=>array(
 * 
 *          'name'=>'abc',
 * 
 *          'get'=>true
 * 
 *      ),
 * 
 *      //超时时间,秒
 * 
 *      'timeout'=>120,
 * 
 *      //cookie数据
 * 
 *      'cookies'=>'asos=userCountryIso=CN&topcatid=1000&currencyid=2&currencylabel=USD',
 * 
 *      //header数据
 * 
 *      '$headers'=>array(
 * 
 *          'X-FORWARDED-FOR'=>'127.0.0.1',
 * 
 *          'CLIENT-IP'=>'127.0.0.1'
 * 
 *      ),
 * 
 *      //useragent信息
 * 
 *      'user_agent'=>'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:16.0) Gecko/20121026 Firefox/16.0',
 * 
 *      //请求来源信息
 * 
 *      'referer'=>'http://127.0.0.1/',
 * 
 *      CURLOPT_FOLLOWLOCATION=>0
 * 
 *  );
 * 
 * OR
 * 
 *  $urls = array(
 * 
 *      array(
 * 
 *          'post'=>'asos=userCountryIso=CN&topcatid=1000&currencyid=2&currencylabel=USD',
 * 
 *          'timeout'=>5,
 * 
 *      ),
 * 
 *      array(
 * 
 *          CURLOPT_TIMEOUT=>5,
 * 
 *          CURLOPT_TIMEOUT=>30,
 * 
 *          'cookies'=>'asos=userCountryIso=CN&topcatid=1000&currencyid=2&currencylabel=USD',
 * 
 *          'headers'=>'X-FORWARDED-FOR:127.0.0.1',
 * 
 *      ),
 * 
 *      array(
 * 
 *          CURLOPT_HEADER=>1,
 * 
 *          'user_agent'=>'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:16.0) Gecko/20121026 Firefox/16.0',
 * 
 *          'referer'=>'http://127.0.0.1/'
 * 
 *      )
 * 
 *  );
 * @param array $urls 要请求的url地址数组
 * @param array $options curl请求选项
 * @return array
 */
function curl_multi(array $urls, array $options = null)
{
    if (empty($urls)) {
        return false;
    }
    $init = curl_multi_init();
    $default_option = array(CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => 1, CURLOPT_TIMEOUT => 5, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_FOLLOWLOCATION => 1);
    $options = (array) $options;
    if (array_keys($options) !== range(0, count($options) - 1)) {
        $options = array_fill(0, count($urls), $options);
    }
    foreach ($options as $i => $option) {
        if (isset($option['timeout'])) {
            $timeout = $option['timeout'];
            if ($timeout) {
                $option[CURLOPT_CONNECTTIMEOUT_MS] = $timeout * 1000;
                $option[CURLOPT_TIMEOUT_MS] = $timeout * 1000;
            }
            unset($option['timeout']);
        }
        if (isset($option['post'])) {
            $post = $option['post'];
            if ($post) {
                $option[CURLOPT_POST] = true;
                if (is_array($post) || is_string($post)) {
                    $option[CURLOPT_POSTFIELDS] = $post;
                }
            }
            unset($option['post']);
        }
        if (isset($option['cookies'])) {
            $cookies = $option['cookies'];
            if ($cookies) {
                if (is_array($cookies)) {
                    $cookies = http_build_cookie($cookies);
                }
                if (is_string($cookies) && strlen($cookies)) {
                    $option[CURLOPT_COOKIE] = $cookies;
                }
            }
            unset($option['cookies']);
        }
        if (isset($option['headers'])) {
            $headers = $option['headers'];
            if ($headers) {
                if (is_array($headers)) {
                    foreach ($headers as $k => $v) {
                        if (is_string($k) && strlen($k)) {
                            $headers[$k] = $k . ': ' . $v;
                        } elseif (!strpos(':', $v)) {
                            $headers[$k] = '';
                        }
                    }
                } elseif (!strpos(':', $headers)) {
                    $headers = '';
                }
                if ($headers = array_filter((array) $headers)) {
                }
                $option[CURLOPT_HTTPHEADER] = $headers;
            }
            unset($option['headers']);
        }
        if (isset($option['user_agent'])) {
            $user_agent = $option['user_agent'];
            if (is_string($user_agent)) {
                $option[CURLOPT_USERAGENT] = $user_agent;
            }
            unset($option['user_agent']);
        }
        if (isset($option['referer'])) {
            $referer = $option['referer'];
            if (is_string($referer)) {
                $option[CURLOPT_REFERER] = $referer;
            }
            unset($option['referer']);
        }
        $options[$i] = $option;
    }
    $conn = array();
    foreach ($urls as $k => $url) {
        $ch = curl_init($url);
        $option = !empty($options[$k]) ? $options[$k] + $default_option : $default_option;
        curl_setopt_array($ch, $option);
        curl_multi_add_handle($init, $ch);
        $conn[$k] = $ch;
    }
    do {
        do {
            $mrc = curl_multi_exec($init, $running);
            usleep(10000);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM || curl_multi_select($init) != -1);
    } while ($running and $mrc == CURLM_OK);
    $ret = array();
    foreach ($urls as $k => $url) {
        $ch = $conn[$k];
        $error = curl_error($ch);
        $ret[$k] = $error ? false : curl_multi_getcontent($ch);
        curl_multi_remove_handle($init, $ch);
        curl_close($ch);
    }
    curl_multi_close($init);
    return $ret;
}
Ejemplo n.º 12
0
 /**
  * Starts the HTTP Request and returns or prints the result
  * 
  * 	@param boolean $buffered
  * 	@param boolean $header return/print response headers as well
  * 	@return boolean|string
  */
 public function exec($buffered = true, $headers = false)
 {
     if (!empty($this->data)) {
         if ($this->method === self::METHOD_POST) {
             curl_setopt($this->handle, CURLOPT_POST, true);
             curl_setopt($this->handle, CURLOPT_POSTFIELDS, http_build_query($this->data));
         } else {
             $this->url .= '?' . http_build_query($this->data);
             curl_setopt($this->handle, CURLOPT_HTTPGET, true);
         }
     }
     curl_setopt($this->handle, CURLOPT_FOLLOWLOCATION, (bool) $this->followLocation);
     if (isset($this->timeout)) {
         curl_setopt($this->handle, CURLOPT_TIMEOUT, (int) $this->timeout);
     }
     if (!empty($this->cookie)) {
         curl_setopt($this->handle, CURLOPT_COOKIE, http_build_cookie($this->cookie));
     }
     if ($this->port !== 80) {
         curl_setopt($this->handle, CURLOPT_PORT, (int) $this->port);
     }
     if (!empty($this->referer)) {
         curl_setopt($this->handle, CURLOPT_REFERER, $this->referer);
     }
     if (!empty($this->userAgent)) {
         curl_setopt($this->handle, CURLOPT_USERAGENT, $this->userAgent);
     }
     if (!empty($this->auth)) {
         curl_setopt($this->handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
         curl_setopt($this->handle, CURLOPT_USERPWD, implode(':', $this->auth));
     }
     if (count($this->headers) > 0) {
         curl_setopt($this->handle, CURLOPT_HTTPHEADER, $this->headers);
     }
     if ($buffered) {
         curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, $buffered);
     }
     if ($headers) {
         curl_setopt($this->handle, CURLOPT_HEADER, true);
     }
     // check if url set
     if (empty($this->url)) {
         throw new CURLEmptyURLException();
     }
     curl_setopt($this->handle, CURLOPT_URL, $this->url);
     curl_setopt($this->handle, CURLOPT_COOKIESESSION, true);
     return curl_exec($this->handle);
 }