note 113682 added to function.curl-setopt

From: Date: Fri, 15 Nov 2013 10:36:52 +0000
Subject: note 113682 added to function.curl-setopt
Groups: php.notes 
Request: Send a blank email to php-notes+get-197527@lists.php.net to get a copy of this message
Many hosters use PHP safe_mode or/and open_basedir, so you can't use CURLOPT_FOLLOWLOCATION. If you try, you see message like this: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in [you script name & path] on line XXX First, I try to use zsalab function (http://us2.php.net/manual/en/function.curl-setopt.php#102121) from this page, but for some reason it did not work properly. So, I wrote my own. It can be use instead of curl_exec. If server HTTP response codes is 30x, function will forward the request as long as the response is not different from 30x (for example, 200 Ok). Also you can use POST. function curlExec(/* Array */$curlOptions='', /* Array */$curlHeaders='', /* Array */$postFields='') { $newUrl = ''; $maxRedirection = 10; do { if ($maxRedirection<1) die('Error: reached the limit of redirections'); $ch = curl_init(); if (!empty($curlOptions)) curl_setopt_array($ch, $curlOptions); if (!empty($curlHeaders)) curl_setopt($ch, CURLOPT_HTTPHEADER, $curlHeaders); if (!empty($postFields)) { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); } if (!empty($newUrl)) curl_setopt($ch, CURLOPT_URL, $newUrl); // redirect needed $curlResult = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($code == 301 || $code == 302 || $code == 303 || $code == 307) { preg_match('/Location:(.*?)\n/', $curlResult, $matches); $newUrl = trim(array_pop($matches)); curl_close($ch); $maxRedirection--; continue; } else // no more redirection { $code = 0; curl_close($ch); } } while($code); return $curlResult; } ---- Server IP: 192.41.45.150 Probable Submitter: 92.113.16.151 ---- Manual Page -- http://php.net/manual/en/function.curl-setopt.php Edit -- https://master.php.net/note/edit/113682 Del: integrated -- https://master.php.net/note/delete/113682/integrated Del: useless -- https://master.php.net/note/delete/113682/useless Del: bad code -- https://master.php.net/note/delete/113682/bad+code Del: spam -- https://master.php.net/note/delete/113682/spam Del: non-english -- https://master.php.net/note/delete/113682/non-english Del: in docs -- https://master.php.net/note/delete/113682/in+docs Del: other reasons-- https://master.php.net/note/delete/113682 Reject -- https://master.php.net/note/reject/113682 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#197527) next »