note 71313 added to function.curl-setopt
| From: | eion at bigfoot dot com | Date: | Wed, 22 Nov 2006 04:40:43 +0000 |
| Subject: | note 71313 added to function.curl-setopt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-119969@lists.php.net to get a copy of this message | ||
If you are trying to use CURLOPT_FOLLOWLOCATION and you get this warning:
Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in
safe_mode or an open_basedir is set...
then you will want to read http://www.php.net/ChangeLog-4.php which says
"Disabled CURLOPT_FOLLOWLOCATION in curl when open_basedir or safe_mode are enabled." as
of PHP 4.4.4/5.1.5. This is due to the fact that curl is not part of PHP and doesn't know the
values of open_basedir or safe_mode, so you could comprimise your webserver operating in safe_mode
by redirecting (using header('Location: ...')) to "file://" urls, which curl
would have gladly retrieved.
Until the curl extension is changed in PHP or curl (if it ever will) to deal with
"Location:" headers, here is a far from perfect remake of the curl_exec function that I am
using.
Since there's no curl_getopt function equivalent, you'll have to tweak the function to
make it work for your specific use. As it is here, it returns the body of the response and not the
header. It also doesn't deal with redirection urls with username and passwords in them.
<?php
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\n", $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);
debug('Redirecting to', $new_url);
return curl_redir_exec($ch);
} else {
$curl_loops=0;
return $data;
}
}
?>
----
Server IP: 202.89.36.242
Probable Submitter: 203.97.10.134
----
Manual Page -- http://www.php.net/manual/en/function.curl-setopt.php
Edit -- https://master.php.net/note/edit/71313
Del: integrated -- https://master.php.net/note/delete/71313/integrated
Del: useless -- https://master.php.net/note/delete/71313/useless
Del: bad code -- https://master.php.net/note/delete/71313/bad+code
Del: spam -- https://master.php.net/note/delete/71313/spam
Del: non-english -- https://master.php.net/note/delete/71313/non-english
Del: in docs -- https://master.php.net/note/delete/71313/in+docs
Del: other reasons-- https://master.php.net/note/delete/71313
Reject -- https://master.php.net/note/reject/71313
Search -- https://master.php.net/manage/user-notes.php