View Single Post
  #1  
Old 21st October 2012, 08:25
firefly's Avatar
firefly firefly is offline
Senior Member
 
Join Date: Dec 2009
P2P
Posts: 74
Default cURL and libcurl Login and post data
Hi there,

I needed a script to login and post data so I put together a script with forms
All html attributes are defined with variables but can moved to the form section.

Update.php

HTML Code:
<?php



////////////////////////////////////////////////////////
// COOKIE JAR
////////////////////////////////////////////////////////

//cookie path
$cookieFile = "/home/user/www/cookie.txt";

/////////////////////////////////////////////////////////
//LOGIN SECTION
/////////////////////////////////////////////////////////

//login Variable details
// This is what is entered into your login form.
$loginurl = "http://siteurl.com/takelogin.php";
$username = "XXXXX";
$password = "XXXX";
$submitvar = "login";

//LOGIN VARIABLES ATTIBUTES DETAILS
// this is the name of the attribute for the login, password and submit button.

$loginatt1 = "username";
$loginatt2 = "password";
$submitatt = "submit";

///////////////////////////////////////////////////////////////
// POST DATA SECTION
///////////////////////////////////////////////////////////////
//this is what you can enter in according to form 
//form Variable
$id = $_POST["id"];
$user = $_POST["usern"];
$title = $_POST["tit"];
$mes = $_POST["mes"];

//hard coded form Variable
$submitvar = "Submit";

//post Variable attributes details
// this is the name of the attributes you want to post in

/// You can find the name by looking for the name of the form

//eg 

//Last name: <input type="text" name="lastname">

// the name you will use is lastname  

//attributes
$postatt1 = "username";
$postatt2 = "subject";
$postatt3 = "msg";
$postsub  = "send it!";




////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
//Post if done
$done = true;



$headers = array(
    "Accept-Language: en-us",
    "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)",
    "Connection: Keep-Alive",
    "Cache-Control: no-cache"
    );
    

$get = curl_init($loginurl);

curl_setopt($get, CURLOPT_HTTPHEADER, $headers); // this pretends this scraper to be browser client IE6 on Windows XP, of course you can pretend to be other browsers just you have to know the correct headers

curl_setopt($get, CURLOPT_REFERER, $id); // lie to the server that we are some visitor who arrived here through google search

// curl_setopt($get, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // this one's a plus, it specifically sets User-Agent in the headers. As we already have set that in $headers, we don't need it this time



/////////////////////////////////////////////////////////////
//END
///////////////////////////////////////////////////////////


// Login
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $loginurl);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_POST, true);
$postVars = array($loginatt1 => $username,
                  $loginatt2 => $password,
                  $submitatt => $submitvar);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postVars);
$resp = curl_exec($curl);
curl_close($curl);

// Parse sid from cookie file
//preg_match('/phpbb2mysql_sid\t(.*)/', file_get_contents($cookieFile), $match);
//$sId = $match[1];

// Post
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $id);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_POST, true);
$postVars = array( $postatt1 => $user,
                   $postatt2 => $title,
                   $postatt3 => $mes,
                   $postsub  => $submitvar);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postVars);
$resp = curl_exec($curl);
if ($done) {
echo "All done.";
}
curl_close($curl);

//echo $resp;
echo $get;

?>
index.php

HTML Code:
<html>
<head>
<title>Post</title>
<style type="text/css">
<!--
.formbox {
    width: 190px;
    margin-right: auto;
    margin-left: auto;
}
.submit {
    width: 180px;
    margin-top: 5px;
}
.formbox .formcont input {
    width: 180px;
}
.formbox .formcont {
}
-->
</style>
</head>
<body>
<?

?>
<div class="formbox">
<form method="post" action="update.php">
<div class="formcont">
<!--- This is where you add the full link where you want to post--->
  <input name="id" type="text" value="url where you want to post to" size="12" maxlength="300">
  <br /></div>
<div class="formcont">
  <input name="usern" type="text" value="Username" size="12" maxlength="12">
  <br /></div>
<div class="formcont">
  <input name="tit" type="text" value="Title" size="12" maxlength="36">
  <br /></div>
<textarea rows="5" cols="20" name="mes" wrap="physical">enter your message!</textarea>

<input name="submit" type="submit" class="submit" value="submit">
</form> 
</div>
<?

?>

Last edited by firefly; 21st October 2012 at 08:37.
Reply With Quote