简体   繁体   中英

CURL request to download results

I need to download results from a website using a for loop to compile them.

Here's a sample link

(Note that it's an ASP request which displays a webpage with these parameters)

I wrote the following code with a CURL request to get me this (I need to submit the form):

<?php
for ($i=10; $i<500; $i++) {
$m = $i*10;
$url = 'http://sanskritischool.com/ProgramFiles/ProgressReport/Report/2012-2013/REPORTCARD_XI_XIII.aspx?';
$fields = array(
                    'ClassId' => 334,
                    'TermId' => 95,
                    'StudentId' => $m,
                    'SelectValue' => 1,
                    'strTerm' => urlencode('SECOND TERM'),
                    'SelectType' => urlencode('Academic '),
                    'ClassName' => urlencode('XI(A)')
                );

//url-ify the data for POST

foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

echo $fields_string;

//CURL POST

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

$result = curl_exec($ch);
curl_close($ch);

//Reset string

$fields_string='';
}
?>

Though the loop runs, it returns all errors ( Object reference not set to an instance of an object )

Screenshot of the error: http://imm.io/11by4

How can I successfully run this loop and get the results?

Excuse me if I'm asking a naive question, I'm only 16 and I'm new to PHP. I'd be glad if you can help!

You build the query string but you don't append it at the end of $url . You should do something like:

$url = $url.$fields_string;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM