123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace local_corfows;
- defined('MOODLE_INTERNAL') || die();
- class http_request {
- public function __construct() {
- $this->env = getenv('CORFOWS_ENV') ?
- getenv('CORFOWS_ENV') : 'production';
- }
- protected static function log($data) {
- global $CFG;
- file_put_contents(
- $CFG->dataroot . '/corfows.log',
- date('c') . " $data\n",
- FILE_APPEND
- );
- }
- public function post() {
- $ch = curl_init($this->url);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $this->data);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- if(getenv('CORFOWS_DONT_VERIFY_SSL')) {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- }
- $response = curl_exec($ch);
- if(getenv('CORFOWS_LOG')) {
- if (curl_error($ch)) $response = curl_error($ch);
- static::log(
- $this->url . ' - ' .
- json_encode( $this->data ) . ' - ' .
- $response
- );
- }
- curl_close($ch);
- return $response;
- }
- protected static function bearer_token() {
- $request = new static();
- if ($request->env === 'testing') {
- $request->url = 'https://apitest.corfo.cl:9101/api/oauth/token';
- } else if ($request->env === 'production') {
- $request->url = 'https://www.corfo.cl/oauth/token';
- }
- $request->data = http_build_query([
- 'scope' => 'resource.READ',
- 'client_secret' => getenv('CORFOWS_CLIENT_SECRET'),
- 'client_id' => getenv('CORFOWS_CLIENT_ID'),
- 'grant_type' => 'client_credentials'
- ], '', '&');
- $request->headers = [
- 'Content-type: application/x-www-form-urlencoded;charset=utf-8'
- ];
- $response = $request->post();
- return $response ? json_decode($response) : false;
- }
- public static function validation($codcert) {
- \require_login();
- $tokenresp = static::bearer_token();
- if (property_exists($tokenresp, 'error_description')) {
- return $tokenresp;
- }
- global $USER, $COURSE;
- $request = new static();
- if ($request->env === 'testing') {
- $request->url = 'https://apitest.corfo.cl:9101/OAG/API_WS_MOOC/Validate';
- } else if ($request->env === 'production') {
- $request->url = 'https://www.corfo.cl/api/StartupJourneyWSApiMooc/Validate';
- }
- $request->data = json_encode([
- 'Institucion' => getenv('CORFOWS_INSTITUTION_ID'),
- 'Rut' => $USER->username,
- 'Contenido' => $COURSE->idnumber,
- 'NombreContenido' => $COURSE->fullname,
- 'CodigoCertificacion' => $codcert,
- 'Evaluacion' => null,
- 'Correo' => $USER->email
- ]);
- $request->headers = [
- "Authorization:Bearer $tokenresp->access_token",
- 'Content-Type:application/json',
- ];
- $response = $request->post();
- return $response ? json_decode($response) : false;
- }
- }
|