3 PHP Curl Examples


CURL also called Client URL, is a tool for transferring data with URL. PHP supports CURL, if CURL has an extension of PHP enabled. CURL can be used for web-crawling, data-mining, proxy server building, making automated script for form submission etc.

First PHP CURL Example

The following example demonstrates the use of CURL in PHP:

<?php

// Init the CURL
$curl=curl_init('http://www.google.co.in');

// Execute the CURL
curl_exec($curl);

// Close the CURL handler
curl_close($curl);

?>

We initialize CURL by calling curl_init,  which returns the CURL resource. curl_exec function executes the curl and renders the content. You can configure the curl_exec to return the content as a string without rendering for further processing -  as demonstrated in the following example. Be sure that you has enabled CURL in PHP.INI file, or else it will result to a : Fatal error: Call to undefined function curl_init()

<?php

$curl=curl_init('http://www.google.co.in');

// Option to force content to be returned instead of render
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

// curl_exec will return the content from url
$data=curl_exec($curl);                      


/**
Do your progressing on "data"
*/

curl_close($curl);
?>

PHP libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols.

Read HTTP(S) Server Header

CURL lets you use or build a proxy server. For building a proxy server, you may need to read the headers coming from the server, and forward them to the browser with or without modification. 

The following example demonstrates the usage of proxy servers with CURL:

<?php
header('Content-Type: text/plain');

$curl=curl_init('http://www.google.co.in'); 

// Option to include the header along with body
curl_setopt($curl, CURLOPT_HEADER, 1);

curl_exec($curl);
curl_close($curl);

?>

You will see, raw text rendering, beginning with HTTP/1.1, and plain HTML data. The text in the beginning before <html> tag is HTTP header. The HTTP header and HTTP body are separated by double new lines. So you can use this feature of HTTP to build your own Proxy system.

How to Post Form Data

We are building a script that can submit forms automatically. This is useful if you want to automate your queries, like using Google search and searching for multiple keywords. You can manually input your keywords, but for bulk keywords, automating the process is more practical. You can just let your smart script run, and leave it as it is while it searches Google, or any other search engine that you might want to use, on its own. 

The following example demonstrates a simple program for form submission using PHP CURL

<?php

// Replace the URL with your URL
$curl=curl_init('http://example.com/examplepath');

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");

// array must be key/value pair of name/value in form.
curl_setopt($curl, CURLOPT_POSTFIELDS, array('name'=>'xyz', 'age'=>30));

curl_exec($curl);

curl_close($curl);