Unable to fetch my schedule data from my schools site. Login with cURL won't work

后端 未结 3 1764
南笙
南笙 2021-01-16 23:27

Edit: Why the minus one?

What I am trying to do is the following:

  • I am trying to login to my school site using cURL and grab the sched
3条回答
  •  余生分开走
    2021-01-17 00:24

    This is how I solved it. The problem was probably the 'not-using-cookies' part. Still this is probably 'ugly' code, so any improvements are welcome!

    // This part is for retrieving the token from the hidden field.
    // To be honest, I have no idea what the cookie lines actually do, but it works.
    $getToken= curl_init();
    curl_setopt($getToken, CURLOPT_URL, '');       // Set the link
    curl_setopt($getToken, CURLOPT_COOKIEJAR, 'cookies.txt');  // Magic
    curl_setopt($getToken, CURLOPT_COOKIEFILE, 'cookies.txt'); // Magic
    curl_setopt($getToken, CURLOPT_RETURNTRANSFER, 1);         // Return only as a string
    $data = curl_exec($token);                                 // Perform action
    
    // Close the connection if there are no errors
    if(curl_errno($token)){print curl_error($token);}
    else{curl_close($token);} 
    
    // Use a regular expression to fetch the token
    $regex = '/name="token" value="(.*?)"/';
    preg_match($regex,$data,$match);
    
    // Put the login info and the token in a post header string
    $postfield = "token=$match[1]&user=&paswoord=";
    echo($postfields);
    
    // This part is for logging in and getting the data.
    $site = curl_init();
    curl_setopt($site, CURLOPT_URL, '

提交回复
热议问题