Posted by on in iDevelop, PHP | 20 comments

As I move into some advance PHP stuff, I came across the cURL. Now some of you might not know what is cURL, so here’s a definition from the cURL Website.

curl is a command line tool for transferring files with URL syntax, supporting FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE and LDAP. Curl supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, kerberos…), file transfer resume, proxy tunneling and a busload of other useful tricks.

So, putting that aside, basically, cURL is used(in PHP as I know it of) for behind the scenes communication between two webservers, something like webservices, but not webservices. Basically you POST the data(like in a form) to another webserver and GET the data returned by that server, its simply uses HTTP headers(usually) but you can use FTP etc. with it as well.

Now, that was somewhat basic intro to what cURL is, I don’t think its all correct but it somewhat paints the picture of what cURL is about, its just like Microsoft.XMLHTTP in ASP. Like if we need to post some data to a page & get some data in return, we do something like this in ASP

  1. Dim oMsXML, strResponse
  2. Set oMsXML = Server.CreateObject("Microsoft.XMLHTTP")
  3. oMsXML.Open "POST", "http://www.example.com/public", False
  4. oMsXML.Send "id=14&start=1&end=50&repeat=false"
  5.  
  6. ' get the response from the other server
  7. strResponse = oMsXML.ResponseText
  8.  
  9. ' release the XMLHTTP object
  10. Set oMsXML = Nothing

 
Now, its almost same in PHP, I mean the concept is same!!

  1. $oCURL = curl_init("http://www.example.com/public");
  2. curl_setopt($oCURL, CURLOPT_HEADER, 0); // set this to 0 as we don't need header info from the response
  3. curl_setopt($oCURL, CURLOPT_RETURNTRANSFER, 1); // set as 1 because we need the response data in a variable
  4. curl_setopt($oCURL, CURLOPT_POSTFIELDS, "id=14&start=1&end=50&repeat=false"); // use HTTP POST to send data
  5. $strResponse = curl_exec($oCURL); //execute post and receive data
  6. curl_close($oCURL); //close the cURL connection

 
Now both of these codes do the same thing, they POST some data and then get the resultant data.

This is usually done for behind the scenes communication on a server to server level, as I said earlier, like on an eCommerce website, where you enter your Credit Card info and the website interacts with the card processor’s server and process the transaction.

Ok, enough of the intro and background info. Coming back to the topic(as the post title suggests), I was pretty much frustrated when the cURL extension of PHP didn’t load after I uncommented its entry in the php.ini file and restarted the server, Windows2000+/IIS5+

After a bit of searching around, I found a solution for Apache on Windows, tried it with some modification and it worked. So here it is(incase I forget again).

  1. uncomment the line in your php.ini where it says php_curl.dll and save the file
  2. copy php4ts.dll and libeay32.dll, ssleay32.dll(these last two found in the “dlls” directory of your PHP directory) to the WINNT(Windows 2000)/Windows(WindowsXP and 2003) directory
  3. restart IIS(you might’ve to restart windows if you are on Windows 2000 or if you don’t have php.ini path specified in the registry)

Now the cURL extension will run smooth as butter!! This is assuming that you’ve PHP4 installed as an ISAPI with IIS and that you installed it manually instead of using an installer.

So you might benefit from this post if you are trying to use cURL on Windows/IIS but have been failing to load the extension.

I’m now going to run some scripts and test this!! 😉