DarkMindZ Articles

[ cURL ] Basics



When using cURL with PHP, you can automate alot of operations easily.

- Basic cURL:



<?php

    $ch 
curl_init('http://www.target.com'); // the target

    
curl_setopt($chCURLOPT_RETURNTRANSFER1); // return the page

    
$result curl_exec ($ch); // executing the cURL

    
curl_close ($ch); // Closing connection



echo $result;

?> 

 



that code would get the source code of target.com, and echo it.

- Post via cURL:



<?php



$data 
"field_name=field_value&submit_value=submit\";



    $ch = curl_init('http://www.target.com'); // the target

    curl_setopt ($ch, CURLOPT_POST, 1); // telling cURl to POST

    curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);

    curl_exec ($ch); // executing the cURL

    curl_close ($ch); // Closing connection



?> 





Simple posting via cURL.

- Using cookies with cURL:

( You will find this usefull when you are trying to do something that needs a login and a cookie stored )



<?php



    $ch 
curl_init();

    
curl_setopt($chCURLOPT_URL'http://www.target.com');

    
curl_setopt($chCURLOPT_FOLLOWLOCATION1);

    
curl_setopt($chCURLOPT_RETURNTRANSFER1);

    
curl_setopt($chCURLOPT_COOKIEJAR'/path/to/cookie.txt');

    
curl_setopt($chCURLOPT_COOKIEFILE'/path/to/cookie.txt');

    
$result curl_exec($ch);

    
curl_close($ch);



echo 
$result;



?>

 



ofcourse you might need to post first into the login form, to get the cookies stored, then you can do other things with you being logged in.

- Extra info:

you can set `user-agent`, `referrer`, `headers`.. using cURL:



<?php

// set user-agent to DarkMindZ

curl_setopt($chCURLOPT_USERAGENT'DarkMindZ');

?>

 





<?php

// set referrer darkmindz.com

 
curl_setopt($chCURLOPT_REFERER"http://www.darkmindz.com\");

?>




- Making life easier:

This function will help you alot in making things go easy:



<?php

function curl_it($method$target$post_var

{

    
$ch curl_init();

    
curl_setopt($chCURLOPT_URL$target);

    
curl_setopt($chCURLOPT_USERAGENT$_SERVER['HTTP_USER_AGENT']);

    
curl_setopt($chCURLOPT_FOLLOWLOCATION1);

    
curl_setopt($chCURLOPT_RETURNTRANSFER1);

    
curl_setopt($chCURLOPT_COOKIEJAR'/path/to/cookie.txt');

    
curl_setopt($chCURLOPT_COOKIEFILE'/path/to/cookie.txt');



    if (
$method == 'POST') {

        
curl_setopt($chCURLOPT_POST1);

        
curl_setopt($chCURLOPT_POSTFIELDS$post_var);

    }



    
$result curl_exec($ch);

    
curl_close($ch);

}



 
// usage:



curl_it('''http://www.darkmindz.com'); // get darkmindz.com homepage



curl_it('POST''http://www.darkmindz.com''user=dude&pass=dude2'); // login using dude:dude2



?>






Submitted by:  RoMeO

Share it:

Add to Google! Stumble It! Reddit! Facebook Digg!
Comment:
dugg
http://digg.com/programming/cURL_Basic_Tutorial_1

By: turbocharged_06


Comment:
nice curl tutorial .. wish i had logged in this morning exactly what i needed as a refresher covers lots of stuff that gets annoying to google.. all in one place.

By: Shadix


Comment:
Damn, cURL annoys me, if i use yours, copy and paste it it works fine.
But when i write it out, and try it, i get 301 Moved or something.
Damn it's annoying.

By: TOAO


Comment:
@TOAO

use CURLOPT_FOLLOWLOCATION set to 1 , your problem will be solved.

By: neotheonehacker