Skip to content

Instantly share code, notes, and snippets.

@Savjee
Last active August 29, 2015 13:57
Show Gist options
  • Save Savjee/9485512 to your computer and use it in GitHub Desktop.
Save Savjee/9485512 to your computer and use it in GitHub Desktop.
ChamiloScraper
<?php
/*
ChamiloScraper
Copyright (c) 2014 Xavier Decuyper
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
class ChamiloScraper{
public function visitHome(){
echo "\n Visiting Chamilo homepage to get cookie..";
$this->curlPost('https://chamilo.hogent.be/', null, false);
}
public function login(){
$params = array(
'login' => '', // Hier aanvullen
'password' => '', // Hier aanvullen
'submitAuth' => 'Aanmelden',
'_qf__login' => ''
);
echo "\n Trying to authenticate with Chamilo..";
$this->curlPost('https://chamilo.hogent.be/index.php?go=login&application=user', $params, true);
}
public function requestLessons(){
$params = array(
'start_time' => '2014-02-27T23:00:00.000Z',
'end_time'=>'2014-02-28T23:00:00.000Z',
'method'=>'get_activities',
'activity_type'=>'lesson',
'context'=>'application\syllabus_plus'
);
echo $this->curlPost('https://chamilo.hogent.be/ajax.php', $params, true);
}
private function curlPost($url, $params, $post){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // timeout = 5 seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // timeout for all operations
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if($post){
$fields_string = '';
foreach($params as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_POST, 1);
}
$html = curl_exec($ch);
if(curl_error($ch)){
return false;
}
return $html;
}
}
$scraper = new ChamiloScraper();
$scraper->visitHome();
$scraper->login();
$scraper->requestLessons();
?>
@Savjee
Copy link
Author

Savjee commented Mar 11, 2014

(Under the MIT license)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment