Created
October 3, 2016 03:04
-
-
Save nishad/86850f50ea7397fcb1c1e3076590b4f7 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 | |
/** | |
* cURL | |
* | |
* @author CertaiN | |
* @github https://github.com/Certainist/cURL | |
* @license BSD 2-Clause | |
*/ | |
class cURL implements Serializable { | |
/** | |
* Default User-Agent. | |
* | |
* @static | |
* @access public | |
*/ | |
public static $defaultUserAgent = 'Chrome'; | |
private $ch; | |
private $fp; | |
private $userAgent; | |
private $cookie; | |
/** | |
* You have to call parent::__construct() on your extended method. | |
* | |
* @magic | |
* @access public | |
* @param string [$user_agent = null] | |
*/ | |
public function __construct($user_agent = null) { | |
$list = static::getUserAgents(); | |
if (!func_num_args()) { | |
$user_agent = static::$defaultUserAgent; | |
} | |
if (!is_string($user_agent)) { | |
throw new InvalidArgumentException('User-Agent value type must be string.'); | |
} | |
if (!is_array($list)) { | |
throw new DomainException('static::getUserAgents() must return 1 dimentional assoc.'); | |
} | |
if (!array_key_exists($user_agent, $list)) { | |
throw new InvalidArgumentException('Unknown User-Agent.'); | |
} | |
if (!is_string($list[$user_agent])) { | |
throw new DomainException('static::getUserAgents() return array must contain string values.'); | |
} | |
$this->userAgent = $list[$user_agent]; | |
$this->init(); | |
} | |
/** | |
* For GET requests. | |
* | |
* @access public | |
* @param string $url | |
* @param mixed [&$info = null] Set result of curl_getinfo(). | |
* @return string Response body. | |
*/ | |
public function get($url, &$info = null) { | |
if (!is_string($url)) { | |
throw new InvalidArgumentException('URL value type must be string.'); | |
} | |
if (!is_resource($this->ch)) { | |
throw new BadMethodCallException('cURL resource is not initialized'); | |
} | |
curl_setopt_array($this->ch, array( | |
CURLOPT_URL => $url, | |
CURLOPT_HTTPGET => true, | |
)); | |
return $this->exec($info); | |
} | |
/** | |
* For POST requests. | |
* | |
* @access public | |
* @param string $url | |
* @param mixed $params Query string or associative array. | |
* @param mixed [&$info = null] Set result of curl_getinfo(). | |
* @return string Response body. | |
*/ | |
public function post($url, $params, &$info = null) { | |
if (!is_string($url)) { | |
throw new InvalidArgumentException('URL value type must be string.'); | |
} | |
if (!is_resource($this->ch)) { | |
throw new BadMethodCallException('cURL resource is not initialized'); | |
} | |
curl_setopt_array($this->ch, array( | |
CURLOPT_URL => $url, | |
CURLOPT_POST => true, | |
CURLOPT_POSTFIELDS => $params, | |
)); | |
return $this->exec($info); | |
} | |
/** | |
* Clear cookies. | |
* | |
* @access public | |
*/ | |
public function clearCookies() { | |
if (!is_resource($this->ch)) { | |
throw new BadMethodCallException('cURL resource is not initialized'); | |
} | |
ftruncate($this->fp, 0); | |
} | |
/** | |
* Return the list of User-Agents. | |
* You can extend this method. | |
* | |
* @static | |
* @access protected | |
* @return array Associative array. | |
*/ | |
protected static function getUserAgents() { | |
return array( | |
'Chrome' => | |
'Mozilla/5.0 (Windows NT 6.1) ' . | |
'AppleWebKit/537.36 (KHTML, like Gecko) ' . | |
'Chrome/28.0.1500.63 Safari/537.36' | |
, | |
'Firefox' => | |
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) ' . | |
'Gecko/20100101 Firefox/9.0.1' | |
, | |
'Android' => | |
'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03S) ' . | |
'AppleWebKit/535.19 (KHTML, like Gecko) ' . | |
'Chrome/18.0.1025.166 Safari/535.19' | |
, | |
'iOS' => | |
'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) ' . | |
'AppleWebKit/536.26 (KHTML, like Gecko) ' . | |
'Version/6.0 Mobile/10A403 Safari/8536.25' | |
, | |
'Windows Phone' => | |
'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; ' . | |
'Trident/5.0; IEMobile/9.0; ' . | |
'FujitsuToshibaMobileCommun; IS12T; KDDI)' | |
, | |
'Internet Explorer' => | |
'Mozilla/5.0 (Windows NT 6.3; WOW64; ' . | |
'Trident/7.0; Touch; rv:11.0) like Gecko' | |
, | |
); | |
} | |
/** | |
* Serialize your own properties. | |
* You can extend this method. | |
* | |
* @access protected | |
* @return mixed | |
*/ | |
protected function userSerialize() { return null; } | |
/** | |
* Unserialize your own properties. | |
* You can extend this method. | |
* | |
* @param anything $data | |
* @access protected | |
* @return mixed | |
*/ | |
protected function userUnserialize($data) { } | |
/** | |
* You have to call parent::__destruct() on your extended method. | |
* | |
* @magic | |
* @access public | |
*/ | |
public function __destruct() { | |
if (is_resource($this->ch)) { | |
curl_close($this->ch); | |
} | |
if (is_resource($this->fp)) { | |
$this->cookie = stream_get_contents($this->fp); | |
fclose($this->fp); | |
} | |
} | |
final public function serialize() { | |
$this->__destruct(); | |
$this->init($this->cookie); | |
return serialize(array( | |
$this->userAgent, | |
$this->cookie, | |
$this->userSerialize(), | |
)); | |
} | |
final public function unserialize($data) { | |
if ( | |
!$data = @unserialize($data) or | |
!array_key_exists(0, $data) or | |
!array_key_exists(1, $data) or | |
!array_key_exists(2, $data) or | |
!in_array($data[0], static::getUserAgents(), true) or | |
!is_string($data[1]) | |
) { | |
throw new UnexpectedValueException('Invalid serial'); | |
} | |
$this->userAgent = $data[0]; | |
$this->init($data[1]); | |
$this->userUnserialize($data[2]); | |
} | |
private function exec(&$info) { | |
$ret = curl_exec($this->ch); | |
$info = curl_getinfo($this->ch); | |
return $ret; | |
} | |
private function init($data = '') { | |
$this->fp = tmpfile(); | |
if ($data !== '') { | |
fwrite($this->fp, $data); | |
rewind($this->fp); | |
} | |
$info = stream_get_meta_data($this->fp); | |
$cookie_uri = $info['uri']; | |
$this->ch = curl_init(); | |
curl_setopt_array($this->ch, array( | |
CURLINFO_HEADER_OUT => true, | |
CURLOPT_AUTOREFERER => true, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_SSL_VERIFYPEER => false, | |
CURLOPT_CONNECTTIMEOUT => 10, | |
CURLOPT_TIMEOUT => 15, | |
CURLOPT_MAXREDIRS => 5, | |
CURLOPT_COOKIEFILE => $cookie_uri, | |
CURLOPT_COOKIEJAR => $cookie_uri, | |
CURLOPT_ENCODING => 'gzip, deflate', | |
CURLOPT_USERAGENT => $this->userAgent, | |
CURLOPT_HTTPHEADER => array( | |
'Accept: ' . | |
'text/html,' . | |
'application/xhtml+xml,' . | |
'application/xml' . | |
';q=0.9,*/*;q=0.8' | |
, | |
'Accept-Language: ' . | |
'ja,en-us;q=0.7,en;q=0.3' | |
, | |
), | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example with nicovideo
Output
It's possible to serialize and maintain Cookie just as it is.