Edmonds Commerce Logo
  • home
    • blog
  • ecommerce
    • product catalogue
    • order processing
    • customer services
    • stock control
    • human resources
    • management information
  • development
    • oscommerce
    • php
    • mysql
    • open source
    • performance tuning
  • design
  • marketing
  • contact us
    • pricing

Edmonds Commerce Blog

Freelance PHP Ecommerce and SEO Developer in the UK

Latest Posts

CRELoaded Remove Google Ads -
ICECat Integration with osCommerce, Magento etc
Magento UK
PHP Cached Download Function

Most Popular Posts

PHP Email Attachment Function Freelance osCommerce UK Ultimate osCommerce Checkout - Fast and Friendly PHP : Dead Easy Excel Export

PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode

July 18th, 2008
Read More curl

If you are trying to get a curl script which needs follow on location functionality to run on a server which has either open_basedir or safe mode enabled you will get an error message similar to the following:

CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set

After a bit of digging, some kind soul has put a workaround here

Here is how to use the function

PLAIN TEXT
PHP:
  1. function curl($url){
  2. curl_setopt ($go, CURLOPT_URL, $url);
  3. //follow on location problems
  4.  
  5. if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')){
  6.  
  7. curl_setopt ($go, CURLOPT_FOLLOWLOCATION, $l);
  8.  
  9. $syn = curl_exec($go);
  10.  
  11. }else{
  12.  
  13. $syn = curl_redir_exec($go);
  14.  
  15. }
  16.  
  17. curl_close($go);
  18. return $syn;
  19. }
  20.  
  21. //follow on location problems workaround
  22.  
  23. function curl_redir_exec($ch)
  24.  
  25. {
  26.  
  27. static $curl_loops = 0;
  28.  
  29. static $curl_max_loops = 20;
  30.  
  31. if ($curl_loops++>= $curl_max_loops)
  32.  
  33. {
  34.  
  35. $curl_loops = 0;
  36.  
  37. return FALSE;
  38.  
  39. }
  40.  
  41. curl_setopt($ch, CURLOPT_HEADER, true);
  42.  
  43. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  44.  
  45. $data = curl_exec($ch);
  46.  
  47. list($header, $data) = explode("\n\n", $data, 2);
  48.  
  49. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  50.  
  51. if ($http_code == 301 || $http_code == 302)
  52.  
  53. {
  54.  
  55. $matches = array();
  56.  
  57. preg_match('/Location:(.*?)\n/', $header, $matches);
  58.  
  59. $url = @parse_url(trim(array_pop($matches)));
  60.  
  61. if (!$url)
  62.  
  63. {
  64.  
  65. //couldn't process the url to redirect to
  66.  
  67. $curl_loops = 0;
  68.  
  69. return $data;
  70.  
  71. }
  72.  
  73. $last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
  74.  
  75. if (!$url['scheme'])
  76.  
  77. $url['scheme'] = $last_url['scheme'];
  78.  
  79. if (!$url['host'])
  80.  
  81. $url['host'] = $last_url['host'];
  82.  
  83. if (!$url['path'])
  84.  
  85. $url['path'] = $last_url['path'];
  86.  
  87. $new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query']?'?'.$url['query']:'');
  88.  
  89. curl_setopt($ch, CURLOPT_URL, $new_url);
  90.  
  91. return curl_redir_exec($ch);
  92.  
  93. } else {
  94.  
  95. $curl_loops=0;
  96.  
  97. return $data;
  98.  
  99. }
  100.  
  101. }

Bookmark this Post
Add 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to Del.icio.usAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to diggAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to FURLAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to blinklistAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to redditAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to Feed Me LinksAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to TechnoratiAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to Yahoo My WebAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to NewsvineAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to SocializerAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to Ma.gnoliaAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to Stumble UponAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to Google BookmarksAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to RawSugarAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to SquidooAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to SpurlAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to BlinkBitsAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to NetvouzAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to RojoAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to BlogmarksAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to ShadowsAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to Co.mments
Add 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to ScuttleAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to BloglinesAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to TailrankAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to SegnaloAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to OKnotizieAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to NetscapeAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to Bookmark.itAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to AskAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to SmarkingAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to LinkagogoAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to DeliriousAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to SocialdustAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to Live-MSNAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to SlashDotAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to SphinnAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to DiggitaAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to SeotribuAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to FaceBookAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to UpnewsAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to WikioAdd 'PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode' to Social Bookmarking Reloaded

Feed | Respond | Trackback

Leave a Reply

  • RSS Feed
  • Categories

    • apache
    • barcode
    • creloaded
    • curl
    • customer services
    • debugging
    • ecommerce
    • email
    • excel
    • firefox
    • flash
    • gd
    • graphs
    • hosting
    • icecat
    • internet news
    • javascript
    • link building
    • linux
    • magento
    • management
    • mod_rewrite
    • mysql
    • oscommerce
    • php
    • plesk
    • product catalogue
    • product feed
    • programming
    • regular expressions
    • scraping
    • search engine optimisation
    • spidering
    • ubuntu
    • web design
    • web development
    • Windows
    • xampp
    • zip
  • Archives

    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
  • Tags

    addons advanced adverts blackhat blocking css curl development directories find firefox google hosts file html javascript keywords links msn mysql myths operators oscommerce paid links paid placement performance php ppc reciprocal linking replace screen scraping security seo serp speed spider spidering tuning user friendly vista web web design web developer
  • Random Posts

    • Demonoid is Back - The Return of Demonoid.com
    • Meta Title Tag and SEO
    • Advanced PHP Debug Function
    • PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode
    • Plesk Web Fusion PHP Limits
    • MySQL Add Column if Not Exists - PHP Function
    • HOSTS file, Vista and Blocking Unwanted Adverts
    • Saving a File or Webpage using PHP
    • PHP Save Images Using cURL
    • Purashop : SEO Services

Edmonds Commerce related questions? Send us a message or call us on 0844 357 0201.

Freelance PHP Web Design UK Commercial Web Design