2 Replies - 6614 Views - Last Post: 14 March 2011 - 04:44 AM Rate Topic: -----

#1 sirdavethebrave  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-March 11

cURL and viewstate problem

Posted 09 March 2011 - 03:57 PM

Hi guys - first time post obviously, hoping someone can help with with a cURL and viewstate problem.

I've been playing around with cURL, logging into sites I have developed myself and scraping the contents purely to get to grips with everything cURL has to offer. I have run into difficulties however, when I have tried to log into a site developed in .net.

I know that my problem lies in the viewstate aspect of aspx pages but I just don't know how I can go about getting round it. I've been trying to log in to pinnaclesports.com and scrape the odds for English Premier League football games for a little app I am developing for myself. If I log into the site manually I can browse to the page I want, save it to my hard drive and parse the resulting html as I see fit, but I would like to do all this on the fly. However I keep failing!

Here is what I understand -

I have used LiveHTTPHeaders in firefox to establish the url I need and the data that must be submitted. What I'm not entirely sure about is how to interpret this data so I can send the correct information back to the page to allow me to login and do what I want...

Running WAMP on Windows Vista here is my code:

<?php 
function curl_login($url, $data, $proxy, $proxystatus) {
	$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_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
    curl_setopt($login, CURLOPT_TIMEOUT, 40);
    curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE);
    if ($proxystatus == 'on') {
        curl_setopt($login, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($login, CURLOPT_HTTPPROXYTUNNEL, TRUE);
        curl_setopt($login, CURLOPT_PROXY, $proxy);
    }
    curl_setopt($login, CURLOPT_URL, $url);
    curl_setopt($login, CURLOPT_HEADER, TRUE);
    curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($login, CURLOPT_POST, TRUE);
   
    ob_start();      // prevent any output
    return curl_exec ($login); // execute the curl command
    ob_end_clean();  // stop preventing output
    curl_close ($login);
    unset($login);  
	}
	
$url = 'https://www.pinnaclesports.com/Secure/LoginPage.aspx?destination=sports';
$data = '__EVENTTARGET=ctl00%24LF%24LB&__EVENTARGUMENT=&__LASTFOCUS=&
__VIEWSTATE=%2FwEPDwULLTE1MTczNzI3NjUPZBYCZg9kFgQCAQ9kFgQCBA8WAh4EVGV4dAVUDQo8bWV0YSBuYW1lPSJ2ZXJpZnktdjEiIGNvbnRlbnQ9InJvT3JjOUljZHlZVFgrVDZnWm1PN2VQejMwRWNLR2VXTkJqbmwzWnlaQUk9IiAvPg0KZAIKD2QWAmYPFgIfAAWJATxzdHlsZSB0eXBlPSJ0ZXh0L2NzcyI%2BKi5pIHtiYWNrZ3JvdW5kLWltYWdlOnVybChodHRwczovL2NvbnRlbnQucGlubmFjbGVzcG9ydHMuY29tL3VwbG9hZGVkSW1hZ2VzL0d1ZXN0U2VjdGlvbi9jb21iaW5lZDIzLnBuZyk7fTwvc3R5bGU%2BZAIDDxYEHgZvbmxvYWQFMmphdmFzY3JpcHQ6UDdfaW5pdFBNKDEsOCwwLC0yMCwyKTtQN19pbml0VFAoNCwgMCk7HghvbnVubG9hZAUWamF2YXNjcmlwdDpvblVubG9hZCgpOxYCAgEPZBYEAgMPZBYCAgEPZBYGZg8QZGQWAWZkAgEPEGRkFgFmZAICDxAPFgIeB1Zpc2libGVoZGQWAQICZAIHD2QWBAIFDxYCHwNoZAIHDxYCHwNoZGQ%3D
&__PREVIOUSPAGE=cC6v86rLgpzKauPW5R75VZ6MnouJHAk-4dX5bAgGIAb5u7LxzsDThRsxab3Nvnz3jfDBRuieaF3sFEN_3HIHC-n835N01&ctl00%24LDDL=1
&ctl00%24PSDDL=american
&ctl00%24MCPH%24LF%24UserName=******
&ctl00%24MCPH%24LF%24Password=*****&ctl00%24MCPH%24LF%24LanguageID=0
&ctl00%24MCPH%24LF%24PriceStyle=&ctl00%24MCPH%24LF%24LinesTypeView=c
&ctl00%24MCPH%24LF%24MemberServer=www31.pinnaclesports.com';


function curl_get_page($site, $proxy, $proxystatus) {
	$ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    if ($proxystatus == 'on') {
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
    }
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_URL, $site);
    ob_start();      // prevent any output
    return curl_exec ($ch); // execute the curl command
    ob_end_clean();  // stop preventing output
    curl_close ($ch);
	}
	
echo curl_login($url,$data,'','off');
echo curl_get_page('https://www17.pinnaclesports.com/members/betpage.asp','','off');
//curl_login($url,$data,'','off');
//curl_get_page($url, '','off');
?>

I understand it's always site specific, but does my problem lie somewhere in the $data variable?
I hope someone can help as this has been driving me mad for weeks!
If I have got something fundamentally wrong, perhaps someone can point me in the right direction.

Thanks in advance.

DS

This post has been edited by sirdavethebrave: 09 March 2011 - 04:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: cURL and viewstate problem

#2 sirdavethebrave  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-March 11

Re: cURL and viewstate problem

Posted 14 March 2011 - 04:21 AM

Nobody attempted using cURL with viewstate?
Would love some help with this if anyone can offer anything at all.

Cheers again.

DS
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: cURL and viewstate problem

Posted 14 March 2011 - 04:44 AM

Where are you using the $data value in your curl_login function, and be aware that you need to URL-encode that value before you send it: http://www.vaibhavbo...with-viewstate/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1