Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class WebClient {
- private $ch;
- private $url;
- private $headers;
- private $content;
- private $cookies = array();
- private $info;
- private $queryString;
- ###############
- ### SETTERS ###
- ###############
- public function SetUrl($url){
- if (!is_string($url))
- throw new InvalidArgumentException('WebClient::SetQueryString expects parameter 1 to be string. '.gettype($queryString).' given.');
- $this->url = $url;
- }
- public function SetQueryString($queryString) {
- if (!is_string($queryString))
- throw new InvalidArgumentException('WebClient::SetQueryString expects parameter 1 to be string. '.gettype($queryString).' given.');
- $this->queryString = $queryString;
- }
- public function SetOpt($option, $value) {
- curl_setopt($this->ch, $option, $value);
- }
- ###############
- ### GETTERS ###
- ###############
- public function GetUrl() {
- return $this->url;
- }
- public function GetQueryString() {
- return $this->queryString;
- }
- public function GetUrlWithQueryString() {
- $return = $this->url;
- if ($this->queryString !== null) {
- if (strpos($return, '?') !== false) {
- $return .= '&'.$this->queryString;
- }
- else {
- $return .= '?'.$this->queryString;
- }
- }
- return $return;
- }
- public function GetHeaders() {
- return $this->headers;
- }
- public function GetContent() {
- return $this->content;
- }
- public function GetCookies() {
- return $this->cookies;
- }
- public function GetCookiesAsString() {
- $cookies = array();
- foreach ($this->cookies as $key => $value) {
- $cookies[] = $key.'='.$value;
- }
- return implode(';', $cookies);
- }
- public function GetInfo() {
- return $this->info;
- }
- ######################
- ### Public Methods ###
- ######################
- public function MakePostRequest($values) {
- if (!is_array($values) && !is_string($values))
- throw new InvalidArgumentException('WebClient::MakePostRequest expects parameter 1 to be string or array. "'.gettype($values).'" given."');
- $this->SetOpt(CURLOPT_POST, true);
- $this->SetOpt(CURLOPT_POSTFIELDS, $values);
- return $this->Execute();
- }
- public function MakeGetRequest() {
- return $this->Execute();
- }
- ############
- ### Ctor ###
- ############
- public function __construct($url = null){
- $this->url = $url;
- $this->InitializeResource();
- }
- ###############
- ### Helpers ###
- ###############
- private function InitializeResource() {
- $this->ch = curl_init();
- $this->SetOpt(CURLOPT_HEADER, 1);
- $this->SetOpt(CURLOPT_RETURNTRANSFER, 1);
- }
- private function Execute() {
- $this->SetOpt(CURLOPT_URL, $this->GetUrlWithQueryString());
- $this->SetOpt(CURLOPT_COOKIE, $this->GetCookiesAsString());
- $response = curl_exec($this->ch);
- $info = curl_getinfo($this->ch);
- curl_close($this->ch);
- if ($response === false)
- return false;
- $this->info = $info;
- $this->headers = mb_substr($response, 0, $this->info['header_size']);
- $this->content = mb_substr($response, $this->info['header_size'], mb_strlen($response));
- $this->GetCookiesFromHeader();
- $this->InitializeResource();
- return true;
- }
- private function GetCookiesFromHeader() {
- $headerInfo = $this->ParseHTTPHeaders();
- $cookieInfo = isset($headerInfo['Set-Cookie']) ? $headerInfo['Set-Cookie'] : null;
- $this->GetCookiesFromCookieInfo($cookieInfo);
- }
- private function GetCookiesFromCookieInfo($cookieInfo)
- {
- if (is_string($cookieInfo)) {
- $items = explode(';', $cookieInfo);
- foreach ($items as $item) {
- preg_match_all('/\s*(\w+)\s*=\s*(\w+)/', $item, $matches); //Regex might be improved
- if (count($matches[1]) > 0 && count($matches[2]) > 0)
- $this->cookies = array_merge(array_combine($matches[1], $matches[2]), $this->cookies);
- }
- } else if (is_array($cookieInfo)) {
- foreach ($cookieInfo as $value) {
- $this->GetCookiesFromCookieInfo($value);
- }
- }
- }
- private function ParseHTTPHeaders() {
- $return = array();
- $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $this->headers));
- foreach($fields as $field) {
- if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
- $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
- if( isset($return[$match[1]]) ) {
- $return[$match[1]] = array($return[$match[1]], $match[2]);
- } else {
- $return[$match[1]] = trim($match[2]);
- }
- }
- }
- return $return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement