I am trying my first PHP CLI using cURL, and simply want to complete a login to an HTTPS site. The site HTML is simple:

<body>
    <div class='plain-header'>
      <img src='/images/logo.jpg' />
    </div>
    <div class='login-form'>
      <form method='post'>
        <table>
          <tr>
            <td class='fieldname'>Username:</td>
            <td>
              <input name='login' type='text' />
            </td>
          </tr>
          <tr>
            <td class='fieldname'>Password:</td>
            <td>
              <input name='password' type='password' />
            </td>
          </tr>
          <tr>
            <td></td>


            <td>
              <input type='submit' value='Login' />
            </td>
          </tr>
         </table>
      </form>
    </div>
  </body>

and I tried

<?php
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'https://mysite.com/'); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/BuiltinObjectToken=GoDaddyClass2CA.crt");
curl_setopt($ch, CURLOPT_POST, 1) ;
$login = 'mylogin';
$pwd =  'mypassword';
$postdata = "login=". $login ."&password=". $pwd; 
curl_setopt($ch,CURLOPT_POSTFIELDS,$postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


if(curl_exec($ch) === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
else
{
    echo 'Operation completed without any errors';
}


//$data=curl_exec($ch);
//echo $data;  

curl_close($ch);
?>

as is the script echoes back it completed without error, but if uncomment the //$data lines I receive

<body>
    <div class='plain-header'>
      <img src='/images/logo.jpg' />
    </div>
    <h1>There was an error. Sorry about that.</h1>
  </body>

The url, real login and password all work manually, and I don't really need the certificate info in order to open the url. I assume I am making a fundamental mistake in the code and would appreciate being corrected.

TIA

Are you absolutely sure that the fields are named login and password ? Also, most examples I've seen use an array for the post fields, not a query string.

Are you absolutely sure that the fields are named login and password ?

If you look at my post, that is what the HTML appears to say. I did try Username and Password but to no avail. I tried an array but the script just died - could you suggest the proper syntax for the array?

I scripted curl_setopt($ch, CURLOPT_HEADER, 1); and used an array to post:

$postdata = array('login' => urlencode('mylogin'), 'password' => urlencode('mypasword'));
curl_setopt($ch,CURLOPT_POSTFIELDS,$postdata);

which now give me error 500

HTTP/1.1 500 Internal Server Error
Date: Wed, 22 Jun 2011 18:53:40 GMT
Server: Apache/2.2.3 (CentOS)
X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 2.2.2
Set-Cookie: _session_id=257e67869a44b30ff86f4d9c03c3cf5b; path=/;
Status: 500
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8

still would like to know what I am doing wrong?

This would suffice, but unsure why the server is returning 500. I do not have a https server to test against, so cannot try myself. Hopefully someone else can help out.

$postdata = array('login' => 'mylogin', 'password' => 'mypassword');
Member Avatar for diafol

I assume this is a permanent situation, not intermittent (not some prob with network). cURL is enabled? You can check in phpinfo(). Sorry if you're already clued up with this.

I threw in every bell and whistle I read up on to get a simple login with cURL to work:

<?php
$ch = curl_init();
//curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');  
curl_setopt($ch, CURLOPT_URL, 'myurl'); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"; 
curl_setopt($ch, CURLOPT_USERAGENT, $useragent); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch,CURLOPT_AUTOREFERER,1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/BuiltinObjectToken=GoDaddyClass2CA.crt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1) ;
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
$postdata = array('login' => urlencode('mylogin'), 'password' => urlencode('mypassword'));

curl_setopt($ch,CURLOPT_POSTFIELDS,$postdata);
$data=curl_exec($ch);
$getInfo = curl_getinfo($ch); 
if(curl_exec($ch) === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
else
{
    echo 'Operation completed without any errors';
}
echo "\n";
echo $getInfo;
echo $data;  
curl_close($ch);
?>

and it still errors:

Operation completed without any errors
ArrayHTTP/1.1 100 Continue

HTTP/1.1 500 Internal Server Error
Date: Thu, 23 Jun 2011 12:44:41 GMT
Server: Apache/2.2.3 (CentOS)
X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 2.2.2
Set-Cookie: _session_id=396955cc5070f42e6218bb40eda20fcb; path=/;
Status: 500
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8

<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title></title>
    <link rel="stylesheet" href="/stylesheets/styles.css" type="text/css" />
  </head>
  <body>
    <div class='plain-header'>
      <img src='/images/logo.jpg' />
    </div>
    <h1>There was an error. Sorry about that.</h1>
  </body>
</html>

Note: if if use
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);

I will get an error that the certificate locations could not be verified, but I can't see this having anything to do with the script error. The site cannot be entered with IE, otherwise I would have used DHTML (has to do with the version of jscript Microsoft uses).

I tried workarounds with file_get_contents() which does not work with https, so I am still looking for the magic bullet.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.