Home > Code > cURL with PHP and Apache on Windows

cURL with PHP and Apache on Windows

October 22nd, 2003 Tony Leave a comment Go to comments

Setting up cURL my linux server it was no problem at all, but I had a heck of a time getting cURL to work properly on my Windows test box with PHP and Apache. There are a lot of tricks scattered around on the web so here is my list of notes:

1. Only install PHP with the zip’d binaries. Don’t use the installer. I recommend deleting your current PHP installation and reinstalling with the binaries. Downloading the latest PHP has the added benefit of ensuring its compatible with the version of cURL you’ll download later. (I installed to D:\apps\php and will use that path for the rest of this example)

2. Edit your php.ini file:

   - set extensions_dir to d:\apps\php\extensions

   - set register_globals to On

   - set sessions.save_path to d:\apps\php\temp (you need to create that directory first)

3. Copy php4ts.dll (located in d:\apps\php\) to your Apache bin folder

4. Copy libeay32.dll and ssleay32.dll (located in d:\apps\php\dlls\) to c:\windows\system32

5. Download cURL for Windows at: http://curl.haxx.se/download.html. I chose the Win32 – Generic by J?Hartroth. I recommend getting the SSL version in case you ever need SSL. I unzipped cURL to d:\apps\curl and will use that path for the rest of this example

6. [SSL INSTALL ONLY] Download OpenSSL for Windows from http://curl.haxx.se/download.html. (Its near the bottom of the page). Extract libssl32.dll to d:\apps\curl

7. [Windows XP Install Only] Check to see if you have the following file: c:\windows\system32\msvcr70.dll. If not, search for it in Google and download it to system32. You may get error messages without it.

8. Uncomment the curl line in your php.ini file to enable curl: extension=php_curl.dll

9. Finally edit your Apache httpd.conf file to enable php:

   - Uncomment: LoadModule php4_module d:/apps/php/sapi/php4apache2.dll

   - Add Line: AddType application/x-httpd-php .php

Test with the following PHP code:

   $url = “http://www.thinkgeek.com”;

   $ch = curl_init();

   curl_setopt($ch, CURLOPT_URL,$url);

   curl_setopt($ch, CURLOPT_VERBOSE, 1);

   curl_setopt($ch, CURLOPT_POST, 0);

   curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

   $returned = curl_exec($ch);

   curl_close ($ch);

   echo $returned;

SSL NOTE: I kept getting no response when I tried using curl with SSL urls. I found that adding the following solved the problem:

   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

I have read that the proper solution is to use the ca-bundle.crt file for curl to be able to verify certificates but I haven’t tried this yet:

   curl_setopt($ch, CURLOPT_CAINFO, ‘drive:\pathto\ca-bundle.crt’);

Categories: Code Tags:
  1. Hitesh Patel
    June 5th, 2006 at 08:21 | #1

    Hi
    I am Hitesh Patel
    I have yahoo addressbook grabber but i face problem to execute script call to undefined function curl_init()

    plz send me reply how to execute my script.
    inform me all the neccessary file that i have to set.

  2. Jeff
    June 13th, 2006 at 13:52 | #2

    Worked like a charm! Thanks.

    And yes, running SSL with ca-bundle.crt works too.

  3. phphtm
    June 21st, 2006 at 06:34 | #3

    can you please tell me
    how can i encode the link with
    urlencode and php
    thanks

  4. Rajesh Adapa
    June 28th, 2006 at 12:15 | #4

    Really Worked Great…. Thank you very much. I should really appreciate your contribution.

  5. shan
    June 28th, 2006 at 13:57 | #5

    This is not a comment, but a question,
    I have two live applications running, which I cannot tell of here. Application A is a shopping site, and Application B is a Merchant Control Site:

    Application A is hosted on a windows server. whilst Application B on a linux.

    A posts form to B using simple HTML form for credit card approval, B replies back using form post at a specified URL at A, but always get an error on curl Could not Connect, any Ideas, any help very much appreciated am stuck on this for quite lots of days.

    I have site C also that is on linux, and is also shopping site, C does the same process but does not get any error in receiving response back from A.

    Any help is very much appreciated.

    Please email me at s4shani83@hotmail.com

  6. Joko Sunaryo
    July 4th, 2006 at 02:36 | #6

    Thanks for the article.

    Very helpful.

  7. Sheldon
    July 24th, 2006 at 16:18 | #7

    All this time and it still works – Thanks

  8. Tim
    July 27th, 2006 at 08:54 | #8

    xampp userse:

    for those pulling as much hair out as me, read this list to ensure you have all php.ini copies covered:

    http://www.menyhart.net/blog/developer-stuff/enabling-curl-on-xampp/

    why there’s not one I don’t know…

  9. Alex
    August 2nd, 2006 at 08:59 | #9

    Xampp inst. does not require any additional settings, but those mentioned by Tim above.

    Thanks Tim

  10. B.yondd
    August 2nd, 2006 at 12:49 | #10

    If you’ve just installed PHP5 on windows XP, the extension dir may be “extension_dir={your_php_dir}/ext”, and not “extensions”.

    Thanks Tony!

  11. August 4th, 2006 at 04:12 | #11

    Hi
    I am prasenjit modak I have yahoo addressbook grabber but i face problem to execute script call to undefined function curl_init()

    plz send me reply how to execute my script.
    inform me all the neccessary file that i have to set.

  12. prasenjit modak
    August 4th, 2006 at 04:16 | #12

    dear sir, i am not properly integrate curl in my php project .kindly tell me the procedure how to integrate these.when run the link then curl_init() is not found.

  13. Caty Tota
    August 4th, 2006 at 05:26 | #13

    You guys are the 80582 best, thanks so much for the help.

  14. Friedrich
    August 18th, 2006 at 18:18 | #14

    I realise this article is quite old – but I came across it so others might too.

    First of all as jb correctly pointed out, you don’t need to install curl if you just want to use curl in php. Nor do you need to install openSSL to access secure pages using curl with PHP.

    By the way, and this is important, setting CURLOPT_SSL_VERIFYPEER to false (or 0) undermines the security system – SO DON’T DO IT – It is not a solution to your problems.

    To use ssl correctly, just download curl for the “curl-ca-bundle.crt” file as suggested by jb and give its location with CURLOPT_CAINFO.

    Here’s a tip: don’t copy libeay32.dll and ssleay32.dll to your system32 dir. Instead add the php directory to your system PATH. This is recommended in other places in the php manual, but was left out on the curl page. To quote http://www.php.net/manual/en/install.windows.manual.php

    “And as you’ll soon learn, the preferred method for installing PHP is to keep all PHP related files in one directory and have this directory available to your systems PATH.”

  15. WaltDe
    September 1st, 2006 at 02:27 | #15

    Keep up the great work on your blog. Best wishes WaltDe

  16. Sally
    September 5th, 2006 at 21:32 | #16

    Hello there.

    If you want to install cURL on Windows, make sure that you’ve copied “php.ini” from the PHP folder (mine is currently “php-4.3.5-Win32″) to your SYSTEM or SYSTEM32 folder (I’m still on Win ME, so it’s SYSTEM). Open the copied version to the SYSTEM folder and look out for these lines:

      ; Directory in which the loadable extensions (modules) reside.
      extension_dir = “C:\php-4.3.5-Win32\extensions

    As you could see, just replace the VALUE of extension_dir to the full path (Windows-based PATH using BACK-slashes) of the folder “extensions” on your PHP folder.

    Then copy all of these libraries (DLL’s) from the PHP folder into the SYSTEM folder:
      php4ts.dll,
      ssleay32.dll,
      php_curl.dll, (inside “extensions”)
      msvcrt.dll

    I thought the last one should already be in the SYSTEM folder there :)

    cURL can then be enabled by uncommenting the line ‘extension=php_curl.dll‘ in the php.ini file. Alternatively you can load the module dynamically in your script using:

    <?php
    dl(“php_curl.dll”);
    ?>

    Create a simple PHP file that looks something like this:

    <?php
    echo phpInfo();
    ?>

    … and save it as “phpInfo.php” or anything up to you. Then run it via your Web browser. If the cURL installation was done successfully and is now enabled, you’ll see the “curl” section from the page — mine is after “ctype and before “ftp“.

    [Good Reference:

  17. Sally

    Woopss!

    Sorry… I was in a bit hurry when I posted my first comment :P I’m pretty sure you can figure things I’ve ‘coded’ mistakenly like the <A&gt! Also the <br>s Heheheh…

  18. arul
    September 12th, 2006 at 01:51 | #18

    So i don’t know how to install curl and configure this.please help me as soon as possible.
    please give me the easy steps to go one by one.
    Thanks very much.

  19. sammy
    September 14th, 2006 at 21:52 | #19

    Why turn register_globals on? This is a security risk and is not recommended. I got curl working fine with this setting off.

  20. Jeff
    September 27th, 2006 at 22:51 | #20

    Sorry, I just don’t get it. I need to post information from a secure site to a secure site which will write the information into a database and return a reply. How do I get curl to do this. The example above worked but how do I post.

    This worked but how do I post to something like this?

    $url = “http://www.thinkgeek.com”;

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL,$url);

    curl_setopt($ch, CURLOPT_VERBOSE, 1);

    curl_setopt($ch, CURLOPT_POST, 0);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

    $returned = curl_exec($ch);

    curl_close ($ch);

    echo $returned;

    Thanks, I am getting closer.

  21. Bruce Macdonald
    October 26th, 2006 at 20:13 | #21

    I fixed my

    Fatal error: Call to undefined function: curl_init()

    issue on WinXP.

    I followed the instructions above but refused to
    believe that you need to put the php.ini file in
    the windows/system32 directory. After all, if you
    put php before the windows & system32 entries in your path windows should load these first. Right?
    I don’t know and right now don’t care. I
    put the php.ini file (after all modifications as
    outline et al above) into the windows/system32 directory. I then got curl showing up in a phpinfo.php.

    whew!

  22. arena
    October 28th, 2006 at 20:13 | #22

    I check everything about your chack-list and still have

    PHP Startup : unable to load dynamic library ‘C:\php\ext\php_curl.dll’

    i am with PHP 5.0.2 under Windows XP SP2

    if you or anybody have an idea.

  23. webdsn
    November 14th, 2006 at 21:37 | #23

    Amazing!!! 10+
    No pain! Easy install!
    Thank you!!!
    +++++++++++++++++++++++

  24. phil
    November 15th, 2006 at 11:33 | #24

    yes it does not work for me either, there is no php_cull.dll in tany directiory of that distribution as far as I can see

  25. sundaresan
    November 22nd, 2006 at 01:49 | #25

    Just copied the php_curl file to php directory and uncommented extension=php_curl.dll and set globals on it worked on windows2000.
    Thanks
    Sundaresan

  26. gary g
    December 6th, 2006 at 18:07 | #26

    Thanks everyone, esp. Eric\Pokerface…

    My “Fatal error: Call to undefined function: curl_init()” error was resolved after I realized I was changing the wrong php.ini file. Who knows why there’s a separate on in my apache\bin directory. Maybe I put it there a long time ago or maybe XAMPP makes a copy there.

    C:\Program Files\xampp\apache\bin\php.ini

    Searching my xampp directory tree showed more than one php.ini file.
    C:\Program Files\xampp>grep -idl php php.ini*
    File apache\bin\php.ini
    File php\php.ini
    File php\php.ini-dist
    File php\php.ini-recommended
    File php\php4\php.ini
    File php\php4\php.ini-dist
    File php\php4\php.ini-recommended

    I’m running PHP Version 5.0.4
    Thanks everyone!

  27. Girish
    January 3rd, 2007 at 03:36 | #27

    Thank you sally
    Now my curl is working like a knife on a butter.
    Thanx again bye.

  28. r.patel
    January 4th, 2007 at 04:10 | #28

    hi,

    that was really a great piece of work. excellent explanation. curl is now working fine.

    thanks much

  29. Girish
    January 10th, 2007 at 01:42 | #29

    I want to upload a file using cURL.
    I have installed zlib and libgcc packages.
    But i m not getting an parse error.
    So any one having code for it with explanation
    Please reply

  30. April 17th, 2007 at 18:42 | #30

    this worked for me — it is from the php manual (http://www.php.net/curl)

    Note to Win32 Users: In order to enable this module on a Windows environment, libeay32.dll and ssleay32.dll must be present in your PATH.

    You don’t need libcurl.dll from the cURL site.

  31. TheRods
    June 18th, 2007 at 01:13 | #31

    How did you manage to set it up on Linux without compiling php? Please provide the steps.

  32. June 21st, 2007 at 17:43 | #32

    Great Page.
    But I was sitting behind a firewall and I had not set up the proxy. Thus whenever I executed the php script, I got a blank page!! Solution:
    curl_setopt ($ch,CURLOPT_PROXY, “proxy_server:port”);

    Also very good debug option: A must for beginners

    print_r(curl_getinfo($ch));
    echo “\n\ncURL error number:” .curl_errno($ch);
    echo “\n\ncURL error:” . curl_error($ch);

  33. Sebastian
    July 14th, 2007 at 13:09 | #33

    Thanks works perfect!

  34. vanchin
    July 15th, 2007 at 23:23 | #34

    hi,

    currently i’m trying curl with tomcat5.5 and php5, i followed your steps, but I dont know how to do the step 9.

    please guide me.

    thanks in advance.

    vanchin

  35. Paulo
    August 31st, 2007 at 13:17 | #35

    For those that don’t work try this to see if you have the curl enable:

    I had the php.ini file in the windows directory and was editing the php.ini on the php directory, and with that i found the problem.

  36. Alaf
    September 5th, 2007 at 05:43 | #36

    i am using php5 with apache server on windowsXP my curl library is also enable but i am getting this error:

    7 Failed to connect to xx.xxx.xxx.xx (ip): Permission denied

    plz help me

  37. September 12th, 2007 at 16:11 | #37

    Not sure if this will help anyone because it appears that everyone who has installed PHP, IIS, and cURL didn’t encounter any issues.

    My issue was that everything worked, PHP, IIS, cURL, but not cURL under SSL even after adding “curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);” to my test script. The answer, found on the PHP.net site (“http://fr.php.net/curl”), use …

    “curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);”

    It works…

  38. Vishwa
    September 23rd, 2007 at 09:14 | #38

    Easily worked for me…thanks !

  39. October 9th, 2007 at 03:50 | #39

    hy … i installeted on that tutorial a xampp pack …and ” all`s greate ” but curl don`t running … isn`t in phpinfo is not a curl enable .. maby xamp pack don`t compil a php with curl ?

    pls help me ;)

  40. TLT
    November 14th, 2007 at 21:31 | #40

    Hello,
    I have got PHP With IIS in win-XP.I am getting one error in the below program as follows.
    “Fatal error: Call to undefined function: curl_init()”

    So i don’t know how to install curl and configure this.please help me as soon as possible.
    please give me the easy steps to go one by one.
    Thanks very much.

  41. Geekfish
    January 13th, 2008 at 14:11 | #41

    Thanks a lot, your instructions really helped me out of this one :)

    A little note to all XAMPP users,
    php.ini is not loaded from the file found in the php installation directory, but from apache/bin/php.ini

  42. LarryH
    January 22nd, 2008 at 18:56 | #42

    For those interested in just getting cURL capability going (perhaps because you already have PHP & Apache running but now need the curl stuff), just follow items 4&8 from the tutorial. the php_curl.dll won’t load (step 8) until you perform step #4 when you restart the Apache server.

  43. Sina
    March 31st, 2008 at 02:05 | #43

    oh mate , that was awsome ,… how come u’r the only resource for this?

  44. gl
    May 2nd, 2008 at 09:55 | #44

    Can anyone help me get this working. I can’t get the curl module to load. I uncommented the line in the php.ini file and have the dlls in the system32 dir but, in phpinfo() the module does not show up.

  45. JimD
    May 5th, 2008 at 17:46 | #45

    Having same problem as g! above. Both copies of php.ini have been adjusted (one under c:\Program Files\PHP and one under c:\WINDOWS\system32), the Apache .conf file has been modified, the extension path has been set, etc., etc. but curl still refuses to load. Been working on this for 3+ days (pushing 30 hours). Oh yeah: Windows XP SPII, Apache 2.2, PHP 5.2.5, curl 7.18.1. Any obscure idea would help in this battle.

  46. Jim D
    May 19th, 2008 at 13:00 | #46

    For some reason, Apache was not picking up the path for my php.ini file, but was looking in the C:WINDOWS/ folder for it. Another issue was that php was looking in c:php5/ext by default for my extensions. I placed the .ini folder in the C:WINDOWS/ folder and placed the ext folder in the default location and cURL loaded perfectly. This is only a short term solution, but at least it got me up and running. Hope this helps.

  47. Loc
    June 4th, 2008 at 04:08 | #47
  48. Summer
    June 5th, 2008 at 08:30 | #48

    Thanks! I just did step #4 and uncommented the extensions for php_curl.dll and php_openssl.dll in the php.ini file; worked like a charm.

  49. Joe
    June 6th, 2008 at 23:39 | #49

    Like many of you, I had trouble installing CURL on a Windows Box running Apache and PHP. My exact setup is Windows XP with Apache 2.2.6, and PHP 5.2.6. (The latest releases of both.)

    It seems to me that the new version of PHP does things a bit differently with respect to importing modules, and the instructions given here can’t really be applied to the current version of PHP without some modification. Obviously, I have no idea what these mods are, and you probably don’t either.

    But, THERE IS A SOLUTION. When you install PHP via the .exe file, the default options are to install the program of PHP itself along with a few really common extensions. BUT, you can also install less commonly used extensions (like CURL) by simply checking them off under the little extensions box. I just did this and voila! – everything works.

    No sense in killing yourself over this when the installer does it for you.

    Good luck.
    -Joe

  50. Amit kumar
    June 9th, 2008 at 05:14 | #50

    I have problem with the 9th step

    - Uncomment: LoadModule php4_module (no line contains this string, so what have I to do. By the way I have added the following line(Tell me is it correct or not?)

    LoadModule php4_module modules/mod_dir.so

    d:/apps/php/sapi/php4apache2.dll(What is this?)

Comment pages