Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
the hacking & security community

[ 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



?>




By RoMeO







Published: 10:24:40 24.04.2008 by commander



Source
http://www.darkmindz.com