Created
April 8, 2013 13:20
-
-
Save rubensayshi/5336719 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function extractHeaders($responseHeader) { | |
| $responseCookies = array(); | |
| $responseHeaders = array(); | |
| $responseHeader = str_replace("\r\n", "\n", $responseHeader); | |
| // explode and parse the headers | |
| foreach (explode("\n", $responseHeader) as $line) { | |
| $line = explode(':', $line, 2); | |
| if (count($line) != 2) { | |
| continue; | |
| } | |
| list($k, $v) = $line; | |
| // cookies \o/ nomnomnom | |
| if (strtolower(trim($k)) == 'set-cookie') { | |
| $cookiesplit = explode("=", $v); | |
| if (count($cookiesplit) < 2) { | |
| continue; | |
| } else if (count($cookiesplit) > 2) { | |
| // this is dirty as @&#%@ but let's roll with this for now | |
| $cookiesplit = array($cookiesplit[0], reset(explode(" ", $cookiesplit[1]))); | |
| } | |
| if (substr($cookiesplit[1], -1, 1) == ";") { | |
| $cookiesplit[1] = substr($cookiesplit[1], 0, -1); | |
| } | |
| $responseCookies[trim($cookiesplit[0])] = trim($cookiesplit[1]); | |
| } else { | |
| $responseHeaders[trim($k)] = trim($v); | |
| } | |
| } | |
| return array($responseHeaders, $responseCookies); | |
| } | |
| $myCookie = tempnam("/tmp", "myCookie"); | |
| $myAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"; | |
| $myEmail = '[email protected]'; | |
| $myPassword = 'yourpass'; | |
| $url_login = "https://account.guildwars2.com/login"; | |
| $url_data = "https://tradingpost-live.ncplatform.net/ws/search.json?ids=24341"; | |
| $post_info = http_build_query(array('email' => $myEmail, 'password' => $myPassword)); | |
| //Login | |
| $ch = curl_init(); | |
| curl_setopt($ch, CURLOPT_URL, $url_login); | |
| curl_setopt($ch, CURLOPT_COOKIEJAR, $myCookie); | |
| curl_setopt($ch, CURLOPT_USERAGENT, $myAgent); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
| curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
| curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
| curl_setopt($ch, CURLOPT_FAILONERROR, false); | |
| curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); | |
| curl_setopt($ch, CURLOPT_POSTFIELDS, $post_info); | |
| curl_setopt($ch, CURLOPT_POST, 1); | |
| curl_setopt($ch, CURLOPT_HEADER, true); | |
| curl_setopt($ch, CURLOPT_REFERER, $url_login); | |
| curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
| "Accept-Language: en-us,en;q=0.5", | |
| )); | |
| $result = curl_exec($ch); | |
| $info = curl_getinfo($ch); | |
| // retrieve header string based on reponse info | |
| $responseHeaders = trim(substr($result, 0, $info['header_size'])); | |
| // retrieve body string based on reponse info | |
| $responseBody = substr($result, $info['header_size']); | |
| $responseHeaders = explode("\r\n\r\n", $responseHeaders); | |
| $session_key = null; | |
| foreach ($responseHeaders as $responseHeader) { | |
| list($headers, $cookies) = extractHeaders($responseHeader); | |
| if (isset($cookies['s'])) { | |
| $session_key = $cookies['s']; | |
| break; | |
| } | |
| } | |
| if (!$session_key) { | |
| echo "failed to retrieve session_key :( \n"; | |
| exit(1); | |
| } | |
| //Fetch data | |
| curl_setopt($ch, CURLOPT_URL, "{$url_data}"); | |
| curl_setopt($ch, CURLOPT_COOKIEFILE, $myCookie); | |
| curl_setopt($ch, CURLOPT_USERAGENT, $myAgent); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
| curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
| curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
| curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); | |
| curl_setopt($ch, CURLOPT_COOKIE, "s={$session_key}"); | |
| curl_setopt($ch, CURLOPT_REFERER, $url_data); | |
| curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
| "Accept-Language: en-us,en;q=0.5", | |
| )); | |
| curl_setopt($ch, CURLOPT_POST, 0); | |
| $result = curl_exec($ch); | |
| // Close curl and deal with the result | |
| curl_close($ch); | |
| json_decode($result, true); | |
| echo $result; | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment