Advertisement
retesere20

culr-php-

Feb 27th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. //i.e. set_cookies_from_url("http://example.com/?username=user&auth=key');
  2. public function set_cookies_from_url($url)
  3. {
  4. $d=$this->get_remote_data($url, false, ["curl_opts"=>["CURLOPT_HEADERFUNCTION"=>
  5. ( function ($ch, $headerLine) {
  6. if (preg_match('/^Set-Cookie:\s*([^;]*)/mi', $headerLine, $cookieArr) == 1)
  7. {
  8. $cookie = $cookieArr[1];
  9. $cookie_vars = explode('=', $cookie, 2);
  10. $this->example_cookies[$cookie_vars[0]] = $cookie_vars[1];
  11. }
  12. return strlen($headerLine); // Needed by curl
  13. }
  14. )
  15. ]]
  16. );
  17. foreach($this->example_cookies as $key=>$name)
  18. {
  19. $this->set_cookie($key,$name, 86000, '/target_dir/');
  20. }
  21. $this->set_cookie("sample_confirm","1");
  22. }
  23.  
  24. /*
  25. usage:
  26. list($curl_responses, $curl_errors) = $helpers->multi_curl($urls);
  27.  
  28. foreach($urls as $key => $url)
  29. {
  30. if (!$curl_responses[$key]) $failed_urls[$key]=$urls[$key];
  31. else { $data=parse_response_into_candles( $curl_responses[$key], $key ); if($data) calcs($data, $key, $url); }
  32. }
  33.  
  34. if ( ! empty( $failed_urls ) )
  35. {
  36. //make re-triger
  37. if (!$is_retrigger) {
  38. echoEnabled('<h2 style="color:red;">Re-triggering requests:</h2> ['. datet() .']');
  39. errorlog("Re-triggering cURL for these urls: \r\n". print_r($failed_urls, true) . "\r\n\r\n");
  40. this_function($failed_urls, $is_retrigger=true);
  41. }
  42. //result if re-trigger done already
  43. else errorlog("After retrigger, still failed urls:\r\n" . print_r($failed_urls, true) );
  44. }
  45. */
  46. public function multi_curl($urls)
  47. {
  48. $curl_responses =[];
  49. $curl_errors =[];
  50. $mch = curl_multi_init();
  51. $handlesArray=[];
  52. $curl_max_timeout = 60*60; //max 1 hr to run
  53. foreach ($urls as $key=> $url) {
  54. $ch = curl_init();
  55. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  56. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  57. curl_setopt($ch, CURLOPT_HEADER, false);
  58. // timeouts: https://thisinterestsme.com/php-setting-curl-timeout/ and https://stackoverflow.com/a/15982505/2377343
  59. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $curl_max_timeout);
  60. curl_setopt($ch, CURLOPT_TIMEOUT, $curl_max_timeout);
  61. if (defined('CURLOPT_TCP_FASTOPEN')) curl_setopt($ch, CURLOPT_TCP_FASTOPEN, 1);
  62. curl_setopt($ch, CURLOPT_ENCODING, ""); // empty to autodetect | gzip,deflate
  63. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  64. curl_setopt($ch, CURLOPT_URL, $url);
  65. $handlesArray[$key] = $ch;
  66. curl_multi_add_handle($mch, $handlesArray[$key]);
  67. }
  68.  
  69. // other approaches are deprecated ! https://stackoverflow.com/questions/58971677/
  70. do {
  71. $execReturnValue = curl_multi_exec($mch, $runningHandles);
  72. } while ($runningHandles>0);
  73.  
  74. //exec now
  75. foreach($urls as $key => $url)
  76. {
  77. $handle = $handlesArray[$key];
  78. // Check for errors
  79. $curlError = curl_error($handle);
  80. if($curlError != "") {
  81. $curl_responses[$key]=false;
  82. $curl_errors[$key] = $curlError;
  83. }
  84. else{
  85. $curl_responses[$key] = curl_multi_getcontent($handle);
  86. }
  87. curl_multi_remove_handle($mch, $handle); curl_close($handle);
  88. }
  89. curl_multi_close($mch);
  90. return [$curl_responses, $curl_errors];
  91. }
  92.  
  93. // for fast requests on same server
  94. public function curlFast($url, $params){
  95. if (!property_exists($this,'curlFastInited'))
  96. {
  97. $c = curl_init();
  98. curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  99. curl_setopt($c, CURLOPT_HEADER, false);
  100. curl_setopt($c, CURLOPT_TIMEOUT, 9);
  101. curl_setopt($c, CURLOPT_HTTPGET, true);
  102. curl_setopt($c, CURLOPT_SSL_VERIFYHOST,false);
  103. curl_setopt($c, CURLOPT_SSL_VERIFYPEER,false);
  104. curl_setopt($c, CURLOPT_MAXREDIRS, 1);
  105. curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 9);
  106. curl_setopt($c, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
  107. curl_setopt($c, CURLOPT_ENCODING, '');
  108. if (defined('CURLOPT_TCP_FASTOPEN')) curl_setopt($c, CURLOPT_TCP_FASTOPEN, 1);
  109. $this->curlFastInited =$c;
  110. register_shutdown_function( function(){ curl_close($this->curlFastInited); } );
  111. }
  112. curl_setopt($this->curlFastInited, CURLOPT_URL, $url);
  113. $data = curl_exec($this->curlFastInited);
  114. return $data;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement