Curl is a very interesting utility library which lets one connect and interact with any remote url via any of the supported standard protocol. Though curl is essentially a command line tool, various bindings are available for access via programming languages, popularly php
From a php perspective, curl can be used to remotely access any URL, login, authenticate, and perform other actions. Directly using curl to retrieve content is a no brainer like fetching rss feeds, etc.
:::php <?php $ch = curl_init( "http://www.foo.com/test.php"); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 ); $content = curl_exec( $ch ); ?>
Going beyond that a common scenario can be a contacts grabber, in which the user specifies the auth details, the script logins into email servers, fetch the contacts in a usable format. More interesting applications can be interacting with payment gateways, shopping carts, etc.
This weekend has been spent with curl after long time, extracting data and fooling around with it, it just too much fun !
There are lots of examples and documentation regarding handling of cookies with php curl. But there are couple of catches which always turned into dead ends for me in the past couple of years, and i never really bothered to find way around as i didn't needed them in the past. The curl documentation suggests you set the cookie handlers for curl this way, and usually as with any other php function relative paths should work, like this...
<?php curl_setopt( $ch, CURLOPT_COOKIEFILE, "file.txt" ); curl_setopt( $ch, CURLOPT_COOKIEJAR, "file.txt" ); ?>
But weirdly for some reason relative path doesn't work ! Since my weekend fooling with curl needed it, on playing around with it a little and discovered that giving absolute path works, it just works. So using the realpath() makes it all easy.
<?php //$fileName = "/home/kalyan/curlfoo/file.txt"; //or just use the real path function $fileName = realpath( "file.txt" ); curl_setopt( $ch, CURLOPT_COOKIEFILE, $fileName ); curl_setopt( $ch, CURLOPT_COOKIEJAR, $fileName ); ?>