PHP cURL functions with example
cURL is stand for client URL
It is a library (libcurl) which allows you to connect and communicate to many different types of servers with many different types of protocols.
libcurl supports http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, and user+password authentication ( >PHP 4).
Using cURL you can do
- Implement payment gateway’s scripts (communication between payment gateway and your website script).
- Login to other websites and access their members only sections (Read mails or get contacts).
- Check whois/domain availability.
- Download and upload file from remote server.
curl_opt– This function is called multiple times and specifies what we want the cURL library to do.
This is used to specify the URL which you want to process.
Setting this option to 1 will cause the curl_exec function to return the content instead of echoing them.
You can find full list of curl_opt by click here
Write the contents to a file as it downloads a web page or file.
curl_exec – Executes a cURL session.
curl_close– Closes the cURL session.
Below are some examples.
/** reading the content/feed of a website */
/* Initialize the cURL session */
$ch = curl_init();
/* Set the URL of the page or file to download or read content */
curl_setopt($ch, CURLOPT_URL, ‘http://www.scriptarticle.com/feed/’);
/* ask cURL to return the contents in a variable instead of simply echoing them to the browser */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
/* execute the cURL session */
$content = curl_exec ($ch);
/* Close cURL session */
Another example for check/fetch the domain who is information.
/** whois-domain availability check */
$domain = "scriptarticle.com";
$data = ‘http://’.$domain;
$ch = curl_init($data);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch); // Check if any error occurred
echo ‘The domain is available!’;
echo ‘The domain is not available’;
Hope it all make sense !! post your comment or suggestion below if you need any more assistance.