note 95027 added to function.curl-setopt
| From: | PawelAntczak at osu1 dot php dot net | Date: | Wed, 09 Dec 2009 20:07:51 +0000 |
| Subject: | note 95027 added to function.curl-setopt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-163751@lists.php.net to get a copy of this message | ||
Hello.
During problems with "CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an
open_basedir is set"
I was looking for solution.
I've found few methods on this page, but none of them was good enough, so I made one.
<?php
function curl_redirect_exec($ch, &$redirects, $curlopt_header = false) {
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 301 || $http_code == 302) {
list($header) = explode("\r\n\r\n", $data, 2);
$matches = array();
preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
$url = trim(array_pop($matches));
$url_parsed = parse_url($url);
if (isset($url_parsed)) {
curl_setopt($ch, CURLOPT_URL, $url);
$redirects++;
return curl_redirect_exec($ch, $redirects);
}
}
if ($curlopt_header)
return $data;
else {
list(,$body) = explode("\r\n\r\n", $data, 2);
return $body;
}
}
?>
Main issue in existing functions was lack of information, how many redirects was done.
This one will count it.
First parameter as usual.
Second should be already initialized integer, it will be incremented by number of done redirects.
You can set CURLOPT_HEADER if You need it.
----
Server IP: 69.147.83.197
Probable Submitter: 95.160.164.237
----
Manual Page -- http://www.php.net/manual/en/function.curl-setopt.php
Edit -- https://master.php.net/note/edit/95027
Del: integrated -- https://master.php.net/note/delete/95027/integrated
Del: useless -- https://master.php.net/note/delete/95027/useless
Del: bad code -- https://master.php.net/note/delete/95027/bad+code
Del: spam -- https://master.php.net/note/delete/95027/spam
Del: non-english -- https://master.php.net/note/delete/95027/non-english
Del: in docs -- https://master.php.net/note/delete/95027/in+docs
Del: other reasons-- https://master.php.net/note/delete/95027
Reject -- https://master.php.net/note/reject/95027
Search -- https://master.php.net/manage/user-notes.php