PHP: Practical cURL function
Posted on September 2, 2008, Filled under PHP,
Bookmark it
A common cURL function that can be used for multiple purposes:
<?php
function LoadCURLPage($url, $agent = '', $cookie = "", $referer = "",
$post_fields = "", $return_transfer = 1, $follow_location = 1)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
if($agent)
{
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
}
if($post_fields)
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
if($referer)
{
curl_setopt($ch, CURLOPT_REFERER, $referer);
}
if($cookie)
{
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
}
$result = curl_exec ($ch);
curl_close ($ch);
return $result;
}
### Usage Examples ####
/* LOGIN (POST Method) */
$login_url = 'http://www.domain.com/login';
// Login
$post_info = array("username" => "username_here",
"password" => "password_here");
$post_data = '';
foreach($post_info as $name => $value)
{
$post_data .= $name.'='.$value.'&';
}
$post_data = trim(substr($post_data, 0, -1));
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4)
Gecko/20030624 Netscape/7.1 (ax)";
$cookie_file_path = 'temp/cookie.txt';
$result = LoadCURLPage($login_url, $agent, $cookie_file_path,
$login_page, $post_data);
echo $result;
/* Fetch a web page (GET Method) */
$result = LoadCURLPage('http://www.yahoo.com/', $agent);
echo $result;
?>
Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!
- September 2, 2008
- article by Gabriel C.
- 1 comment
Related Posts
Create a PHP Script that Logins in to a Password Protected Areaat December 17, 2008 with 14 comments
PHP: Creating a simple web data (spider) extractorat September 14, 2008 with 10 comments
PHP: How to Download a Remote Image using GD or cURLat January 5, 2009 with 19 comments
Creating the array_isearch() Functionat November 8, 2008
PHP: Equivalent of trim() Function for Arraysat October 5, 2008 with 2 comments

One Reply to "PHP: Practical cURL function"
September 7, 2009 at 1:57 AM
[...] use cURL to connect to Delicious and get some information regarding the saved URL (including the total [...]