Skip to content

Instantly share code, notes, and snippets.

@demonio
Last active January 28, 2016 21:13
Show Gist options
  • Save demonio/43d6ca376077ff4b0c50 to your computer and use it in GitHub Desktop.
Save demonio/43d6ca376077ff4b0c50 to your computer and use it in GitHub Desktop.
Compilación de funciones CURL para toma de datos e imagenes remotas.
<?php # CREADA EN EL 2009
class Curl
{
static function file($url, $dest)
{
$i = curl_init();
curl_setopt($i, CURLOPT_URL, $url);
$referer = Str::cut($url, array('beg' => 'http://', 'end' => '/'), 1);
curl_setopt($i, CURLOPT_REFERER, $referer);
curl_setopt($i, CURLOPT_HEADER, 0);
$browser = 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)';
curl_setopt($i, CURLOPT_USERAGENT, $browser);
$f = fopen($dest, 'w');
curl_setopt($i, CURLOPT_FILE, $f);
curl_exec($i);
fclose($f);
curl_close($i);
}
static function str($url)
{
$i = curl_init(); # initialize curl handle
curl_setopt($i, CURLOPT_URL, $url); # set url to post to
curl_setopt($i, CURLOPT_FAILONERROR, 1); # Fail on errors
#curl_setopt($i, CURLOPT_FOLLOWLOCATION, 1); # allow redirects
curl_setopt($i, CURLOPT_RETURNTRANSFER, 1); # return into a variable
#curl_setopt($i, CURLOPT_PORT, 80); # Set the port number
#curl_setopt($i, CURLOPT_TIMEOUT, 15); # times out after 15s
$browser = 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)';
curl_setopt($i, CURLOPT_USERAGENT, $browser);
$code = curl_exec($i);
curl_close($i);
return $code;
}
static function get($url, $pat, $dest)
{
$code = Curl::str($url);
$part = Str::cut($code, $pat);
$a = explode('<img ', $part);
foreach ($a as $v)
{
$http = strstr('http://', $v);
$src = Str::cut($v, array('beg' => 'src="', 'end' => '"') );
$domain = Str::cut($url, array('beg' => 'http://', 'end' => '/'), 1);
$name = banename($src);
if ( ! $http) $src = $domain . $src;
Curl::file($src, $dest . $name);
}
}
}
<?php # CREADA EN EL 2008
function xRss($url)
{
$xml = utf8_encode(file_get_contents($url));
$item = explode('<item', $xml);
for($a = 0; $a < count($item); ++$a)
{
$one = xCutStr($item[$a], '<title>', '</title>');
$obj[$a]->title = xCutStr($one, ') ', 0);
$obj[$a]->date_ = xCutStr($one, '(', ')');
$obj[$a]->link_ = xCutStr($item[$a], '<link>', '</link>');
$obj[$a]->description = xCutStr($item[$a], '<description>', '</description>');
}
return $obj;
}
function xCutStr($str, $beg = 0, $end = 0)
{
if($beg)
{
$a = explode($beg, $str, 2);
$str = $a[1];
}
if($end)
{
$b = explode($end, $str, 2);
$str = $b[0];
}
return trim($str);
}
function xSave($origen, $destino)
{
$fopen = fopen($destino, 'w');
fwrite($fopen, $origen);
fclose($fopen);
}
function xCurlWeb($url, $destino = 0)
{
$user_agent = 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)';
$ch = curl_init(); # initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url); # set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1); # Fail on errors
#curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); # allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); # return into a variable
#curl_setopt($ch, CURLOPT_PORT, 80); # Set the port number
#curl_setopt($ch, CURLOPT_TIMEOUT, 15); # times out after 15s
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
$code = curl_exec($ch);
curl_close($ch);
/*echo '<hr />'.$destino;
echo '<hr />'.
xSave($code, $destino);*/
$code = str_replace('<', '&lt;', $code);
return $code;
}
function xCurlImg($img, $url, $referer)
{
$user_agent = 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)';
$init = curl_init();
curl_setopt($init, CURLOPT_URL, $url.$img);
curl_setopt($init, CURLOPT_REFERER, $referer);
curl_setopt($init, CURLOPT_HEADER, 0);
curl_setopt($init, CURLOPT_USERAGENT, $user_agent);
$fopen = fopen('img/'.$img, 'w');
curl_setopt($init, CURLOPT_FILE, $fopen);
curl_exec($init);
fclose($fopen);
curl_close($init);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment