get_redirect($url, $content); return $r[1]; } /* Handle Location redirect */ public function get_redirect($url, $content = NULL) { $r = $this->get_basic($url, $content); preg_match('`Location: ([^\r]+)`', $r[0], $location); if (count($location) > 0) { $r = $this->get_redirect($location[1]); } return $r; } /* Get a file from the url with the post content. It handles cookies */ function get_basic($url, $content = NULL) { /* Open the socket */ $url_parsed = parse_url($url); if (!isset($url_parsed['port'])) { if ($url_parsed['scheme'] == 'http') { $url_parsed['port'] = 80; } else if ($url_parsed['scheme'] == 'https') { $url_parsed['port'] = 443; } } while (!($fd = fsockopen($url_parsed['host'], $url_parsed['port']))) { // Retry } /* Generate the Query */ $query = ''; if (isset($url_parsed['query'])) { $query = $url_parsed['query']; } if (isset($content)) { $method = 'POST'; } else { $method = 'GET'; } if (isset($query) && strlen($query) > 0) { $header = $method." ".$url_parsed['path'].'?'.$query." HTTP/1.1\r\n"; } else { $header = $method." ".$url_parsed['path']." HTTP/1.1\r\n"; } // $header .= "Host: ".$url_parsed['host']."\r\n"; // $header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6\r\n"; // $header .= "Referer: ".$url_parsed['scheme'].'://'.$url_parsed['host'].$url_parsed['path']."\r\n"; $cookie = $this->dumpCookies(); if (isset($cookie)) { $header .= "Cookie: ".$cookie."\r\n"; } if (isset($content)) { $header .= "Content-Type: text/xml\r\n"; $header .= "User-Agent: Blizzard Web Client\r\n"; $header .= "Cache-Control: no-cache\r\n"; $header .= "Content-Length: ".strlen($content)."\r\n"; } $header .= "Connection: close\r\n\r\n"; if (isset($content)) { $header .= $content; } echo htmlentities($header) . "

"; fputs($fd, $header); /* Handle the results */ $content = ''; $header = ''; $is_content = false; while ($line = fgets($fd)) { echo $line; if ($is_content) { $content .= $line; } else { $header .= $line; } if (strlen($line) <= 2) { $is_content = true; } } $this->handleCookies($header); fclose($fd); return array($header, $content); } /* Read the HTTP Header Response and Sets the cookies accordingly */ private function handleCookies($header) { preg_match_all('`Set-Cookie: ([^=]+)=([^;]+);`', $header, $out); foreach ($out[1] as $key => $val) { if ($out[2][$key] == 'delete') { unset($this->cookies[$out[1][$key]]); } else { $this->cookies[$out[1][$key]] = $out[2][$key]; } } } /* Convert the cookie array into a key=val; format */ private function dumpCookies() { if (count($this->cookies) == 0) { return NULL; } $c = ''; foreach($this->cookies as $key => $val) { $c .= $key.'='.$val.'; '; } return substr($c, 0, strlen($c) - 2); } } ?>