PHP cURL functions & example

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.
PHP cURL wraps up major parts of functionality in just four functions. Simply PHP cURL follows the following steps in sequence.
curl_init – Initializes the session and returns a cURL handle(eg. $ch) which will passed to other cURL functions.
curl_opt– This function is called multiple times and specifies what we want the cURL library to do.
CURLOPT_URL
This is used to specify the URL which you want to process.
CURLOPT_RETURNTRANSFER
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
CURLOPT_FILE
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.

[php]

<?php

/** 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 */
curl_close($ch);

?>

[/php]

Another example for check/fetch the domain who is information.

[php]

<?php
/** 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

if(curl_errno($ch)){

echo ‘The domain is available!’;

}else{

echo ‘The domain is not available’;
}

curl_close($ch);

?>

[/php]

Hope it all make sense !!  post your comment or suggestion below if you need any more assistance.

16 thoughts on “PHP cURL functions & example”

  1. Pingback: Amedar Consulting
  2. If you facing any issue related to this post,please let me know..I’ll help you with full of my affords.

  3. Hey! Do you know if they make any plugins to help with SEO?
    I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good success. If you know of any please share.
    Thank you!

  4. Greate article. Keep posting such kind of info on your page.
    Im really impressed by it.
    Hi there, You have done an incredible job.
    I’ll certainly digg it and individually suggest to my friends. I’m sure they will be benefited from this website.

  5. Your mode of describing all in this paragraph is genuinely nice, all can effortlessly know it, Thanks a lot.

  6. We are a gaggle of volunteers and starting a brand new scheme in
    our community. Your web site provided us with valuable info to work on.
    You have done an impressive process and our whole
    community can be grateful to you.

  7. Pretty! This has been a really wonderful post. Thank you for providing this info.

  8. It’s going to be end of mine day, except before finish I am reading this fantastic paragraph to increase my know-how.

  9. Hi there great website! Does running a blog such as this take
    a massive amount work? I’ve no understanding of coding however I was hoping to start my own blog soon. Anyway, if you have any ideas or techniques for new blog owners please share. I understand this is off topic nevertheless I simply had to ask. Thank you!

  10. Hi colleagues, how is everything, and what you
    wish for to say concerning this piece of writing, in my view its really
    awesome designed for me.

  11. Thank you for sharing your info. I really appreciate your efforts and I am waiting for your further post thanks once again.

  12. I just like the valuable information you supply in your articles.
    I’ll bookmark your weblog and check again right here regularly. I am relatively certain I will be informed many new stuff proper here! Good luck for the following!

  13. Hi! Someone in my Myspace group shared this website with us so I came to look it over.
    I’m definitely loving the information. I’m bookmarking and will be
    tweeting this to my followers! Exceptional blog and fantastic design.

  14. Hey just wanted to give you a brief heads up and let you know a few of the pictures aren’t loading correctly. I’m not sure why but I think its a linking issue.

    I’ve tried it in two different browsers and both show the same outcome.

  15. Hi there to all, how is everything, I think every one is getting more from this web page, and your views are fastidious in favour of new people.

Comments are closed.