next Posted November 27, 2012 Share Posted November 27, 2012 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', ); Quote Link to comment https://forums.phpfreaks.com/topic/271266-cant-figure-out-how-to-login-with-curl/ Share on other sites More sharing options...
premiso Posted November 27, 2012 Share Posted November 27, 2012 (edited) 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 November 27, 2012 by premiso Quote Link to comment https://forums.phpfreaks.com/topic/271266-cant-figure-out-how-to-login-with-curl/#findComment-1395681 Share on other sites More sharing options...
next Posted November 27, 2012 Author Share Posted November 27, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/271266-cant-figure-out-how-to-login-with-curl/#findComment-1395683 Share on other sites More sharing options...
next Posted November 27, 2012 Author Share Posted November 27, 2012 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/271266-cant-figure-out-how-to-login-with-curl/#findComment-1395689 Share on other sites More sharing options...
mrMarcus Posted November 27, 2012 Share Posted November 27, 2012 (edited) 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 November 27, 2012 by mrMarcus Quote Link to comment https://forums.phpfreaks.com/topic/271266-cant-figure-out-how-to-login-with-curl/#findComment-1395690 Share on other sites More sharing options...
next Posted November 27, 2012 Author Share Posted November 27, 2012 (edited) 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 November 27, 2012 by next Quote Link to comment https://forums.phpfreaks.com/topic/271266-cant-figure-out-how-to-login-with-curl/#findComment-1395696 Share on other sites More sharing options...
mrMarcus Posted November 27, 2012 Share Posted November 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271266-cant-figure-out-how-to-login-with-curl/#findComment-1395701 Share on other sites More sharing options...
next Posted November 27, 2012 Author Share Posted November 27, 2012 That page is accessible only by logged on users. I redirected curl to it after I passed login info. Quote Link to comment https://forums.phpfreaks.com/topic/271266-cant-figure-out-how-to-login-with-curl/#findComment-1395704 Share on other sites More sharing options...
Muddy_Funster Posted November 28, 2012 Share Posted November 28, 2012 you have 2 hidden input fields at the top, do they post information as well? Quote Link to comment https://forums.phpfreaks.com/topic/271266-cant-figure-out-how-to-login-with-curl/#findComment-1395834 Share on other sites More sharing options...
next Posted November 28, 2012 Author Share Posted November 28, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/271266-cant-figure-out-how-to-login-with-curl/#findComment-1395940 Share on other sites More sharing options...
Muddy_Funster Posted November 29, 2012 Share Posted November 29, 2012 nope, this is basicly telling the curl function that the sata being passed is sing the post method, rather than get Quote Link to comment https://forums.phpfreaks.com/topic/271266-cant-figure-out-how-to-login-with-curl/#findComment-1396227 Share on other sites More sharing options...
next Posted November 30, 2012 Author Share Posted November 30, 2012 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"? Quote Link to comment https://forums.phpfreaks.com/topic/271266-cant-figure-out-how-to-login-with-curl/#findComment-1396466 Share on other sites More sharing options...
Christian F. Posted December 1, 2012 Share Posted December 1, 2012 Yes it would, and the output buffer wouldn't end/clear until the PHP script dies or your met another (lone) ob_end/clean () call. Whomever wrote the code above clearly didn't know what, exactly, they were doing. Quote Link to comment https://forums.phpfreaks.com/topic/271266-cant-figure-out-how-to-login-with-curl/#findComment-1396613 Share on other sites More sharing options...
DavidAM Posted December 1, 2012 Share Posted December 1, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271266-cant-figure-out-how-to-login-with-curl/#findComment-1396679 Share on other sites More sharing options...
next Posted December 2, 2012 Author Share Posted December 2, 2012 Makes more sense now. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/271266-cant-figure-out-how-to-login-with-curl/#findComment-1396829 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.