Advertisement
Guest User

CurlRequest.php

a guest
Feb 24th, 2021
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.92 KB | None | 0 0
  1. <?php
  2. /*
  3.     Last update: Jan 30, 2021
  4.     Settings: curl timeout from 60s to 180s
  5. */
  6. set_time_limit(600); //10 min + 2x v Curl
  7.  
  8. class CurlRequest
  9. {
  10.   private $ch;
  11.   /**
  12.    * Init curl session
  13.    *
  14.    * $params = array( 'url' => '',
  15.    *                  'host' => '',
  16.    *                  'header' => '',
  17.    *                  'method' => '',
  18.    *                  'referer' => '',
  19.    *                  'cookie' => '',
  20.    *                  'post_fields' => '',
  21.    *                     ['login' => '',]
  22.    *                     ['password' => '',]
  23.    *                  'timeout' => 0
  24.    *                 );
  25.    */                
  26.   public function init($params)
  27.   {
  28.     $this->ch = curl_init();
  29.     $user_agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:77.0) Gecko/20100101 Firefox/77.0';
  30.     $header = array(
  31.     "Accept: text/xml,application/xml,application/xhtml+xml, text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
  32.     "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",
  33.     "Keep-Alive: 300");
  34.     if (isset($params['host']) && $params['host'])     $header[]="Host: ".$params['host'];
  35.     if (isset($params['header']) && $params['header']) $header[]=$params['header'];
  36.  
  37.     if(isset($params['port']) && $params['port'])
  38.       @curl_setopt ( $this -> ch, CURLOPT_PORT , 443); //adfp::Adobe Flash Player, Port 443
  39.  
  40.     @curl_setopt ( $this -> ch , CURLOPT_RETURNTRANSFER , 1 );
  41.     @curl_setopt ( $this -> ch , CURLOPT_VERBOSE , 1 );
  42.     @curl_setopt ( $this -> ch , CURLOPT_HEADER , 1 );
  43.    
  44.     if ($params['method'] == "HEAD") @curl_setopt($this -> ch,CURLOPT_NOBODY,1);
  45.     @curl_setopt ( $this -> ch, CURLOPT_FOLLOWLOCATION, 1);
  46.     @curl_setopt ( $this -> ch , CURLOPT_HTTPHEADER, $header );
  47.     if ($params['referer'])    @curl_setopt ($this -> ch , CURLOPT_REFERER, $params['referer'] );
  48.     @curl_setopt ( $this -> ch , CURLOPT_USERAGENT, $user_agent);
  49.     if ($params['cookie'])    @curl_setopt ($this -> ch , CURLOPT_COOKIE, $params['cookie']);
  50.  
  51.     if ( $params['method'] == "POST" )
  52.     {
  53.         curl_setopt( $this -> ch, CURLOPT_POST, true );
  54.         curl_setopt( $this -> ch, CURLOPT_POSTFIELDS, $params['post_fields'] );
  55.     }
  56.     @curl_setopt( $this -> ch, CURLOPT_URL, $params['url']);
  57.     @curl_setopt ( $this -> ch , CURLOPT_SSL_VERIFYPEER, 0 );
  58.     @curl_setopt ( $this -> ch , CURLOPT_SSL_VERIFYHOST, 0 );
  59.     if (isset($params['login']) & isset($params['password']))
  60.         @curl_setopt($this -> ch , CURLOPT_USERPWD,$params['login'].':'.$params['password']);
  61.     @curl_setopt ( $this -> ch , CURLOPT_TIMEOUT, $params['timeout']);
  62.   }
  63.  
  64.   /**
  65.    * Make curl request
  66.    *
  67.    * @return array  'header','body','curl_error','http_code','last_url'
  68.    */
  69.   public function exec()
  70.   {
  71.     $response = curl_exec($this->ch);
  72.     $asoc = curl_getinfo($this->ch);
  73.     $error = curl_error($this->ch);
  74.     $result = array( 'header' => '',
  75.                      'body' => '',
  76.                      'curl_error' => '',
  77.                      'http_code' => '',
  78.                      'url2' => '',
  79.                      'last_url' => '' );
  80.  
  81.     $result['url2'] = $asoc["url"];
  82.  
  83.     if ( $error != "" ) {
  84.         $result['curl_error'] = $error;
  85.         return $result;
  86.     }
  87.  
  88.     $header_size = curl_getinfo($this->ch,CURLINFO_HEADER_SIZE);
  89.     $result['header'] = substr($response, 0, $header_size);
  90.     $result['body'] = substr( $response, $header_size );
  91.     $result['http_code'] = curl_getinfo($this -> ch,CURLINFO_HTTP_CODE);
  92.     $result['last_url'] = curl_getinfo($this -> ch,CURLINFO_EFFECTIVE_URL);
  93.     return $result;
  94.   }
  95. }
  96.  
  97. function gHtml($url,$log = true) {
  98.   try {
  99.     $cu = new CurlRequest();
  100.     $params = array('url' => $url,
  101.     'host' => '',
  102.     'header' => '',
  103.     'method' => 'GET', // 'POST','HEAD'
  104.     'referer' => '',
  105.     'port' => '',
  106.     'cookie' => '',
  107.     'post_fields' => '', // 'var1=value&var2=value
  108.     'timeout' => 180
  109.     );
  110.      
  111.     $cu->init($params);
  112.     $result = $cu->exec();
  113.     //echo $result['body'];    
  114.     if ($result['curl_error']) throw new Exception($result['curl_error']);
  115.     if ($result['http_code']!='200') throw new Exception("HTTP Code = ".$result['http_code']);
  116.     if (!$result['body']) throw new Exception("Body of file is empty");
  117.   }
  118.  
  119.   catch (Exception $e) {
  120.     $msg = $e->getMessage();
  121.     if( strpos($msg, "503") !== false )
  122.       if($log) echo $msg . " &hellip; url: " . $url . "<br />";
  123.     else
  124.       if($log) echo "{503}";
  125.     return false;
  126.   }
  127.   return $result['body'];
  128. }
  129.  
  130. function gHtmlFileName($url) {
  131.   try {
  132.     $cu = new CurlRequest();
  133.     $params = array('url' => $url,
  134.     'host' => '',
  135.     'header' => '',
  136.     'method' => 'GET', // 'POST','HEAD'
  137.     'referer' => '',
  138.     'port' => '',
  139.     'cookie' => '',
  140.     'post_fields' => '', // 'var1=value&var2=value
  141.     'timeout' => 1
  142.     );
  143.  
  144.     $cu->init($params);
  145.     $result = $cu->exec();
  146.   }
  147.  
  148.   catch (Exception $e) {
  149.   }
  150.   return $result['url2'];
  151. }
  152.  
  153. function gHtml2($url) {
  154.   try {
  155.     $cu = new CurlRequest();
  156.     $params = array('url' => $url,
  157.     'host' => '',
  158.     'header' => '',
  159.     'method' => 'GET', // 'POST','HEAD'
  160.     'referer' => '',
  161.     'port' => '443',
  162.     'cookie' => '',
  163.     'post_fields' => '', // 'var1=value&var2=value
  164.     'timeout' => 180
  165.     );
  166.  
  167.     $cu->init($params);
  168.     $result = $cu->exec();
  169.     //echo $result['body'];
  170.     if ($result['curl_error']) throw new Exception($result['curl_error']);
  171.     if ($result['http_code']!='200') throw new Exception("HTTP Code = ".$result['http_code']);
  172.     if (!$result['body']) throw new Exception("Body of file is empty");
  173.   }
  174.  
  175.   catch (Exception $e) {
  176.     echo $e->getMessage();
  177.     return false;
  178.   }
  179.   return $result['body'];
  180. }
  181.  
  182. /* Test if localhost server id off-line */
  183.  
  184. function is_connected() {
  185.     $connected = @fsockopen("www.example.com", 80);
  186.                                         //website, port  (try 80 or 443)
  187.     if ($connected){
  188.         $is_conn = true; //action when connected
  189.         fclose($connected);
  190.     } else{
  191.         $is_conn = false; //action in connection failure
  192.     }
  193.     return $is_conn;
  194. }
  195.  
  196. /* Remote file utils: version, filename, size, date of last update, formated filesize */
  197.  
  198. function get_product_version($file_name) {
  199.   $key = "P\x00r\x00o\x00d\x00u\x00c\x00t\x00V\x00e\x00r\x00s\x00i\x00o\x00n\x00\x00\x00";
  200.   $fptr = fopen($file_name, "rb");
  201.   $version = "";
  202.   if($fptr !== false) {
  203.     $data = "";
  204.     while (!feof($fptr)) {
  205.       $data .= fread($fptr, 65536);
  206.       if (strpos($data, $key)!==FALSE)
  207.          break;
  208.       $data = substr($data, strlen($data)-strlen($key));
  209.     }
  210.     fclose($fptr);
  211.     if (strpos($data, $key)===FALSE)
  212.       return "";
  213.     $pos = strpos($data, $key)+strlen($key);
  214.  
  215.     for ($i=$pos; $data[$i]!="\x00"; $i+=2)
  216.       $version .= $data[$i];
  217.   }
  218.   return $version;
  219. }
  220.  
  221. function remote_filename($url) {
  222.   $ch = curl_init($url);
  223.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  224.   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //will stop cURL from verifying the peer's certificate
  225.   curl_setopt($ch, CURLOPT_HEADER, TRUE);
  226.   curl_setopt($ch, CURLOPT_NOBODY, TRUE);
  227.   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  228.   $data = curl_exec($ch);
  229.   $fname = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  230.   curl_close($ch);
  231.   return $fname;
  232. }
  233.  
  234. function remote_file_size($url) {
  235.   $ch = curl_init($url);
  236.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  237.   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //will stop cURL from verifying the peer's certificate
  238.   curl_setopt($ch, CURLOPT_HEADER, TRUE);
  239.   curl_setopt($ch, CURLOPT_NOBODY, TRUE);
  240.   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  241.   $data = curl_exec($ch);
  242.   $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
  243.   curl_close($ch);
  244.   return $size;
  245. }
  246.  
  247. function remote_file_size_formated($url) {
  248.   $size = remote_file_size($url);
  249.   if($size != "")
  250.     $size = number_format(trim($size), 0, ".", " ");
  251.   return $size;
  252. }
  253.  
  254. function remote_file_date($url) {
  255.   $curl = curl_init($url);
  256.   //don't fetch the actual page, you only want headers
  257.   curl_setopt($curl, CURLOPT_NOBODY, true);
  258.   //stop it from outputting stuff to stdout
  259.   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  260.   // attempt to retrieve the modification date
  261.   curl_setopt($curl, CURLOPT_FILETIME, true);
  262.   $result = curl_exec($curl);
  263.   if ($result === false) {
  264.     return ""; //die (curl_error($curl));
  265.   }
  266.   $timestamp = curl_getinfo($curl, CURLINFO_FILETIME);
  267.   if ($timestamp != -1) { //otherwise unknown
  268.     return date("Y-m-d H:i:s", $timestamp);
  269.   }
  270.   return "";
  271. }
  272.  
  273. function appendFileSize($f,$expectedSize = 0) {
  274.   $size = remote_file_size_formated($f);
  275.   $s = "";
  276.   if( isset($size) ) {
  277.     if($expectedSize != 0) {
  278.       $size2 = str_replace(' ', '', $size);
  279.       if($expectedSize <= $size2)
  280.         $s = " (".$size.")";
  281.     } else
  282.       $s = " (".$size.")";
  283.   }
  284.   return $s;
  285. }
  286.  
  287. /* String operations: trimm */
  288.  
  289. function trimAfter($s,$after) {
  290.   if( strlen($after) > 0 ) {
  291.     $p = strpos($s, $after);
  292.     if( $p !== false ) {
  293.       return trim( substr( $s, $p + strlen($after) ) );
  294.     }
  295.   }
  296.   return trim($s);
  297. }
  298.  
  299. function trimBefore($s,$before) {
  300.   if( strlen($before) > 0 ) {
  301.     $p = strpos($s, $before);
  302.     if( $p !== false ) {
  303.       return trim( substr( $s, 0, $p ) );
  304.     }
  305.   }
  306.   return trim($s);
  307. }
  308.  
  309. function trimm($s,$after,$before) { return trimBefore( trimAfter($s,$after) ,$before ); }
  310.  
  311. function select($pred, $s, $za) { return trimm($s,$pred,$za); }
  312.  
  313. // JSON
  314.  
  315. function prettyPrint( $json ) {
  316.   $result = '';
  317.   $level = 0;
  318.   $in_quotes = false;
  319.   $in_escape = false;
  320.   $ends_line_level = NULL;
  321.   $json_length = strlen( $json );
  322.  
  323.   for( $i = 0; $i < $json_length; $i++ ) {
  324.     $char = $json[$i];
  325.     $new_line_level = NULL;
  326.     $post = "";
  327.     if( $ends_line_level !== NULL ) {
  328.         $new_line_level = $ends_line_level;
  329.         $ends_line_level = NULL;
  330.     }
  331.     if ( $in_escape ) {
  332.         $in_escape = false;
  333.     } else if( $char === '"' ) {
  334.         $in_quotes = !$in_quotes;
  335.     } else if( ! $in_quotes ) {
  336.       switch( $char ) {
  337.       case '}': case ']':
  338.           $level--;
  339.           $ends_line_level = NULL;
  340.           $new_line_level = $level;
  341.           break;
  342.  
  343.       case '{': case '[':
  344.           $level++;
  345.       case ',':
  346.           $ends_line_level = $level;
  347.           break;
  348.  
  349.       case ':':
  350.           $post = " ";
  351.           break;
  352.  
  353.       case " ": case "\t": case "\n": case "\r":
  354.           $char = "";
  355.           $ends_line_level = $new_line_level;
  356.           $new_line_level = NULL;
  357.           break;
  358.       }
  359.     } else if ( $char === '\\' ) {
  360.         $in_escape = true;
  361.     }
  362.     if( $new_line_level !== NULL ) {
  363.         $result .= "\n".str_repeat( "  ", $new_line_level );
  364.     }
  365.     $result .= $char.$post;
  366.   }
  367.  
  368.   return $result;
  369. }
  370.  
  371. function GetJSON($url) {
  372.   $data = gHtml($url);
  373.   return prettyPrint( $data );
  374. }
  375.  
  376. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement