0

I'm recently started developing on moodle and one big problem is that whenever i build a plugin with a web page, it don't execute any of my curl commands, i hear that moodle has his own php library(an php file called filelib.php i suppose) with curl commands and i should be using its commands instead of mine, but i don't have much experience with moodle development and php so all that code make me very confused.

i tried this question before and a gentleman helped me with an example, i tried his code and it didn't work(it changed the output but still show an error message, i also tried changing the code to no avail) and i didn't get much of an explanation about how curl commands work on moodle(i'm still grateful for his help) or whats commands on filelib.php will substitue my code, can someone help me?

my code:

<?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() 

the purpose of this curl request is to print a list of ids that will be used on another curl request to generate a video file that will be executed on a videoplayer, the variables:

$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,
);

Are all needed to execute the commands (one of the reasons i think the gentleman's example didn't work is that it didn't used all the variables of the code to get full validation).

well that is all, i'm very new to all of this so any explanation will help, thanks for the help.

  • Four things. 1: "but still show an error message" -- what is the error message? 2: `CURLOPT_NOBODY` means you will not get a body back from the request, so `$output` will always be empty. 3: Does `echo "Curl error: " . curl_error($curl);` indicate that the request itself failed? 4: I would personally get confused really quickly with two variables named `$OUTPUT` and `$output`. I'd choose a different variable name for one of them. – rickdenhaan Jan 25 '22 at 12:49
  • 1: the error message : object(stdClass)#806 (2) { ["status"]=> bool(false) ["mensagem"]=> string(50) "DisciplinaID is a obligatory camp!" } – squarefighter Jan 25 '22 at 13:41
  • yeah my code really need some correction, when i execute this code on a normal page it returns a string – squarefighter Jan 25 '22 at 13:43
  • it looks like this: ,{"MatriculaID":1006176,"CursoID":305203,"GradeID":264650,"DisciplinaID":101391,"AulaID":366969,"Tema":"Indicadores de sustentabilidade - se\u00e7\u00e3o 04" But repeated various times. – squarefighter Jan 25 '22 at 13:44
  • Okay, so the error message comes from the remote API and is saying something about `DisciplinaID`. I *think* this is because you execute the request already (right after setting `CURLOPT_NOBODY`) before you've set the post data. What happens if you remove the `curl_setopt()` line for `CURLOPT_NOBODY` and the `curl_exec` line immediately after that? – rickdenhaan Jan 25 '22 at 13:47
  • The variables on my question have values that should be insert by the user, those values are tied to their personal data so i used some template values that will return nothing if execute the code. – squarefighter Jan 25 '22 at 13:49
  • i tried now it doesn't return anything – squarefighter Jan 25 '22 at 13:56
  • I think my problem is with line 'curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));' i tried removing from the code on my localhost web page( where the code works) and it returned the same error message from the moodle page, i think that somehow it is not filling the camps 'IDstudent' and 'IDcourse' in $params since is returning and error saying is empty ( in my error it says "DisciplinaID is a obligatory camp!" but is actually named IDcourse in the code, because in my code the variable is named diferently). – squarefighter Jan 25 '22 at 14:16
  • so the actual error message with the code from the question is '{ ["status"]=> bool(false) ["mensagem"]=> string(50) "IDcourse is a obligatory camp!" }'. – squarefighter Jan 25 '22 at 14:17
  • So the underlying problem is that `$params` does not contain the information in the way the remote API expects it. If `IDcourse` is expected to be called `DisciplinaID` by the remote API, then shouldn't you use that key value `DisciplinaID` in your `$params` array? The remote API can't know (and doesn't care) what you call the variable in your code internally, just as long as you translate it to what they expect when you send the API request. – rickdenhaan Jan 25 '22 at 15:00
  • yeah i solved that, now is the same in both process,both the API and the code in moodle now use the same variable(DisciplinaID) the problem still persist, its like $curl, CURLOPT_POSTFIELDS, http_build_query($params) is either sending no value or the API is not receiving it properly, which is wierd since it executes normaly in normal html(even with the variable error). – squarefighter Jan 25 '22 at 17:48
  • It seems like this line - curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params)); is not working, either not sending values or the API is not receiving them properly. – squarefighter Jan 25 '22 at 18:04

0 Answers0