Showing posts with label Curl with example. Show all posts
Showing posts with label Curl with example. 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);
?>

Popular Posts