General purpose function for curl

Hi there,

I was working on a project which included a web scrapper to grab data from a site. For that I had to use curl,  but the problem was the site was checking for cookies on some pages so I have made a function which can serve my application and set cookies when ever required. I have found it on some where and modified to serve my purpose.


*@param: $url  is the URL which will be called

*@param: $params (array)the parameters which will be passed by get method

*@param: $is_coockie_set whether cookies should be set or not


public function curl($url,$params = array(),$is_coockie_set = false)

 /* create a cookie file */
 $ckfile = tempnam ("/tmp", "CURLCOOKIE");

/*  visit the homepage to set the cookie properly */
 $ch = curl_init ($url);
 curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
 $output = curl_exec ($ch);

$str = ''; $str_arr= array();
 foreach($params as $key => $value)
 $str_arr[] = "$key=".urlencode($value);
 $str = '?'.implode('&',$str_arr);
 $ch = curl_init ($url.$str);
 curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

return  $output = curl_exec ($ch);



One comment on “General purpose function for curl

  1. Thanks for sharing your info. I really appreciate your efforts
    and I am waiting for your further post thanks once again.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s