简体   繁体   中英

Moodle plugin CURL methods

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?

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)

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