Basically what it does is connects to other websites and gets information.
cURL is a command line tool used to transfer files with URL syntax
The data transfer protocols supported by cURL include HTTP, HTTPS, FTP, FTPS, GOPHER, LDAP, DICT, TELNET and FILE.
PHP cURL allows you to read websites, make automated logins, upload files and many more. I have used
it for creating Twitter API, URL shortening, Facebook API and also for downloading feed updaters for
a web site.
Using it is a 4 step process.
1. Initialize the curl,
$ch = curl_init();2. Set the options for curl. This is where all the action takes place
curl_setopt($ch, CURLOPT_HEADER, 1); //Display the server response header. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//This tells cURL to return the data instead of displaying itBy default, the result is displayed in the browser.
3. Execute and fetch result
$yahoo=curl_exec($ch); //The result is stored in $yahoo.4. Free the curl handle
curl_close($ch);A sample server response header
HTTP Headers for www.boatbuys.ca ============== HTTP/1.1 302 Found Date: Tue, 19 Jul 2011 14:10:44 GMT Server: Apache/2.2.17 (Unix) mod_ssl/2.2.17 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4 PHP/5.3.4 X-Powered-By: PHP/5.3.4 Set-Cookie: PHPSESSID=f629dd2154a7ea018812e52f5d5541ed; path=/ Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Location: http://www.boatbuys.com/ Vary: Accept-Encoding,User-Agent Connection: close Content-Type: text/html; charset=utf-8The problem with using cURL in the above example is that all the info must be first fed to the memory.
If the info is large, this can be a hassle. Instead, we can use a file to store the info using the CURLOPT_FILE option
Steps:
-Create a new file pointer using fopen
-Pass the file pointer to cURL
-Perform the request
eg:
<?php $url = 'http://www.php-pranthan.blogspot.com/a-large-file.zip'; //source $path = 'file.zip'; //destination file location $fp = fopen($path, 'w'); $ch = curl_init($url); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);//Follow any "Location:" header that the server sends as part of the HTTP header $data = curl_exec($ch); curl_close($ch); fclose($fp); ?>For more info:
http://ca2.php.net/curl_setopt
No comments:
Post a Comment