Skip to content

Instantly share code, notes, and snippets.

@h2rd
Created January 6, 2012 23:57
Show Gist options
  • Save h2rd/1573079 to your computer and use it in GitHub Desktop.
Save h2rd/1573079 to your computer and use it in GitHub Desktop.
protected function doRequest($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'spider');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$html = curl_exec($ch);
$header = curl_getinfo($ch);
$charset = 'windows-1251';
if (preg_match('/charset=(.*)$/', $header['content_type'], $match)) {
$charset = strtolower($match[1]);
} else {
if (preg_match('/charset=(.*)\;/', $html, $match)) {
$charset = $match[1];
}
}
curl_close($ch);
if ($charset != 'utf-8') {
$html = iconv($charset, 'utf-8', $html);
}
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment