» Birthday Bundle - Over $400 worth of Envato files for just $20

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!

Get our RSS Feed!

Related Posts

One Reply to "PHP: Practical cURL function"

  1. [...] use cURL to connect to Delicious and get some information regarding the saved URL (including the total [...]

Leave a Reply


* = required fields

  (will not be published)


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Note: If you want to post CODE Snippets, please make them postable first!
(e.g. <br /> should be converted to &lt;br /&gt;)