Showing posts with label CURL. Show all posts
Showing posts with label CURL. Show all posts

Tuesday, September 9, 2008

Write Site content into a file using CURL

/*
This CURL program is for opening a given URL page
and write the content into a file

Example :
URL ==> http://google.com
file ==> testing.txt
*/

/* Site URL */
$url = "http://google.com";

/* Write the Content into a file */
$filename = 'testing.txt';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$somecontent = curl_exec ($ch);
$xml = curl_exec ($ch);
curl_close ($ch);

// In our example we're opening $filename in write mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}

// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

echo "Success, wrote ($somecontent) to file ($filename)";

fclose($handle);
?>

Posting data using CURL


/* Simple example to Login into a Site using
1. URL of that site
2. Actual field name of username and password (depands upon your situation)
3. Valid username and password
*/

/* Configuration Starts */
$site_url = "http://192.168.1.101/phpdocs/oriental/index.php?Action=user_login";
$query_string = "user_name=raja123&user_pass=raja123";
/* Configuration Ends */

$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_COOKIEFILE, "cookiefile");
curl_setopt($curl, CURLOPT_COOKIEJAR, "cookiefile"); # SAME cookiefile

curl_setopt($curl, CURLOPT_URL, $site_url); # this is where you first time connect - GET method authorization in my case, if you have POST - need to edit code a bit
$xxx = curl_exec($curl);

curl_setopt($curl, CURLOPT_URL, $site_url); # this is where you are requesting POST-method form results (working with secure connection using cookies after auth)
curl_setopt($curl, CURLOPT_POSTFIELDS, $query_string); # form params that'll be used to get form results
$return_content = curl_exec($curl);

curl_close ($curl);
echo $return_content;
?>

Get Site Content using CURL

// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://evergreenphp.blogspot.com/search/label/CURL");
curl_setopt($ch, CURLOPT_HEADER, false);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>

Monitor a Site using CURL

/*
This program for the Monitoring our site status..

AZAds is a traffic exchange that we wrote.
We wanted to come up with a way to check the UP or DOWN status of a submitted URL.
Here is the solution we came up with. Can also be used for a site monitoring service.
*/

$uServer = 'http://evergreenphp.blogspot.com/';
@$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$uServer");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
@curl_exec($ch);
$errnum=curl_errno($ch);
@curl_close($ch);
if($errnum != "0") {
print "Remote Site Status: DOWN";
} else {
print "Remote Site Status: UP";
}
?>

CURL with HTTP Authentication

// HTTP authentication
$site_url = "http://evergreenphp.com";
$ftp_username = "onetwothree";
$ftp_password = "threetwoone";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $site_url);
curl_setopt($ch, CURLOPT_USERPWD, $ftp_username.":".$ftp_password);
$result = curl_exec($ch);
print_r(curl_getinfo($ch));
echo "\n\ncURL error number:" .curl_errno($ch);
echo "\n\ncURL error:" . curl_error($ch);
// ...close cURL handle ($ch) below
curl_close($ch);
?>

Introduction to CURL?

What is PHP/CURL?

CURL is the name of the project. The name is a play on 'Client for URLs',
originally with URL spelled in uppercase to make it obvious it deals with
URLs. The module for PHP that makes it possible for PHP programs to access curl-
functions from within PHP.

We can use several web protocols using one uniform interface (CURL), most notably FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, and LDAP.

In March of 2001, the Curl Corporation launched the Curl Language, their Run
Time Environment (RTE), and their Integrated Development Environment (IDE).

A very powerful feature of the Curl Language is its Just-In-Time compiling.
Curl code is sent over the Web as source code. Curl’s compiler, which ships as part
of the runtime environment, sits on the client computer and compiles the source
code directly to the client’s low-level code. The goal is to minimize the load on the
servers and to capitalize on the end-user’s machine’s processing capability, rather
than their relatively slow communication rate.

TIP: If you are using fopen and fread to read HTTP or FTP or Remote Files, and experiencing some performance
issues such as stalling, slowing down and otherwise, then it's time you learned a thing called cURL.

Performance Comparison:

10 per minute for fopen/fread for 100 HTTP files
2000 per minute for cURL for 2000 HTTP files

cURL should be used for opening HTTP and FTP files, it is EXTREMELY reliable, even when it comes to performance.

Reference sites :

1. What is cURL, libcurl, PHP/CURL?
2. PHP/CURL Examples Collection

Popular Posts