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. trieu
    October 27th, 2004 at 02:33 | #1

    Thanks for writting this. These steps worked like a charm on windows xp with php and apache installed.

    Although it works, I don’t understand how apache or php knows of the executable in d:\apps\curl; this directory is not reference anywere in apace or php. Do they use them at all?

  2. Jerry Kita
    November 24th, 2004 at 20:28 | #2

    Thanks …. it worked great. Like the previous comment-er I also don’t know how apache or php knows of the whereabouts of the curl executable. Nevertheless it works and your instructions were very helpful.

  3. jb
    December 7th, 2004 at 20:39 | #3

    Regarding the comments above, php does NOT need the standalone windows curl program to be installed. It implements libcurl in “php_curl.dll” which is included in the php extensions directory of the full windows binary installation. However, php does NOT include the certificate bundle file, which, AFAIK, is the only reason you’d need to download the standalone curl app for Windows — so you have a file to use when specifying “CURLOPT_CAINFO”.

  4. alixta
    February 20th, 2005 at 20:50 | #4

    Thanks for the tip about SSL – I was using code supplied by our CC solution (eway.com.au) & it didn’t include the ‘CURLOPT_SSL_VERIFYPEER’ line.

    A few hours of hacking I’ll never get back … *sigh*

  5. Mark B
    March 6th, 2005 at 22:21 | #5

    I’m not getting anywhere with this. I’ve got PHP and Apache working well (along with MySQL) on WinXP. I’m getting:

    “Fatal error: Call to undefined function: curl_init() in C:\webpages\mark\test.php on line 20″

    on the curl_init line of your sample code. I’ve followed your instructions, step by step as below.

    1) Didn’t do anything – php already installed this way
    2) made changes as specified
    3) done
    4) done
    5) done
    6) NA (don’t care about ssl support)
    7) file exists 8) done
    9) done previously when installed php

    Apache restarted before page loaded where error obtained. PHP runs fine – phpMyAdmin and I are getting along famously. Other php pages loading. Just cURL doesn’t want to work.

    Any suggestions for troubleshooting? I’m a bit of a newbie at this whole thing. Thanks for your help.

  6. Koas
    March 8th, 2005 at 07:04 | #6

    Thanks very much, it worked great for me!

  7. xmanticorex
    April 16th, 2005 at 06:16 | #7

    thanks it works!

  8. Beth Brennan
    May 16th, 2005 at 09:25 | #8

    omg, you clever, clever person!! i’ve been winding myself up about this at work for days

    your solution totally worked!! *hugs*

  9. Amit Mani
    June 10th, 2005 at 02:42 | #9

    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()”
    This is the program……….

    $curlPost = ‘pop=’ . urlencode($pop) . ‘&alias=’ . urlencode($uid) . ‘&pass=’ . urlencode($txtPass) . ‘&SUBMIT=Send’;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, ‘put here url whihc needs to be executed’);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec($ch);
    curl_close($ch);

    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.

  10. Yousifi
    June 22nd, 2005 at 05:11 | #10

    Thanks for the instructions.

    Yet I have a problem with posting to a test page I have on my localhost. When I use curl to post:

    $post = Array();
    $post[1] = urlencode(‘test1′);
    $post[2] = urlencode(‘test2′);
    $post[3] = urlencode(‘test3′);

    $curl = curl_init(“http://localhost/curltest/post.php”);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
    $response = curl_exec($curl);
    curl_close($curl);

    —- post.php

    echo “CONTENTS:”;
    print_r($_POST);

    With my understanding of curl, this should print the POST arrays contents which it received via curl.

    But I get nothing.

    Can anyone help me? Thanks in advance :)

  11. william
    July 2nd, 2005 at 19:18 | #11

    thanks very much for the info, worked very well.

  12. July 8th, 2005 at 16:37 | #12

    Hi,
    I’ve a problem with curl…
    How can I test if the port 443 are open or closed? (is it available or not?)

    I run the following script to access my bank url, but get the errormessage :”couldn’t connect to host “..

    Thanks
    Enes
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, “https://xxxxx.com”);
    curl_exec($ch);

    if (curl_errno($ch)) {
    print curl_error($ch);
    } else {
    curl_close($ch);
    }

  13. Zahirul
    July 30th, 2005 at 03:56 | #13

    I can run my apache after adding this line in my httpd.conf :LoadModule php4_module c:\php\sapi\php4apache2.dll

    c:\php is my php home dir.

    Pls give comment.

  14. hkboy
    August 2nd, 2005 at 01:23 | #14

    Hi, thanks for these instructions. Only problem i am facing is, images don’t show up. It works fine but only images of the target site don’t show up. i am using windows 2003 server. do the directy session.save needs to have some special permissions? thanks

  15. Oliver
    August 3rd, 2005 at 17:37 | #15

    Excellent…. Couldn’t find the lines in step 9 but figured as already had PHP and Apache working not needed.

    Your guide – saved me a lot of time!!

    Cheers
    Oliver

  16. TM4B Bulk SMS Gateway
    September 4th, 2005 at 02:01 | #16

    I was under the impression that values only needed to be url encoded if the ‘GET’ method was being used.

    Could someone confirm whether it is necessary or not to urlencode values when CURLOPT_POST is being used. //Thank you – Abu Aaminah

  17. Per
    September 13th, 2005 at 10:00 | #17

    To those of you who experienced “Fatal error: Call to undefined function: curl_init()” error:

    I got the same error message, and I restarted Apache. Then it worked 100%!

    Nice article! =)

  18. M@
    September 27th, 2005 at 17:31 | #18

    Thank You!!!!!!! It works great. Keep up the good work.

    Awsome!!!! Thank you

    -M@

  19. Rooster
    September 28th, 2005 at 23:29 | #19

    What are the procedures for setting this cURL with ssl support using php 5 and iis6?

  20. Royce
    September 29th, 2005 at 02:10 | #20

    Whats the setup procedure for iis6 and cURL with php5?

  21. Mukesh Variya
    October 10th, 2005 at 07:34 | #21

    Hello,

    I am php web programmer looking for php curl support. I tried to find curl library for windows platform, but there several links on google that gives php_curl2.dll information. But the problem is that there is no link to download that files.
    Please help me. send your suggetion on mukeshvariya34@yahoo.com

  22. Sellgren
    October 14th, 2005 at 01:04 | #22

    I’m using php 4.3.11 on Windows2k3 to talk to SalesForce.com’s web services (using the PEAR SOAP libraries). I was getting error “SSL certificate problem, verify that the CA cert is OK” when I tried to talk to the https SalesForce server.

    You can fix the problem directly using Tony’s CURLOPT_CAINFO info at the end of his first post, but if you don’t want to go hacking into the PEAR library files to find where curl_init is called, you can fix it when you initialized SOAP:

    get the ca-bundle.crt out of the the cURL source distribution(http://curl.haxx.se/download.html). It lives in the lib folder
    put it somewhere on your server, like c:\windows
    right before you initialize SOAP_Client, use SOAP_Client->setOpt with CURLOPT_CAINFO to point to the ca-bundle.crt

    class WebService_SforceService_Soap extends SOAP_Client
    {
      function WebService_SforceService_Soap() {
        $this->setOpt(‘curl’, CURLOPT_CAINFO, ‘c:\windows\ca-bundle.crt’);
        $this->SOAP_Client(“https://www.salesforce.com/services/Soap/u/2.5″, 0);
      }

    You can easily hack that idea for other SOAP uses.

  23. Tony Spencer
    October 14th, 2005 at 15:26 | #23

    Great tip Sellgren!

  24. Simbo
    October 16th, 2005 at 08:16 | #24

    Thanks Tony! This is a great tip. It even works with Zend Studio 4 which is the php IDE and debugger. Add the folowing lines to C:\Program Files\Zend\StudioServer\etc\php.ini

    extension_dir=”C:\Program Files\Zend\StudioServer\lib\php_ext”

    sessions.save_path=”C:\Program Files\Zend\StudioServer\lib\temp”

    extension=php_curl.dll

    then create the directory C:\Program Files\Zend\StudioServer\lib\temp. Then you have OpenSSL and Curl support running so you can debug your encrypted soap calls. Neat!

  25. Bill
    October 25th, 2005 at 19:25 | #25

    I thank you too!
    I couldn’t figure out how to get curl working on my xp laptop.
    Thanks!!!

  26. cynthia
    October 26th, 2005 at 15:03 | #26

    Hi,

    I am the client side of a SOAP service. I got the certificate from the server and I need to access the service using this certificate/key. I am working on Windows XP. The following code is written in PHP but doesn’t work. The message is: “Unable to retrieve WSDL”.

    Any help is greatly appreciated.

    ======================

    require_once (“SOAP/Client.php”);

    # $options['curl']['CURLOPT_SSLKEY']=”c:\certificate.pem”;

    ## $options['curl']['CURLOPT_SSLCERT']=”c:\exportedcertificate.pfx”;

    $options['curl']['CURLOPT_SSLCERT']=”c:\certnew.cer”;
    $options['curl']['CURLOPT_SSLKEY']=”c:\server.keystore”;
    $options['curl']['CURLOPT_SSLKEY']=”c:\certificate.pem”; ?????????should be this

    $options['curl']['CURLOPT_SSLCERTPASSWD']=”pw”;
    $options['curl']['CURLOPT_SSL_VERIFYPEER']=0;
    $options['curl']['CURLOPT_SSL_VERIFYHOST']=0;
    $options['curl']['CURLOPT_VERBOSE']=1;
    $options['curl']['CURLOPT_CAINFO']=??????? what should this be?

    $soapclient = new SOAP_Client(‘https://…./postal.asmx?WSDL’, true, false, $options);

    print_r ($soapclient);

    ======================================

    When print out the info of SOAP_Client object, I got “Unable to retrieve WSDL.

    Thanks in advance

  27. Cynthia
    October 26th, 2005 at 18:12 | #27

    Hi, This is still Cynthia who posted the last messge. I also tried the following solution, the error message is

    Error:
    58 – unable to set certificate file (wrong password?)

    But I am sure the password IS correct.. I gave the multiple settings to CAINFO and SSLCERT just for testing. I had a 5*4 combination for testing. nOne of them worked.

    any help is appreciated!

    ================

    #$url = “http://www.thinkgeek.com”;
    $url=”https://…./postal.asmx?WSDL”;
    $ch = curl_init($url);

    #curl_setopt($ch, CURLOPT_CAINFO, ‘c:\curl-7.15.0\lib\ca-bundle.crt’);

    #curl_setopt($ch, CURLOPT_CAINFO, ‘c:\certificate.pem’);

    #curl_setopt($ch, CURLOPT_CAINFO,’c:\certnew.cer’);

    #curl_setopt($ch, CURLOPT_CAINFO,’c:\exportedcertificate.pfx’);

    #curl_setopt($ch, CURLOPT_CAINFO,’c:\server.keystore’);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($ch, CURLOPT_SSLCERT,’c:\certnew.cer’);
    #curl_setopt($ch, CURLOPT_SSLCERT,’c:\certificate.pem’);
    #curl_setopt($ch, CURLOPT_SSLCERT,’c:\exportedcertificate.pfx’);

    #curl_setopt($ch, CURLOPT_SSLCERT,’c:\server.keystore’);

    curl_setopt($ch, CURLOPT_SSLCERTPASSWD,’xzhengpw’);

    ##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);
    echo $returned;

    if ($returned == NULL) {
    echo “Error:”;
    echo curl_errno($ch) . ” – ” . curl_error($ch) . “”;
    }

    curl_close ($ch);

    Thanks a lot

  28. Gene K
    November 17th, 2005 at 22:42 | #28

    I have a working Apache2 PHP5 server running on Windows XP. After following Tony’s instructions I am experiencing the same trouble as several users above.

    I have posted a php web page with Tony’s testing code. The browser answers with the following error code:

    Fatal error: Call to undefined function curl_setopt() in C:\Data\GNK-site\Curl\CurlTest.php on line 9.

    Having no prior experience with CURL, I’m not sure if the problem lies in configuration or in the posted test page.

    According to PHP.NET, Curl must be compiled into PHP5, yet Tony has not included any compile procedures in the instructions.

    I would appreciate any suggestions/explanations you might offer. I can be reached directly at geno11@softhome.net.

    Gene K.

  29. Gene K
    November 18th, 2005 at 02:35 | #29

    Supplement to my previous post: I installed Apache2, PHP, MySQL, and CURL in the WinXP “Program Files” directory which contains a space. Even though everything else is working properly, could the space in the path be causing my problem?

  30. yoni
    December 4th, 2005 at 16:55 | #30

    Thanks man.

    I was frustrated with getting nothing for SSL sites when it worked ok with HTTP sites and also with HTTPS from the command line.

    You are my hero…

  31. dylon
    December 14th, 2005 at 04:54 | #31

    Hie
    I aslo installed php5 and Apache on winXP and these are working fine.I followed tony spencer’s steps to installing cURL and used the test code given,I get no response.i also tried the following suggestions give but still I get no response.

    Where could the problem be?Please help.

  32. Commodore
    January 9th, 2006 at 13:32 | #32

    I get the same

    Call to undefined function curl_init() in . . . crack smoking POS Windows box.

    error that some others have been getting.

    php -m says curl is a compiled in module. WTF???? If it’s a compiled in module then why is the function undefined?

  33. Anonymous
    January 21st, 2006 at 20:33 | #33

    Alright all, i just tested and it works fine here. This is how to enable curl in iis. First, make sure 3 files is in the c:\php dir, php_curl.dll, php4ts.dll, php.exe. You can install the binary version of php in windows. ALso, download the zip file of php. So you have both the binary n the zip file. The binary version doesnt have the file php_curl.dll and you must copy it from the zip version. Make sure all 3 files are using the same version. After that, edit php.ini to uncomment extension=php_curl.dll and test it with this small script:

    Save this script call it curl.php and placed in the inetpud/wwwroot. Use a browser to point to the script eg: http://localhost/curl.php and if it works, you should get this message: string(40) “libcurl/7.14.0 OpenSSL/0.9.8a zlib/1.2.3″

    By the way, also install openssl from here: http://www.shininglightpro.com/products/Win32OpenSSL.html

    That will get curl working. Peace.

  34. Eric
    January 29th, 2006 at 15:09 | #34

    I am new to php and just wanted to pass along something I learned.

    Verify the path to php.ini. You can do this by running phpinfo().

    I was changing the wrong php.ini file and was going crazy.

    The key is to make sure you are changing the right php.ini file.

    This tutorial help

    Thanks

  35. vishal
    February 1st, 2006 at 07:52 | #35

    Hello sir,

    I used curl according to you. i have installed all things according to u. but even after i faced following error:

    Fatal error: Call to undefined function: curl_init() in d:\apache group\apache\htdocs\clients\try\test.php on line 3

    so please tell me what should I do.
    Thanking you

  36. arch
    February 3rd, 2006 at 07:46 | #36

    i tried all this, but still i get curl_init() not defined

  37. pokerface
    February 17th, 2006 at 11:02 | #37

    I am new to php and just wanted to pass along something I learned.

    Verify the path to php.ini. You can do this by running phpinfo().

    I was changing the wrong php.ini file and was going crazy.

    Same here. thanks for the tip, tutorial works great.

  38. jomel
    March 2nd, 2006 at 00:24 | #38

    Hi,

    I’m working with curl in my client right now. I’ve tried my script in another server and it’s working and when I transfered it to another server, problem occurs:

    Error 58: unable to set certificate file (wrong password?)

    I don’t understand why this error occured. I haven’t changed any certificate file. I just copied everything and that’s is.

    Here is the info of the new server:
    PHP 5.0.4

    While in my server its PHP 4.3.11.

    Do you think the php version matters?

    If you have time kindly give me some answers..

    Thanks and God bless…

  39. March 10th, 2006 at 05:50 | #39

    dude, you totally f***ing rock.
    i can’t express how completely fed up i’d become with uploading, testing, tweaking and re-uploading cURL intensive apps to my web host when i have a perfectly fine dev environment on my laptop…
    well now, thanks to you that ‘clean room’ now features fully functioning cURL. THANKS MUCH.

    btw, i can verify that adding:

    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($ch,CURLOPT_CAINFO,’drive:/path/curl-ca-bundle.crt’);

    right after the call to curl_init works like a charm for ssl url’s.

  40. March 16th, 2006 at 16:07 | #40
  41. Aish
    March 26th, 2006 at 16:23 | #41

    I just want to say that this was EXTREMELY helpful! Thanks so much!

  42. Subhojit Dhara
    April 11th, 2006 at 01:13 | #42

    Hi,
    I am a freshers and I am working on php for last 2 months.I am doing a project where multi tasking concept is needed,but as in php there is no direct multithreading concept,so I want to do my job with curl or multiprocess concept.I am using unix platform.Please help me in this context with some codes and examples having multitasking or parallel processing.
    Thanks and regards,
    Subhojit

  43. Remi
    April 11th, 2006 at 12:22 | #43

    This was extremely helpful. I thought I need to hack at this for 2 days… it took me 5 mins.

    Thanks

  44. tajik
    April 12th, 2006 at 02:53 | #44

    Please help me
    I’m Using Php 5 And For connecting To Https
    Please Help Me for Configuration Php.Ini
    My error Is
    wsdl error: HTTP ERROR: cURL ERROR: 7: Connect failed; No error
    url: https://www.pec24.com:443/pecpaymentgateway/eshopservice.asmx?wsdl
    http_code: 0
    header_size: 0
    request_size: 0
    filetime: -1
    ssl_verify_result: 0
    redirect_count: 0
    total_time: 0
    namelookup_time: 0.031
    connect_time: 0
    pretransfer_time: 0
    size_upload: 0
    size_download: 0
    speed_download: 0
    speed_upload: 0
    download_content_length: 0
    upload_content_length: 0
    starttransfer_time: 0
    redirect_time: 0

  45. Abhishek
    April 18th, 2006 at 07:15 | #45

    hello all

    i am sorry for posting my doubt here, but found no way to post it.
    i am new to curl and incorporating it with java to get the desire webpage. i had got curl lib but not able to work it with java, so pleease anybod who had worked over it then guide me.
    Thanks in advance.

  46. phpthoughts
    April 24th, 2006 at 00:31 | #46

    That’s a great writing. I really find it very helpful while installing curl. It works quite nice.
    Thanks for providing such a great document.

  47. Matt
    May 9th, 2006 at 22:49 | #47

    I don’t get it.. You downloaded curl and openssl into d:\apps\curl, but you don’t reference that path anywhere else in the configuration of httpd.conf or php.ini. When you enable curl in php.ini, you use the default curl library that comes with the php distribution, “extension=php_curl.dll”. So, how does php/apache know about the dlls you downloaded to d:\apps\curl? Doesn’t the above steps you outline just use the default php_curl.dll and not the other ones?? I tried the above steps, and I can’t use curl to query any https addresses…

  48. pedroac
    May 11th, 2006 at 14:06 | #48

    I tried to do as told, but couldn’t fix the error. For those still with problems (as i was until a few minutes), please refer to
    http://www.zend.com/pecl/tutorials/curl.php

  49. Abhishek
    May 29th, 2006 at 06:10 | #49

    hi all

    iam a JAVA programmer and iam new to curl. Can anyone can help me out to work in curl and incorporating its command in java api so as to run it from the console.

  50. Dino
    May 30th, 2006 at 05:15 | #50

    For those with “Fatal error: Call to undefined function: curl_init()” that wasn’t fixed by any of the above comments.

    I hard-coded the extensions directory (for me it is extension_dir = “C:/php-4.4.2-Win32/extensions/”) and all was schweet

Comment pages
1 2 3 29