note 93163 added to ref.curl
| From: | Emprivo dot com at osu1 dot php dot net | Date: | Wed, 26 Aug 2009 16:58:28 +0000 |
| Subject: | note 93163 added to ref.curl | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159902@lists.php.net to get a copy of this message | ||
Fixed bugs in the function posted earlier (better javascript redirect following and now supports
HTTPS)
<?php
/*==================================
Get url content and response headers (given a url, follows all redirections on it and returned
content and response headers of final url)
@return array[0] content
array[1] array of response headers
==================================*/
function get_url( $url, $javascript_loop = 0, $timeout = 5 )
{
$url = str_replace( "&", "&", urldecode(trim($url)) );
$cookie = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3)
Gecko/20041001 Firefox/0.10.1" );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); # required for https urls
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
$content = curl_exec( $ch );
$response = curl_getinfo( $ch );
curl_close ( $ch );
if ($response['http_code'] == 301 || $response['http_code'] == 302)
{
ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3)
Gecko/20041001 Firefox/0.10.1");
if ( $headers = get_headers($response['url']) )
{
foreach( $headers as $value )
{
if ( substr( strtolower($value), 0, 9 ) == "location:" )
return get_url( trim( substr( $value, 9, strlen($value) ) ) );
}
}
}
if ( ( preg_match("/>[[:space:]]+window\.location\.replace\('(.*)'\)/i",
$content, $value) || preg_match("/>[[:space:]]+window\.location\=\"(.*)\"/i",
$content, $value) ) &&
$javascript_loop < 5
)
{
return get_url( $value[1], $javascript_loop+1 );
}
else
{
return array( $content, $response );
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 146.95.31.131
----
Manual Page -- http://www.php.net/manual/en/ref.curl.php
Edit -- https://master.php.net/note/edit/93163
Del: integrated -- https://master.php.net/note/delete/93163/integrated
Del: useless -- https://master.php.net/note/delete/93163/useless
Del: bad code -- https://master.php.net/note/delete/93163/bad+code
Del: spam -- https://master.php.net/note/delete/93163/spam
Del: non-english -- https://master.php.net/note/delete/93163/non-english
Del: in docs -- https://master.php.net/note/delete/93163/in+docs
Del: other reasons-- https://master.php.net/note/delete/93163
Reject -- https://master.php.net/note/reject/93163
Search -- https://master.php.net/manage/user-notes.php