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

Magento Most Popular Extensions
Google Chrome for Linux Beta
Firefox Address Bar Lag + Solution
Custom Buttons for Firefox

Most Popular Posts

Magento Developer UK Freelance osCommerce UK Magento Training CRE Loaded UK

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

Possibly Relevant Posts:

  • no matches

Feed | Respond | Trackback

8 Responses to “PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode”

  1. sakao Says:
    January 18th, 2009 at 11:08 am

    this is not a working code
    as far as i can see
    there's no
    $go = curl_init($url);
    in the first line

  2. admin Says:
    January 19th, 2009 at 9:35 am

    oops sorry about that- i have add this in there.

  3. Karl Says:
    February 5th, 2009 at 11:08 pm

    Very nice, works like a treat. Am using it with domdocument and xpath to scrape content from various sources.

    Quick tip: obvious, but for php novices, you'll need to add something like the following to your code to actually start the process

    $html=curl('http://www.urltobeparsed.com');

    See here for more info: http://www.merchantos.com/makebeta/php/scraping-links-with-php/

  4. Ali Says:
    February 25th, 2009 at 11:15 am

    Hello,

    I am getting the error

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

    What can be the possible solution. I didnt understand the above script you have written

  5. nd Says:
    May 7th, 2009 at 9:54 am

    Nice code!

    However, I had a problem with it since
    explode("\n\n", $data, 2);

    does not work on all platforms. Correct is
    explode("\n\r", $data, 2);

    Regards,
    Andy

  6. admin Says:
    May 7th, 2009 at 10:05 am

    nice one Andy.

    I wonder if we could mod the function to work on any platform..

    would swapping \n\n for PHP_EOL work?

  7. steve Says:
    July 29th, 2009 at 10:15 am

    nd - you're right about the \n\r this worked for me.

    also, error in line 6, should be:

    if (ini_get('open_basedir') == '' && ini_get('safe_mode') == 'Off'){

    in my case I also had to change the safe mode check because safe mode off is an empty string:

    if (ini_get('open_basedir') == '' && ini_get('safe_mode' == '')){

  8. Andy Says:
    September 24th, 2009 at 6:18 pm

    Wonderful, saved my life :) I used it on an connector which i wrote for plugging Wordpress and CakePHP together. In this constellation, there was no $url["query"] and i got an error message. So this is my solution for that:

    $new_url = $url['scheme'].'://'.$url['host'].$url['path'].(array_key_exists('query', $url) && $url['query'] ? '?'.$url['query'] : '');

    Maybe it´s usefull for someone.

    Nice greetings and a big thank you for this workaround.

    Andy

Leave a Reply

  • RSS Feed
  • Categories

    • adwords
    • apache
    • barcode
    • business
    • creloaded
    • css
    • curl
    • customer services
    • debugging
    • drupal
    • eclipse
    • ecommerce
    • edmondscommerce
    • email
    • excel
    • firefox
    • flash
    • gd
    • git
    • graphs
    • hosting
    • icecat
    • internet news
    • javascript
    • jquery
    • link building
    • linux
    • mac
    • magento
    • management
    • misc
    • mod_rewrite
    • mysql
    • netbeans
    • open suse
    • oscommerce
    • php
    • plesk
    • portfolio
    • product catalogue
    • product feed
    • programming
    • regular expressions
    • ria
    • scraping
    • search engine optimisation
    • security
    • seo
    • spidering
    • symfony
    • twitter
    • ubuntu
    • Uncategorized
    • usability
    • vps
    • web design
    • web development
    • Windows
    • xampp
    • zend framework
    • zip
  • Archives

    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
  • Tags

    bulk course cre loaded creloaded css custom developer development directories drupal error find firefox git google hosts file html jaunty javascript leeds links linux magento mysql netbeans oscommerce performance php plesk ppc problem replace search engine optimisation seo server symfony table training ubuntu uk virtualbox web web design xml zend framework
  • Random Posts

    • Symfony Developer UK
    • PHP Convert UNICODE to UTF-8
    • Virtual Box is Good - Virtual Box using Guest Additions is Great!
    • Magento Can't Save Customer + Solution (Zend Framework)
    • Firefox Selective Cache Disable
    • Taming IE6 Definitive Guide
    • Magento Attributes and Attribute Sets - An Explanation
    • Magento Natively Supports Google Website Optimizer
    • Meta Title Tag and SEO
    • Apache, mod_rewrite and SEO
  • Recent Comments

    • admin on Magento Backup Error Filesystem.php on line 234 + Solution
    • admin on Magento Leeds
    • Matthew Dolley on Magento Leeds
    • kash on PHP Email Attachment Function
    • Hussein on PHP Save Images Using cURL
  • Category Specific RSS

    • adwords Feed for all posts filed under adwords
    • apache Feed for all posts filed under apache
    • barcode Feed for all posts filed under barcode
    • business Feed for all posts filed under business
    • creloaded Feed for all posts filed under creloaded
    • css Feed for all posts filed under css
    • curl Feed for all posts filed under curl
    • customer services Feed for all posts filed under customer services
    • debugging Feed for all posts filed under debugging
    • drupal Feed for all posts filed under drupal
    • eclipse Feed for all posts filed under eclipse
    • ecommerce Feed for all posts filed under ecommerce
    • edmondscommerce Feed for all posts filed under edmondscommerce
    • email Feed for all posts filed under email
    • excel Feed for all posts filed under excel
    • firefox Feed for all posts filed under firefox
    • flash Feed for all posts filed under flash
    • gd Feed for all posts filed under gd
    • git Feed for all posts filed under git
    • graphs Feed for all posts filed under graphs
    • hosting Feed for all posts filed under hosting
    • icecat Feed for all posts filed under icecat
    • internet news Feed for all posts filed under internet news
    • javascript Feed for all posts filed under javascript
    • jquery Feed for all posts filed under jquery
    • link building Feed for all posts filed under link building
    • linux Feed for all posts filed under linux
    • mac Feed for all posts filed under mac
    • magento Feed for all posts filed under magento
    • management Feed for all posts filed under management
    • misc Feed for all posts filed under misc
    • mod_rewrite Feed for all posts filed under mod_rewrite
    • mysql Feed for all posts filed under mysql
    • netbeans Feed for all posts filed under netbeans
    • open suse Feed for all posts filed under open suse
    • oscommerce Feed for all posts filed under oscommerce
    • php Feed for all posts filed under php
    • plesk Feed for all posts filed under plesk
    • portfolio Feed for all posts filed under portfolio
    • product catalogue Feed for all posts filed under product catalogue
    • product feed Feed for all posts filed under product feed
    • programming Feed for all posts filed under programming
    • regular expressions Feed for all posts filed under regular expressions
    • ria Feed for all posts filed under ria
    • scraping Feed for all posts filed under scraping
    • search engine optimisation Feed for all posts filed under search engine optimisation
    • security Feed for all posts filed under security
    • seo Feed for all posts filed under seo
    • spidering Feed for all posts filed under spidering
    • symfony Feed for all posts filed under symfony
    • twitter Feed for all posts filed under twitter
    • ubuntu Feed for all posts filed under ubuntu
    • Uncategorized Feed for all posts filed under Uncategorized
    • usability Feed for all posts filed under usability
    • vps Feed for all posts filed under vps
    • web design Feed for all posts filed under web design
    • web development Feed for all posts filed under web development
    • Windows Feed for all posts filed under Windows
    • xampp Feed for all posts filed under xampp
    • zend framework Feed for all posts filed under zend framework
    • zip Feed for all posts filed under zip

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

Freelance PHP Web Design UK Commercial Web Design