nielsv
nielsv

Reputation: 6800

Trying to get data from aspx page after post + PHP

I want to get the data from the following page : http://kovv.mavari.be/kalender.aspx when you press on the submit button and the dropdownlists have no selected values. (So the page where you see a big table)

I've tried to follow a tutorial you can find here: http://www.mishainthecloud.com/2009/12/screen-scraping-aspnet-application-in.html.

This is what I have so far:

public function teamsoostVlaanderen()
{

    $url = "http://kovv.mavari.be/kalender.aspx";
    $regs=array();

    $cookies = '../src/VolleyScout/VolleyScoutBundle/Resources/doc/cookie.txt';

    // regular expressions to parse out the special ASP.NET
    // values for __VIEWSTATE and __EVENTVALIDATION
    $regexViewstate = '/__VIEWSTATE\" value=\"(.*)\"/i';
    $regexEventVal  = '/__EVENTVALIDATION\" value=\"(.*)\"/i';

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    $data=curl_exec($ch);

    $viewstate = $this->regexExtract($data,$regexViewstate,$regs,1);
    $eventval = $this->regexExtract($data, $regexEventVal,$regs,1);

    $postData = '__VIEWSTATE='.rawurlencode($viewstate)
        .'&__EVENTVALIDATION='.rawurlencode($eventval)
        .'&ctl00_ContentPlaceHolder1_ddlGeslacht'
        .'&ctl00$ContentPlaceHolder1$ddlReeks'
        .'&ctl00_ContentPlaceHolder1_ddlDatum'
        .'&ctl00$ContentPlaceHolder1$btnZoek:zoek'
    ;

    curl_setOpt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);

    $data = curl_exec($ch);

    echo $data;

    curl_close($ch);

    die();
}

public function regexExtract($text, $regex, $regs, $nthValue)
{
    if (preg_match($regex, $text, $regs)) {
        $result = $regs[$nthValue];
    }
    else {
        $result = "";
    }
    return $result;
}

But I still get the page without a post (so not with the table). When I check my cookies.txt file it's empty, maybe there's the problem? Can somebody help me find the problem?

Upvotes: 0

Views: 242

Answers (1)

Sabuj Hassan
Sabuj Hassan

Reputation: 39355

Appropriate regex:

$regexViewstate = '/__VIEWSTATE\" value=\"([^"]*)\"/i';
$regexEventVal  = '/__EVENTVALIDATION\" value=\"([^"]*)\"/i';

And missing the equal sign from your post parameters:

$postData = '__VIEWSTATE='.rawurlencode($viewstate)
    .'&__EVENTVALIDATION='.rawurlencode($eventval)
    .'&ctl00_ContentPlaceHolder1_ddlGeslacht='
    .'&ctl00$ContentPlaceHolder1$ddlReeks='
    .'&ctl00_ContentPlaceHolder1_ddlDatum='
    .'&ctl00$ContentPlaceHolder1$btnZoek=zoek'

Upvotes: 1

Related Questions