Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Last update: Jan 30, 2021
- Settings: curl timeout from 60s to 180s
- */
- set_time_limit(600); //10 min + 2x v Curl
- class CurlRequest
- {
- private $ch;
- /**
- * Init curl session
- *
- * $params = array( 'url' => '',
- * 'host' => '',
- * 'header' => '',
- * 'method' => '',
- * 'referer' => '',
- * 'cookie' => '',
- * 'post_fields' => '',
- * ['login' => '',]
- * ['password' => '',]
- * 'timeout' => 0
- * );
- */
- public function init($params)
- {
- $this->ch = curl_init();
- $user_agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:77.0) Gecko/20100101 Firefox/77.0';
- $header = array(
- "Accept: text/xml,application/xml,application/xhtml+xml, text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
- "Accept-Language: ru-ru,ru;q=0.7,en-us;q=0.5,en;q=0.3", "Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7",
- "Keep-Alive: 300");
- if (isset($params['host']) && $params['host']) $header[]="Host: ".$params['host'];
- if (isset($params['header']) && $params['header']) $header[]=$params['header'];
- if(isset($params['port']) && $params['port'])
- @curl_setopt ( $this -> ch, CURLOPT_PORT , 443); //adfp::Adobe Flash Player, Port 443
- @curl_setopt ( $this -> ch , CURLOPT_RETURNTRANSFER , 1 );
- @curl_setopt ( $this -> ch , CURLOPT_VERBOSE , 1 );
- @curl_setopt ( $this -> ch , CURLOPT_HEADER , 1 );
- if ($params['method'] == "HEAD") @curl_setopt($this -> ch,CURLOPT_NOBODY,1);
- @curl_setopt ( $this -> ch, CURLOPT_FOLLOWLOCATION, 1);
- @curl_setopt ( $this -> ch , CURLOPT_HTTPHEADER, $header );
- if ($params['referer']) @curl_setopt ($this -> ch , CURLOPT_REFERER, $params['referer'] );
- @curl_setopt ( $this -> ch , CURLOPT_USERAGENT, $user_agent);
- if ($params['cookie']) @curl_setopt ($this -> ch , CURLOPT_COOKIE, $params['cookie']);
- if ( $params['method'] == "POST" )
- {
- curl_setopt( $this -> ch, CURLOPT_POST, true );
- curl_setopt( $this -> ch, CURLOPT_POSTFIELDS, $params['post_fields'] );
- }
- @curl_setopt( $this -> ch, CURLOPT_URL, $params['url']);
- @curl_setopt ( $this -> ch , CURLOPT_SSL_VERIFYPEER, 0 );
- @curl_setopt ( $this -> ch , CURLOPT_SSL_VERIFYHOST, 0 );
- if (isset($params['login']) & isset($params['password']))
- @curl_setopt($this -> ch , CURLOPT_USERPWD,$params['login'].':'.$params['password']);
- @curl_setopt ( $this -> ch , CURLOPT_TIMEOUT, $params['timeout']);
- }
- /**
- * Make curl request
- *
- * @return array 'header','body','curl_error','http_code','last_url'
- */
- public function exec()
- {
- $response = curl_exec($this->ch);
- $asoc = curl_getinfo($this->ch);
- $error = curl_error($this->ch);
- $result = array( 'header' => '',
- 'body' => '',
- 'curl_error' => '',
- 'http_code' => '',
- 'url2' => '',
- 'last_url' => '' );
- $result['url2'] = $asoc["url"];
- if ( $error != "" ) {
- $result['curl_error'] = $error;
- return $result;
- }
- $header_size = curl_getinfo($this->ch,CURLINFO_HEADER_SIZE);
- $result['header'] = substr($response, 0, $header_size);
- $result['body'] = substr( $response, $header_size );
- $result['http_code'] = curl_getinfo($this -> ch,CURLINFO_HTTP_CODE);
- $result['last_url'] = curl_getinfo($this -> ch,CURLINFO_EFFECTIVE_URL);
- return $result;
- }
- }
- function gHtml($url,$log = true) {
- try {
- $cu = new CurlRequest();
- $params = array('url' => $url,
- 'host' => '',
- 'header' => '',
- 'method' => 'GET', // 'POST','HEAD'
- 'referer' => '',
- 'port' => '',
- 'cookie' => '',
- 'post_fields' => '', // 'var1=value&var2=value
- 'timeout' => 180
- );
- $cu->init($params);
- $result = $cu->exec();
- //echo $result['body'];
- if ($result['curl_error']) throw new Exception($result['curl_error']);
- if ($result['http_code']!='200') throw new Exception("HTTP Code = ".$result['http_code']);
- if (!$result['body']) throw new Exception("Body of file is empty");
- }
- catch (Exception $e) {
- $msg = $e->getMessage();
- if( strpos($msg, "503") !== false )
- if($log) echo $msg . " … url: " . $url . "<br />";
- else
- if($log) echo "{503}";
- return false;
- }
- return $result['body'];
- }
- function gHtmlFileName($url) {
- try {
- $cu = new CurlRequest();
- $params = array('url' => $url,
- 'host' => '',
- 'header' => '',
- 'method' => 'GET', // 'POST','HEAD'
- 'referer' => '',
- 'port' => '',
- 'cookie' => '',
- 'post_fields' => '', // 'var1=value&var2=value
- 'timeout' => 1
- );
- $cu->init($params);
- $result = $cu->exec();
- }
- catch (Exception $e) {
- }
- return $result['url2'];
- }
- function gHtml2($url) {
- try {
- $cu = new CurlRequest();
- $params = array('url' => $url,
- 'host' => '',
- 'header' => '',
- 'method' => 'GET', // 'POST','HEAD'
- 'referer' => '',
- 'port' => '443',
- 'cookie' => '',
- 'post_fields' => '', // 'var1=value&var2=value
- 'timeout' => 180
- );
- $cu->init($params);
- $result = $cu->exec();
- //echo $result['body'];
- if ($result['curl_error']) throw new Exception($result['curl_error']);
- if ($result['http_code']!='200') throw new Exception("HTTP Code = ".$result['http_code']);
- if (!$result['body']) throw new Exception("Body of file is empty");
- }
- catch (Exception $e) {
- echo $e->getMessage();
- return false;
- }
- return $result['body'];
- }
- /* Test if localhost server id off-line */
- function is_connected() {
- $connected = @fsockopen("www.example.com", 80);
- //website, port (try 80 or 443)
- if ($connected){
- $is_conn = true; //action when connected
- fclose($connected);
- } else{
- $is_conn = false; //action in connection failure
- }
- return $is_conn;
- }
- /* Remote file utils: version, filename, size, date of last update, formated filesize */
- function get_product_version($file_name) {
- $key = "P\x00r\x00o\x00d\x00u\x00c\x00t\x00V\x00e\x00r\x00s\x00i\x00o\x00n\x00\x00\x00";
- $fptr = fopen($file_name, "rb");
- $version = "";
- if($fptr !== false) {
- $data = "";
- while (!feof($fptr)) {
- $data .= fread($fptr, 65536);
- if (strpos($data, $key)!==FALSE)
- break;
- $data = substr($data, strlen($data)-strlen($key));
- }
- fclose($fptr);
- if (strpos($data, $key)===FALSE)
- return "";
- $pos = strpos($data, $key)+strlen($key);
- for ($i=$pos; $data[$i]!="\x00"; $i+=2)
- $version .= $data[$i];
- }
- return $version;
- }
- function remote_filename($url) {
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //will stop cURL from verifying the peer's certificate
- curl_setopt($ch, CURLOPT_HEADER, TRUE);
- curl_setopt($ch, CURLOPT_NOBODY, TRUE);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
- $data = curl_exec($ch);
- $fname = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
- curl_close($ch);
- return $fname;
- }
- function remote_file_size($url) {
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //will stop cURL from verifying the peer's certificate
- curl_setopt($ch, CURLOPT_HEADER, TRUE);
- curl_setopt($ch, CURLOPT_NOBODY, TRUE);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
- $data = curl_exec($ch);
- $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
- curl_close($ch);
- return $size;
- }
- function remote_file_size_formated($url) {
- $size = remote_file_size($url);
- if($size != "")
- $size = number_format(trim($size), 0, ".", " ");
- return $size;
- }
- function remote_file_date($url) {
- $curl = curl_init($url);
- //don't fetch the actual page, you only want headers
- curl_setopt($curl, CURLOPT_NOBODY, true);
- //stop it from outputting stuff to stdout
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- // attempt to retrieve the modification date
- curl_setopt($curl, CURLOPT_FILETIME, true);
- $result = curl_exec($curl);
- if ($result === false) {
- return ""; //die (curl_error($curl));
- }
- $timestamp = curl_getinfo($curl, CURLINFO_FILETIME);
- if ($timestamp != -1) { //otherwise unknown
- return date("Y-m-d H:i:s", $timestamp);
- }
- return "";
- }
- function appendFileSize($f,$expectedSize = 0) {
- $size = remote_file_size_formated($f);
- $s = "";
- if( isset($size) ) {
- if($expectedSize != 0) {
- $size2 = str_replace(' ', '', $size);
- if($expectedSize <= $size2)
- $s = " (".$size.")";
- } else
- $s = " (".$size.")";
- }
- return $s;
- }
- /* String operations: trimm */
- function trimAfter($s,$after) {
- if( strlen($after) > 0 ) {
- $p = strpos($s, $after);
- if( $p !== false ) {
- return trim( substr( $s, $p + strlen($after) ) );
- }
- }
- return trim($s);
- }
- function trimBefore($s,$before) {
- if( strlen($before) > 0 ) {
- $p = strpos($s, $before);
- if( $p !== false ) {
- return trim( substr( $s, 0, $p ) );
- }
- }
- return trim($s);
- }
- function trimm($s,$after,$before) { return trimBefore( trimAfter($s,$after) ,$before ); }
- function select($pred, $s, $za) { return trimm($s,$pred,$za); }
- // JSON
- function prettyPrint( $json ) {
- $result = '';
- $level = 0;
- $in_quotes = false;
- $in_escape = false;
- $ends_line_level = NULL;
- $json_length = strlen( $json );
- for( $i = 0; $i < $json_length; $i++ ) {
- $char = $json[$i];
- $new_line_level = NULL;
- $post = "";
- if( $ends_line_level !== NULL ) {
- $new_line_level = $ends_line_level;
- $ends_line_level = NULL;
- }
- if ( $in_escape ) {
- $in_escape = false;
- } else if( $char === '"' ) {
- $in_quotes = !$in_quotes;
- } else if( ! $in_quotes ) {
- switch( $char ) {
- case '}': case ']':
- $level--;
- $ends_line_level = NULL;
- $new_line_level = $level;
- break;
- case '{': case '[':
- $level++;
- case ',':
- $ends_line_level = $level;
- break;
- case ':':
- $post = " ";
- break;
- case " ": case "\t": case "\n": case "\r":
- $char = "";
- $ends_line_level = $new_line_level;
- $new_line_level = NULL;
- break;
- }
- } else if ( $char === '\\' ) {
- $in_escape = true;
- }
- if( $new_line_level !== NULL ) {
- $result .= "\n".str_repeat( " ", $new_line_level );
- }
- $result .= $char.$post;
- }
- return $result;
- }
- function GetJSON($url) {
- $data = gHtml($url);
- return prettyPrint( $data );
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement