Search code examples
phpcurlphp-curllogin-page

How to login on Discogs.com using PHP curl (or other)?


I'm trying to login on website http://www.discogs.com using PHP script and cURL.

I need that to get data from https://www.discogs.com/sell/mywants?ev=wsim - it's a list of offers with items added to want-list. From what I found this list is not available in API.

I have problem with logging in using standard cURL request. Here's my code:

$username = 'myuser';
$password = 'mypassword';
$loginUrl = 'https://www.discogs.com/login';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.urlencode($username).'&password='.urlencode($password));
curl_setopt($ch, CURLOPT_COOKIEJAR, 'c:\cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'c:\tmp.txt'); 
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);

if (curl_error($ch)) {
    echo curl_error($ch);
    return;
} else {
    echo "OK";
}

curl_setopt($ch, CURLOPT_URL, 'https://www.discogs.com/sell/mywants?ev=wsim');
$content = curl_exec($ch);

echo $content;

curl_close($ch);

As a result I get login page instead of page with listed offers.

Could you tell me what am I doing wrong?


Solution

  • As I tested, the login page requires posting another field, which is Action.Login in order to make the login request valid.

    curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.urlencode($username).'&password='.urlencode($password).'&Action.Login=');
    

    Hope this helps.