Last active
December 8, 2016 12:52
-
-
Save kaz/93b92f5fb08fab34634a009b4949fec9 to your computer and use it in GitHub Desktop.
cookie_parser.php
This file contains 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 | |
class SimpleClient { | |
private $cookies = []; | |
public function get($url){ | |
return $this->request("GET", $url, null); | |
} | |
public function post($url, $param){ | |
return $this->request("POST", $url, $param); | |
} | |
private function request($method, $url, $param){ | |
$response = file_get_contents($url, false, $this->buildContext($method, $url, $param)); | |
$this->parseCookie($http_response_header); | |
foreach($http_response_header as $item){ | |
if(preg_match("/^Location:(.+)/si", $item, $m)){ | |
return $this->get(trim($m[1])); | |
} | |
} | |
return $response; | |
} | |
private function buildContext($method, $url, $param){ | |
$http = [ | |
"method" => $method, | |
"follow_location" => 0 | |
]; | |
$headers = [ | |
"Cookie: " . $this->buildCookieHeader($url) | |
]; | |
if($param){ | |
$http["content"] = http_build_query($param); | |
$headers[] = "Content-Type: application/x-www-form-urlencoded"; | |
$headers[] = "Content-Length: " . strlen($http["content"]); | |
} | |
$http["header"] = implode("\r\n", $headers); | |
return stream_context_create(compact("http")); | |
} | |
private function buildCookieHeader($url){ | |
$url = parse_url($url); | |
if(!isset($url["path"])){ | |
$url["path"] = "/"; | |
} | |
$cookieHeader = []; | |
foreach($this->cookies as $item){ | |
if(strpos($item["domain"], ".") === 0){ | |
$item["domain"] = substr($item["domain"], 1); | |
} | |
if(!empty($item["domain"]) && substr($url["host"], -strlen($item["domain"])) !== $item["domain"]){ | |
continue; | |
} | |
if(!empty($item["path"]) && strpos($url["path"], $item["path"]) !== 0){ | |
continue; | |
} | |
$cookieHeader[] = $item["name"] . "=" . $item["value"]; | |
} | |
return implode("; ", $cookieHeader); | |
} | |
private function parseCookie($http_response_header){ | |
$setCookie = ""; | |
foreach($http_response_header as $item){ | |
if(preg_match("/^Set-Cookie:(.+)/si", $item, $m)){ | |
$setCookie .= $m[1] . ";"; | |
} | |
} | |
$now = null; | |
$cookies = []; | |
foreach(explode(";", $setCookie) as $item){ | |
$item = array_map("trim", explode("=", $item)); | |
$key = strtolower($item[0]); | |
if($key == "expires"){ | |
$now["expiry"] = strtotime($item[1]); | |
}else if($key == "max-age"){ | |
$now["expiry"] = time() + $item[1]; | |
}else if($key == "path"){ | |
$now["path"] = $item[1]; | |
}else if($key == "domain"){ | |
$now["domain"] = $item[1]; | |
}else if($key == "secure"){ | |
$now["secure"] = true; | |
}else if($key == "httponly"){ | |
$now["httponly"] = true; | |
}else if(!empty($key)){ | |
if($now != null){ | |
$cookies[] = $now; | |
} | |
$now = [ | |
"name" => $item[0], | |
"value" => $item[1], | |
"expiry" => 0, | |
"path" => "", | |
"domain" => "", | |
"secure" => false, | |
"httponly" => false | |
]; | |
} | |
} | |
if($now != null){ | |
$cookies[] = $now; | |
} | |
$cookieMap = []; | |
foreach($this->cookies as $item){ | |
$cookieMap[$item["name"]] = $item; | |
} | |
foreach($cookies as $item){ | |
$cookieMap[$item["name"]] = $item; | |
} | |
$this->cookies = array_values($cookieMap); | |
} | |
public function exportCookie(){ | |
$cookies = []; | |
foreach($this->cookies as $item){ | |
if($item["expiry"] > time()){ | |
$cookies[] = $item; | |
} | |
} | |
return $cookies; | |
} | |
public function importCookie($cookies){ | |
$this->cookies = $cookies; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment