Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //i.e. set_cookies_from_url("http://example.com/?username=user&auth=key');
- public function set_cookies_from_url($url)
- {
- $d=$this->get_remote_data($url, false, ["curl_opts"=>["CURLOPT_HEADERFUNCTION"=>
- ( function ($ch, $headerLine) {
- if (preg_match('/^Set-Cookie:\s*([^;]*)/mi', $headerLine, $cookieArr) == 1)
- {
- $cookie = $cookieArr[1];
- $cookie_vars = explode('=', $cookie, 2);
- $this->example_cookies[$cookie_vars[0]] = $cookie_vars[1];
- }
- return strlen($headerLine); // Needed by curl
- }
- )
- ]]
- );
- foreach($this->example_cookies as $key=>$name)
- {
- $this->set_cookie($key,$name, 86000, '/target_dir/');
- }
- $this->set_cookie("sample_confirm","1");
- }
- /*
- usage:
- list($curl_responses, $curl_errors) = $helpers->multi_curl($urls);
- foreach($urls as $key => $url)
- {
- if (!$curl_responses[$key]) $failed_urls[$key]=$urls[$key];
- else { $data=parse_response_into_candles( $curl_responses[$key], $key ); if($data) calcs($data, $key, $url); }
- }
- if ( ! empty( $failed_urls ) )
- {
- //make re-triger
- if (!$is_retrigger) {
- echoEnabled('<h2 style="color:red;">Re-triggering requests:</h2> ['. datet() .']');
- errorlog("Re-triggering cURL for these urls: \r\n". print_r($failed_urls, true) . "\r\n\r\n");
- this_function($failed_urls, $is_retrigger=true);
- }
- //result if re-trigger done already
- else errorlog("After retrigger, still failed urls:\r\n" . print_r($failed_urls, true) );
- }
- */
- public function multi_curl($urls)
- {
- $curl_responses =[];
- $curl_errors =[];
- $mch = curl_multi_init();
- $handlesArray=[];
- $curl_max_timeout = 60*60; //max 1 hr to run
- foreach ($urls as $key=> $url) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_HEADER, false);
- // timeouts: https://thisinterestsme.com/php-setting-curl-timeout/ and https://stackoverflow.com/a/15982505/2377343
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $curl_max_timeout);
- curl_setopt($ch, CURLOPT_TIMEOUT, $curl_max_timeout);
- if (defined('CURLOPT_TCP_FASTOPEN')) curl_setopt($ch, CURLOPT_TCP_FASTOPEN, 1);
- curl_setopt($ch, CURLOPT_ENCODING, ""); // empty to autodetect | gzip,deflate
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_URL, $url);
- $handlesArray[$key] = $ch;
- curl_multi_add_handle($mch, $handlesArray[$key]);
- }
- // other approaches are deprecated ! https://stackoverflow.com/questions/58971677/
- do {
- $execReturnValue = curl_multi_exec($mch, $runningHandles);
- } while ($runningHandles>0);
- //exec now
- foreach($urls as $key => $url)
- {
- $handle = $handlesArray[$key];
- // Check for errors
- $curlError = curl_error($handle);
- if($curlError != "") {
- $curl_responses[$key]=false;
- $curl_errors[$key] = $curlError;
- }
- else{
- $curl_responses[$key] = curl_multi_getcontent($handle);
- }
- curl_multi_remove_handle($mch, $handle); curl_close($handle);
- }
- curl_multi_close($mch);
- return [$curl_responses, $curl_errors];
- }
- // for fast requests on same server
- public function curlFast($url, $params){
- if (!property_exists($this,'curlFastInited'))
- {
- $c = curl_init();
- curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($c, CURLOPT_HEADER, false);
- curl_setopt($c, CURLOPT_TIMEOUT, 9);
- curl_setopt($c, CURLOPT_HTTPGET, true);
- curl_setopt($c, CURLOPT_SSL_VERIFYHOST,false);
- curl_setopt($c, CURLOPT_SSL_VERIFYPEER,false);
- curl_setopt($c, CURLOPT_MAXREDIRS, 1);
- curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 9);
- curl_setopt($c, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
- curl_setopt($c, CURLOPT_ENCODING, '');
- if (defined('CURLOPT_TCP_FASTOPEN')) curl_setopt($c, CURLOPT_TCP_FASTOPEN, 1);
- $this->curlFastInited =$c;
- register_shutdown_function( function(){ curl_close($this->curlFastInited); } );
- }
- curl_setopt($this->curlFastInited, CURLOPT_URL, $url);
- $data = curl_exec($this->curlFastInited);
- return $data;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement