TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Advertisement
cURL Basics
   When using cURL with PHP, you can automate a lot of operations easily.

- Basic cURL:

PHP Code:
$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 Code:

$data 
'field_name=field_value&submit_value=submit';
$ch curl_init('http://www.target.com'); // the target
curl_setopt($chCURLOPT_POST1); // telling cURl to POST
curl_setopt($chCURLOPT_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 useful when you are trying to do something that needs a login and a cookie stored )

PHP Code:
$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
Of course 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 Code:
// set user-agent to DarkMindZ
curl_setopt($chCURLOPT_USERAGENT'DarkMindZ'); 
PHP Code:
// set referrer darkmindz.com
curl_setopt($chCURLOPT_REFERER'http://www.darkmindz.com'); 
- Making life easier:

This function will help you a lot in making things go easy:

PHP Code:
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 
Report this Article
Last 5 Article Reviews Read All Reviews
   .....
Review added by Salathe on 04-28-2008
@Kalle I've amended that particular code snippet. There's nothing worse than providing sample code with errors!
   ....
Review added by Kalle on 04-24-2008
You have an syntax error in this code because your escaping the double quote sign:

curl_setopt($ch, CURLOPT_REFERER, "http://www.darkmindz.com\");
   .....
Review added by Salathe on 04-24-2008
@Aaron - very good questions and perhaps ones that should be asked on the forum! ;)
   .....
Review added by Aaron on 04-24-2008
It was a good how-to, but it wasn't really an article. What are the advantages of using CURL over something like the cookie superglobal? What was CURL made for? What problems does it solve? What problems does it have?

All times are GMT. The time now is 09:19 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0