0

How can i make moodle execute CURL commands? i am making a local plugin for moodle and it have a page that must execute some CURL commands, i already tested those codes on my localhost and they work just fine, but whenever i put those codes on my plugin page i just doesn't do anything, i heard that moodle has its own CURL command library(filelib.php) and i must the equivalent commands from this library but i can't find any commands on that file that will do what my old code is supposed to do, can someone help me?

all variables are using sample values

<?php
require_once(__DIR__.'/../../config.php');
require_once($CFG->libdir.'/filelib.php');

$PAGE->set_url(new moodle_url(url:'/local/iesde/selecionaraulas.php'));
$PAGE->set_context(\context_system::instance());
$PAGE->set_title('selecionar aulas');


$PAGE->requires->js_call_amd('local_iesde/tabelas', 'init');


echo $OUTPUT->header();


echo $OUTPUT->render_from_template('local_iesde/manageaulas', $templatecontext);

$api_server = 'url';
$api_http_user = 'user';
$api_http_pass = 'pass';
$key_acess = 'key';
$key_name = 'API-KEY';
$format = 'json';
$params = array(
'IDstudent' => 000000,
'IDcourse' => 000000,
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "{$api_server}/format/{$format}");
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($curl, CURLOPT_USERPWD, "{$api_http_user}:{$api_http_pass}");
curl_setopt($curl, CURLOPT_HTTPHEADER, array("{$key_name}:{$key_acess}"));
curl_setopt($curl, CURLOPT_NOBODY, 1);
curl_exec($curl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($curl);
var_dump(json_decode($output));
echo $output;


echo $OUTPUT->footer() 

whenever i execute this page on my localhost echo $output writes the information i needed but when i put this code on my moodle local plugin page i shows:

object(stdClass)#806 (2) { ["status"]=> bool(false) ["mensagem"]=> string(50) "DisciplinaID is a obligatory camp!" }

can someone help me?

273K
  • 29,503
  • 10
  • 41
  • 64

1 Answers1

0

Here's an example of using curl in Moodle

$url = "{$api_server}/format/{$format}"

$data = array(
    'IDstudent' => 000000,
    'IDcourse' => 000000,
);

$jsondata = json_encode($data);

$options = array(
    'RETURNTRANSFER' => 1,
    'HEADER' => 0,
    'FAILONERROR' => 1,
);

$header = array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($jsondata),
    'Accept: application/json',
    "{$key_name}:{$key_acess}"
);

$curl = new \curl();
$curl->setHeader($header);
$jsonresult = $curl->post($url, $jsondata, $options);

$result = json_decode($jsonresult)
Russell England
  • 9,436
  • 1
  • 27
  • 41
  • Thanks, it was really hard to make thse methos work on moodle – squarefighter Jan 11 '22 at 17:46
  • In your case, i should just add the variables '$api_http_user = 'user';' and '$api_http_pass => 'pass';' add then to an array(**$newarray = array ($api_http_user => 'user', $api_http_pass => 'pass'**);) and execute them into the $jsonresult function right? $jsonresult = $curl->post($url, $jsondata, $options, **$newarray**); for example – squarefighter Jan 20 '22 at 18:31
  • because i find out in your code you didn't use $api_http_user = 'user'; $api_http_pass = 'pass'; variables. – squarefighter Jan 20 '22 at 18:34
  • I tried you example code and it returned "exception - http_build_query(): Argument #1 ($data) must be of type array, null given" – squarefighter Jan 21 '22 at 11:48
  • Where is the command "curl_setopt($curl, CURLOPT_USERPWD, "{$api_http_user}:{$api_http_pass}");" in your code? – squarefighter Jan 31 '22 at 20:41