Jump to content

Can't Figure Out How To Login With Curl


next

Recommended Posts

I'm trying to login to a website with curl, but can't figure out how it's supposed to be done.

Here's my code so far:

<?php
$url = 'http://fmpilot.com/Express/Desk/Login.asp';
$login_data = array(
 'txtUserid' => 'my_user_name',
 'txtPassword' => 'my_password_',
);
$ch = curl_init();

curl_setopt_array($ch, array(
 CURLOPT_URL => $url,
 CURLOPT_USERAGENT => 'Mozilla/curl',
 CURLOPT_HEADER => false,
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_TIMEOUT => 10,
 CURLOPT_POST => true,
 CURLOPT_POSTFIELDS => $login_data,
 CURLOPT_COOKIEFILE => 'cookie.txt',
 CURLOPT_COOKIEJAR => 'cookie.txt',
 CURLOPT_FOLLOWLOCATION => true
 ));

echo curl_exec($ch);
curl_close($ch);

//<input class="formfield" name="txtUserid" maxlength="50" value="" type="text">
//<input class="formfield" name="txtPassword" maxlength="50" type="password">
?>

 

How does

 CURLOPT_POSTFIELDS => $login_data,

work? Did I do it correct here? "form field name" => "user input data" format?

$login_data = array(
 'txtUserid' => 'my_user_name',
 'txtPassword' => 'my_password',
);

Link to comment
Share on other sites

Try using the postfields outside the array. 

 

curl_setopt_array($ch, array(
  CURLOPT_URL => $url,
  CURLOPT_USERAGENT => 'Mozilla/curl',
  CURLOPT_HEADER => false,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 10,
  CURLOPT_POST => true,
  CURLOPT_COOKIEFILE => 'cookie.txt',
  CURLOPT_COOKIEJAR => 'cookie.txt',
  CURLOPT_FOLLOWLOCATION => true
  ));
curl_setopt($ch, CURLOPT_POSTFIELDS, $login_data);

 

And see where that gets ya. 

Edited by premiso
Link to comment
Share on other sites

Same thing. It just diplays the same login page and I don't see a cookie.txt created in the script directory.

 

Does CURL just submits the form by the use of this option: CURLOPT_POST => true ?

I don't need to click the submit button or anything of sort?

Link to comment
Share on other sites

Actually that did make a difference, before it was giving me BAD REQUEST error.

I slightly modified the code to go to the next page that only logged in users can see and it tells me:

Your session has expired.

Please log in again.

 

<?php
$url = 'http://fmpilot.com/Express/Desk/Login.asp';
$login_data = array(
 'txtUserid' => 'my_user_id_info',
 'txtPassword' => 'my_password_info',
);

   $fp = fopen("cookie.txt", "w");
   fclose($fp);

$ch = curl_init();

curl_setopt_array($ch, array(
 CURLOPT_URL => $url,
 CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT'],
 CURLOPT_HEADER => false,
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_TIMEOUT => 10,
 CURLOPT_POST => true,
 CURLOPT_COOKIEFILE => 'cookie.txt',
 CURLOPT_COOKIEJAR => 'cookie.txt',
 CURLOPT_FOLLOWLOCATION => true
 ));

curl_setopt($ch, CURLOPT_POSTFIELDS, $login_data);
curl_exec($ch);

curl_setopt($ch, CURLOPT_URL, 'http://fmpilot.com/Express/Desk/Reports/ProviderReports.asp');
echo curl_exec($ch);
curl_close($ch);
?>

Link to comment
Share on other sites

Few things...

 

Are you positive the login credentials are correct? Does the login processing script require a button to be pushed/additional parameter to be sent? E.g.

 

// standard form processing (in PHP)

if (isset($_POST['submit'])) {
// form has been submitted; do stuff
}

 

If so, you'll need to pass that along in your $login_data array.

 

You do not appear to be using SSL for processing (that I can see from the action URL anyway), and cURL has CURLOPT_SSL_VERIFYPEER set to TRUE by default. What that does is attempts to verify the server certificate on connection. Set this to false:

 

CURLOPT_SSL_VERIFYPEER => FALSE

Edited by mrMarcus
Link to comment
Share on other sites

Credentials are definitely correct. I set the SSL option as you suggested, but that didn't help. I'm not really sure regarding what data it needs. Here's the form html code

<input name="hReturnUrl" value="/Express/ServiceDesk.asp" type="hidden">
<input name="timezoneoffset" value="300" type="hidden">
<table border="0" cellpadding="0" cellspacing="0">
 <tbody><tr>
 <td colspan="2" height="10"><img src="/Express/Desk/images/transpix.gif" height="10"></td></tr>
 <tr>
 <td colspan="2" class="errmsg">Please enter a user name and password.</td></tr>
 <tr>
 <td colspan="2" height="10"><img src="/Express/Desk/images/transpix.gif" height="10"></td></tr>
 <tr>
 <td class="formheader" align="left" nowrap="" width="100"><a class="FormHeader" href="" style="text-decoration:none" onmouseout="javascript:clearInfo();" onmouseover="javascript:writeInfo('Enter your<BR>user name.');">User Name:</a>  </td>
 <td class="Content" align="left" width="100%"><input class="formfield" name="txtUserid" maxlength="50" value="" type="text"></td>
 </tr>
 <tr>
 <td class="formheader" align="left" width="100"><a class="FormHeader" href="" style="text-decoration:none" onmouseout="javascript:clearInfo();" onmouseover="javascript:writeInfo('Enter your<BR>password.');">Password:</a>  </td>
 <td class="Content" align="left" width="100%"><input class="formfield" name="txtPassword" maxlength="50" type="password"></td>
 </tr>
 <tr>
 <td> </td>
 <td align="left"><br>
 <a href="javascript:document.frmServiceDesk.submit();"><img src="/Express/Desk/images/Max5Style/btn_login.gif" border="0"></a>
 <a href="javascript:document.frmServiceDesk.reset();"><img src="/Express/Desk/images/Max5Style/btn_clear.gif" border="0"></a>
 </td>
 </tr>
 <tr>
 <td colspan="2"><img src="/Express/Desk/images/transpix.gif" height="15"></td></tr>
 <tr>
 <td> </td>
 <td align="left"><a href="/Express/Desk/RetPass.asp"><img src="/Express/Desk/images/Max5Style/btn_retrievepassword.gif" border="0"></a></td>
 </tr>

 <tr>
 <td colspan="2" height="10"><img src="/Express/Desk/images/transpix.gif" height="10"></td></tr>
 <tr>
 <td colspan="2"><font class="ErrMsg">This web site is optimized for Internet Explorer 6.0 and above.
 Please note this web site may not operate correctly with other browsers.</font></td>
 </tr>

 <tr>
 <td colspan="2" height="10"><img src="/Express/Desk/images/transpix.gif" height="10"></td></tr>
</tbody></table>

 

This is what submits the form:

javascript:document.frmServiceDesk.submit()

Edited by next
Link to comment
Share on other sites

At this point, for all I know, there is something in the processing script that is not allowing this.

 

And what is the reasoning behind the following?

 

curl_setopt($ch, CURLOPT_URL, 'http://fmpilot.com/Express/Desk/Reports/ProviderReports.asp');
echo curl_exec($ch);

 

At the end of your code.

Link to comment
Share on other sites

I don't have access to how that website works on the backend.

I interact with it on regular basis and was hoping to automate a bunch of things.

 

So what causes the data to post when using curl?

CURLOPT_POST => true

is this basically a submit button action?

Link to comment
Share on other sites

ok so changing the way I passed login data fixed my problem:

$login_data = 'txtUserid=my_username&txtPassword=my_password';

 

I'm examining a function that I found online:

function login($url,$data) {
   $fp = fopen("cookie.txt", "w");
   fclose($fp);
   $login = curl_init();
   curl_setopt($login, CURLOPT_COOKIEJAR, "cookie.txt");
   curl_setopt($login, CURLOPT_COOKIEFILE, "cookie.txt");
   curl_setopt($login, CURLOPT_TIMEOUT, 40000);
   curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE);
   curl_setopt($login, CURLOPT_URL, $url);
   curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
   curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
   curl_setopt($login, CURLOPT_POST, TRUE);
   curl_setopt($login, CURLOPT_POSTFIELDS, $data);

ob_start();
   return curl_exec ($login);

ob_end_clean();
   curl_close ($login);
unset($login);
}		

 

Is the function written wrong or does "return" not end functions, and they go until the closing brace?

Like in this part

ob_start();
   return curl_exec ($login);

ob_end_clean();
   curl_close ($login);
unset($login);

wouldn't the function stop and return value at "return"?

Link to comment
Share on other sites

Furthermore, there is no need for the ob*() calls in that code. The RETURNTRANSFER option of curl will return to content and nothing will be output, except possibly errors. The end of that function should be something like this:

 

 $data = curl_exec ($login);
 curl_close ($login);
 unset($login);
 return $data;
}

 

Although the unset() is completely unnecessary, since the variable immediately falls out of scope.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.