Esempio n. 1
0
 public function HttpRequestGet($url)
 {
     $parsedUrl = parse_url($url);
     if (!isset($parsedUrl['port'])) {
         if ($parsedUrl['scheme'] == 'http') {
             $parsedUrl['port'] = 80;
         } elseif ($parsedUrl['scheme'] == 'https') {
             $parsedUrl['port'] = 443;
         }
     }
     $parsedUrl['query'] = isset($parsedUrl['query']) ? $parsedUrl['query'] : '';
     $parsedUrl['protocol'] = $parsedUrl['scheme'] . '://';
     $eol = "\r\n";
     $headers = "GET " . $url . " HTTP/1.0" . $eol . "Host: " . $parsedUrl['host'] . $eol;
     // "Referer: ".$url['protocol'].$url['host'].$url['path'].$eol.
     if (isset($this->cookie)) {
         $headers .= "Cookie: " . $this->cookie . $eol;
     }
     $headers .= "Connection: close" . $eol . $eol;
     //      echo '<textarea style="width:900px; height:200px; border: 1px solid gold;">';
     //      echo $headers;
     //      echo '</textarea>';
     //
     //      print_r( $parsedUrl );
     //
     //      echo "<hr/>".gettype($parsedUrl['port'])."<hr/>";
     $response = new HTTPResponse();
     $result = '';
     try {
         //$r = file_get_contents($url);
         // TODO: poder devolver numero de error y errstr.
         $fp = fsockopen($parsedUrl['host'], $parsedUrl['port'], $errno, $errstr, $this->timeout);
         if ($fp && is_resource($fp)) {
             fputs($fp, $headers);
             // Envia pedido
             while (!feof($fp)) {
                 //$result .= fgets($fp, self::BUFF_SIZE);
                 $result .= fread($fp, self::BUFF_SIZE);
             }
         }
         fclose($fp);
         // TODO: si hay keepalive, no cerrar el socket...
     } catch (Exception $e) {
         $response->createResponse(array('HTTP/1.1 404 Not Found'));
     }
     // TODO: probar con HTTPS (ssl)
     //      $this->_fp = fsockopen(($this->_protocol == 'https' ? 'ssl://' : '') . $this->_host, $this->_port);
     //      fwrite($this->_fp, $req);
     //      while(is_resource($this->_fp) && $this->_fp && !feof($this->_fp)) $response .= fread($this->_fp, self::BUFF_SIZE);
     //
     //      fclose($this->_fp);
     // Esto de que sirve? siempre da false y nunca entra porque headers siempre tiene algo.
     if (!isset($headers)) {
         //removes headers
         $pattern = "/^.*\r\n\r\n/s";
         $result = preg_replace($pattern, '', $result);
     }
     if ($result !== '') {
         $r = preg_split('/\\r\\n/', $result);
         $response->createResponse($r);
     } else {
         // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
         $response->createResponse(array('HTTP/1.1 503 Service Unavailable'));
     }
     //      echo '<textarea style="width:900px; height:300px; border: 1px solid green;">';
     //      print_r( $response );
     //      echo '</textarea>';
     $responseHeaders = $response->getHeaders();
     if (isset($responseHeaders['Set-Cookie'])) {
         $this->setCookie($responseHeaders['Set-Cookie']);
     }
     return $response;
 }