Exemplo n.º 1
0
 public static function send(Tuitter_Http_Request $req, $method = 'GET')
 {
     $host = $req->getHost();
     $port = $req->getPort();
     $url = $req->getUrl();
     $prms = $req->getPrms();
     $headers = $req->getHeaders();
     $body = '';
     if ($prms) {
         if ($method == 'GET') {
             ksort($prms);
             $url .= '?' . http_build_query($prms);
         } else {
             if ($method == 'POST') {
                 if ($req->isMultipart()) {
                     $boundary = '-TuItTr';
                     $headers['Content-Type'] = "multipart/form-data; boundary={$boundary}";
                     foreach ($prms as $parts) {
                         $body .= "--{$boundary}\r\n";
                         foreach ($parts['header'] as $key => $val) {
                             $body .= "{$key}: {$val}\r\n";
                         }
                         $body .= "\r\n{$parts['body']}\r\n";
                     }
                     $body .= "--{$boundary}--\r\n";
                 } else {
                     $body = http_build_query($prms);
                 }
                 $headers['Content-Length'] = strlen($body);
             }
         }
     }
     $cacheKey = sha1($req->getUser() . '@' . $url);
     if ($method == 'GET') {
         if ($cache = self::getCache($cacheKey, $req)) {
             if ($etag = $cache->getHeader('ETag')) {
                 $headers['If-None-Match'] = $etag;
             }
         }
     }
     $r[] = "{$method} {$url} HTTP/1.1";
     $r[] = "HOST: {$host}";
     foreach ($headers as $key => $val) {
         $r[] = "{$key}: {$val}";
     }
     $r[] = '';
     if ($fp = fsockopen($host, $port)) {
         fputs($fp, implode("\r\n", $r) . "\r\n" . $body);
         $response = '';
         while (!feof($fp)) {
             $response .= fgets($fp, 4096);
         }
         fclose($fp);
         $res = new Tuitter_Http_Response($response);
     }
     if ($method == 'GET') {
         $res = self::putCache($cacheKey, $cache, $res);
     }
     return $res;
 }
Exemplo n.º 2
0
 protected function _requestBasic($url, $host, $opt = array(), $method = 'GET', $auth = true, $multipart = false, $type = ".xml")
 {
     $req = new Tuitter_Http_Request("{$url}{$type}", $host);
     if ($auth) {
         $req->setBasicAuth($this->_user, $this->_pass);
     }
     $headers = array();
     if ($this->_client_name) {
         $headers['X-Twitter-Client'] = $this->_client_name;
     }
     if ($this->_client_version) {
         $headers['X-Twitter-Version'] = $this->_client_version;
     }
     if ($this->_client_url) {
         $headers['X-Twitter-URL'] = $this->_client_url;
     }
     $req->setMultipart($multipart);
     $req->setHeaders($headers);
     $req->setPrms($opt);
     $res = Tuitter_Http::send($req, $method);
     $retry = $this->_retry;
     while ($res->getStatus() >= 500 and $retry--) {
         sleep($this->_retryInterval);
         $res = Tuitter_Http::send($req, $method);
     }
     if ($res->getStatus() >= 400) {
         throw new Exception($this->_getErrMsgByHttp($res));
     }
     return $res->getBody();
 }