php curl log in
i'm quite new to curl, i need to log in to a certain page every hour. i had a curl code that would submit the details and log me in fine using
markupcurl_setopt ($ch, CURLOPT_POSTFIELDS, 'form1=username&form2=stuff.....');
the webiste has about 4 forms that need to be filled in to submit. but 3 of them are hidden and dont really do anything
i realised this morning that the 3 hidden form names & some of their values needed to log in are changed every 24 hours, probably to stop exactly what i want to do.
so instead of me sending the form names are values myself (because they are always changing) i imagine there would be a simpler code to just go to the webpage, enter in 1 form, click submit, and let the other 3 hidden ones stay as per normal.
any ideas? or help? cheers
Here it goes. It might be a bit confusing, but that's because it uses specifics of the website I coded it for. For example, to log in I have to get some hidden values and then log in, which is I guess what you are trying to do. If you have any questions, ask.
//Predefined variables
$server="http://s7.travian.us";
$userlogin="user";
$passlogin="pass";
//Get login page source
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $server."/login.php");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);$loginsource = curl_exec($curl);
curl_close($curl);
//Parse login page source for input names
$text=strstr($loginsource,'<input type="hidden" name="login" value="');
$text=substr($text,strlen('<input type="hidden" name="login" value="'));
$placel=strpos($text,'"');
$login=substr($text, 0, $placel);
$text=strstr($text,'<input class="fm fm110" type="text" name="');
$text=substr($text,strlen('<input class="fm fm110" type="text" name="'));
$placeun=strpos($text,'"');
$username=substr($text, 0, $placeun);
$text=strstr($text,'<input class="fm fm110" type="password" name="');
$text=substr($text,strlen('<input class="fm fm110" type="password" name="'));
$placeup=strpos($text,'"');
$userpassword=substr($text, 0, $placeup);
$text=strstr($text,'<input type="hidden" name="');
$text=substr($text,strlen('<input type="hidden" name="'));
$placeo=strpos($text,'"');
$other=substr($text, 0, $placeo);
$text=strstr($text,'value="');
$text=substr($text,strlen('value="'));
$placeo2=strpos($text,'"');
$other2=substr($text, 0, $placeo2);
//Send post variables
$curl2 = curl_init();
curl_setopt($curl2, CURLOPT_URL, $server."/dorf1.php");
curl_setopt($curl2, CURLOPT_POST, 1);
curl_setopt($curl2, CURLOPT_POSTFIELDS, "w=&login=".$login."&".$username."=".$userlogin."&".$userpassword."=".$passlogin."&".$other."=".$other2."&s1=login");
curl_setopt($curl2, CURLOPT_HEADER, 1);
curl_setopt($curl2, CURLOPT_RETURNTRANSFER, 1);
$result2 = curl_exec($curl2);
curl_close($curl2);
$text2=strstr($result2,'Set-Cookie:');
$text2=substr($text2,strlen('Set-Cookie:'));
$placec=strpos($text2,';');
$thecookie=substr($text2, 0, $placec);
echo "<br><br>Resource Production:
<br><br>
Wood: ".$prodwood." per hour
<br>Clay: ".$prodclay." per hour
<br>Iron: ".$prodiron." per hour
<br>Wheat: ".$prodwheat." per hour<br><br>";
//Use cookie to get source of dorf2.php
$curl4 = curl_init();
curl_setopt($curl4, CURLOPT_URL, $server."/dorf2.php");
curl_setopt($curl4, CURLOPT_COOKIE,$thecookie);
curl_setopt($curl4, CURLOPT_RETURNTRANSFER, 1);
$result4 = curl_exec($curl4);
curl_close($curl4);
//Use sources to get building location and level
$text3=$result3;
for($buildnum=1;$buildnum<=18;$buildnum++)
{
$text3=strstr($text3,'shape="circle" title="');
$text3=substr($text3,strlen('shape="circle" title="'));
$building[$buildnum]=$buildnum."_".
substr($text3, 0, strpos($text3,' level'))."_".
substr($text3, strpos($text3,' level ')+strlen(' level '), strpos($text3,'"><area ')-strpos($text3,' level ')-strlen(' level '));
//$text3=strstr($text3,'level'); //Moves forward for next resource field to catch
echo $building[$buildnum]."<br>";
}
for($buildnum=19;$buildnum<=40;$buildnum++)
{
$text4=strstr($result4,'<area href="build.php?id='.$buildnum.'" title="');
$text4=substr($text4,strlen('<area href="build.php?id='.$buildnum.'" title="'));
$building[$buildnum]=$buildnum."_";
if(strpos(substr($text4, 0, strpos($text4,'"')),' level')===FALSE)
{
$building[$buildnum]=$building[$buildnum].'Building site_0';
}
else
{
$building[$buildnum]=$building[$buildnum].substr($text4, 0, strpos($text4,' level'))."_".
substr($text4, strpos($text4,' level ')+strlen(' level '), strpos($text4,'" coords="')-strpos($text4,' level ')-strlen(' level '));
}
echo $building[$buildnum]."<br>";
}
?>```