Advertisement
bloginfo

Classe PHP cURL / Scrapping in GPL license

Mar 16th, 2012 (edited)
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.33 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Author : Denis Szalkowski Copyright © 2012
  4.  * Licence : GNU General Public Licence v3.0
  5.  * https://www.php.net/manual/fr/book.curl.php
  6.  * https://php.net/manual/fr/function.curl-setopt.php
  7. */
  8. class Curl
  9. {
  10.     protected $ch;
  11.     function __construct($url,$tor=false)
  12.     {
  13.        
  14.         $this->ch = curl_init();
  15.         $headers= [
  16.             'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
  17.             'Accept-Language: en-US;q=0.7,en;q=0.3'
  18.         ];
  19.         $options=[
  20.             CURLOPT_HEADEROPT => CURLHEADER_UNIFIED,
  21.             CURLOPT_HTTPHEADER => $headers,
  22.             CURLOPT_HEADER => false,
  23.             CURLOPT_URL => $url,
  24.             CURLOPT_ENCODING => 'gzip,deflate',
  25.             CURLOPT_AUTOREFERER => true,
  26.             CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0',
  27.             CURLOPT_RETURNTRANSFER => true,
  28.             CURLOPT_COOKIESESSION => true,
  29.             CURLOPT_FOLLOWLOCATION => true,
  30.             CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,
  31.             CURLOPT_SSL_VERIFYPEER => false,
  32.             CURLOPT_PROXY_SSL_VERIFYPEER => false,
  33.             CURLOPT_SSL_VERIFYSTATUS => false,
  34.             CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2TLS,
  35.             CURLOPT_CONNECTTIMEOUT => 15,
  36.             CURLOPT_TIMEOUT => 15,
  37.             CURLOPT_HTTPGET => true
  38. ,            CURLOPT_HTTP_CONTENT_DECODING => true,
  39.             CURLOPT_TCP_FASTOPEN => true,
  40.             CURLOPT_VERBOSE => false
  41.         ];
  42.         curl_setopt_array($this->ch,$options);
  43.         /*
  44.          * Tor enabled if $tor is true.
  45.          */
  46.         if($tor)
  47.         {
  48.             curl_setopt($this->ch,CURLOPT_PROXY,'127.0.0.1:9050');
  49.             curl_setopt($this->ch,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5);
  50.         }
  51.     }
  52.     /*
  53.      * Get url content
  54.      */
  55.     public function get_content()
  56.     {
  57.         $response=false;
  58.         $response=curl_exec($this->ch);
  59.         $httpcode = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
  60.         if($httpcode >= 400 and $httpcode < 200)
  61.         {
  62.             $response=$httpcode;
  63.         }
  64.         return $response;
  65.     }
  66.     /*
  67.      * Destructor invoked by unset
  68.      */
  69.     function __destruct()
  70.     {
  71.        curl_close($this->ch);
  72.     }
  73. }
  74. ?>
  75.  
Tags: php tor scrapping
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement