Php Examples


Php Examples And Php Examples

Bookmark/Share This Page:

Bookmark and Share

Using CURL With PHP

CURL is the abbreviation for "Client URLs" and was developed by Daniel Stenberg in 1998. Basically, CURL is a set of functions that can be used for transferring information between webservers and clients using almost all known Internet Protocols. Formerly, CURL was a command line tool. That was the time when "libcurl" was invented, which is the interface to the CURL functions. Libcurl allows the webserver to send and receive data from another webserver or client, fast and easy. CURL and libcurl can run on any operating system, including Windows, Unix, BeOS or OS/2. It can be used in almost any programming language like C/C++, Visual Basic or Java. It is free and open-source, extremely customizable and there are no restrictions if you want to sell a product of your own that uses CURL.

In PHP also, CURL is available through "libcurl". When PHP is installed, libcurl is automatically installed too. Using CURL requires a very good understanding of PHP syntax and client/server interaction. If CURL is not enabled, please uncomment the following line in "php.ini" file:

extension=php_curl.dll To check that everything is in place, run the "php_info()" function and read the CURL functionality from there.

There are two ways of using CURL within PHP. We can use it like a command line tool, or from the "libcurl".

1. Using CURL as a command line tool From PHP

First off all, you must create a new ".php" file and of course, enable your CURL extension. For executing CURL command we will use "shell_exec" function from PHP. This function is used for executing commands in shells and return the output as a string. The following example will copy a web page and store it in a local file.

$var = echo shell_exec("/usr/bin/curl -o webpage.html");

The code is very simple. CURL has 3 parameters: "-o" tells CURL that a web page is going to be saved to a local file, "webpage.html" is the name of the local file, and "" is the target page. Now, a second example with a different protocol:

$var = echo shell_exec("/usr/bin/curl");

This code will retrieve the "HELLO" file using a File Transfer Protocol connection. Even if this command line method looks extremely easy, using "libcurl" is much easier.

2. Using CURL with Libcurl in PHP

In order to use CURL in php with "libcurl", we have to initialize the CURL session with the "curl_init()" function. After that, we will set the CURL options with the help of "curl_setopt()". Then, we only have to make requests using "curl_exec()" function. Let's have a look at the following example:

$curl = curl_init(); curl_setopt($curl, CURLOPT_URL, ''); curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); curl_close($curl);

We have 3 CURL option that must be set. First, the URL of the target site. Then the header and the mode how the information will be shown. If we set "CURLOPT_RETURNTRANSFER" to 0, the information will appear in browser. It is not only possible to retrieve information with CURL, you can also send it. In the following example we will send data to a form using "GET".

$hello = 'Hello World!'; $message = 'CURL sending information'; $curl_post = 'hello='.urlencode($hello).'&message='.urlencode($message).'&submit=Send'; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, ''); curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post); $result = curl_exec($curl); curl_close($curl);

This code will send the $hello and $message variable to a form through the "GET" method. The "urlencode()" function will handle all the formatting. CURL also supports authentication.

$curl = curl_init(); curl_setopt($curl, CURLOPT_URL, ''); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_USERPWD, 'user:password'); curl_exec($curl); curl_close($curl);

It is just another option to be set, "CURLOPT_USERPWD".

In conclusion, CURL is in this moment the best way to interact with servers or clients for PHP programmers. It will work with any technology, even secure connection or IPv6. It is easy to use, reliable and free, and that is why is the best choice.

By James Chambers -

Next page: Best Free Php Scripts

bring the fresh
Step by Step: how to install WordPress From Scratch Click Image above to learn more.

Option 5 - Order All Four Options Below for just $25 and get an additional 12 E-books as Your Bonus

SEO Ebooks

HTML Table
Money Making E-books
See Option 5
Option 1
  1. 5 Day Crash Course Copywriting Business
  2. 101 Basics To Search Engine Optimization
  3. AdSense Profits Unleashed
  4. Adwords Power Tips

  5. Just $9
Option 2
  1. Basic SEO Explained
  2. Black Hat SEO For Beginners
  3. Easy Articles
  4. List Exploding Squeeze Page Secrets

  5. Just $9
Option 3
  1. Local Business SEO Demystified
  2. Rock Solid SEO
  3. SEO Basics - Tips For Small Business Owners
  4. SEO For Busy Marketers

  5. Just $9
Option 4
  1. SEO Made Easy
  2. Squeezepages for Newbies
  3. SEO Predictions Package
  4. 82 Techniques More Money Into Your Pocket

  5. Just $9

video lesson ipads ipad pete

SEO Enigma Onsite Tycoon Software

Here is a cheaper alternative to the three videos above. The cost is only $5 and the video training course will teach you all free methods. What you will get is website building, autoresponders, article marketing, building amazon websites, how to create reports, 2.0 sites, generating traffic, Blogging and much more. More than 20 videos in all. Every topic you will need to build a real website business filled with many products

video lesson ipads ipad pete pc optimize Worldwide Brands

Recommended Products

Receive 6 Free Biz Tools Enter Details Below