error api get all course and name of teacher tobe attach

Jump to solution
vinhnq
Community Novice

please view my code:

$curl = curl_init();
$curl2 = curl_init();
$pageNo = 0;
$msg = '';
$calenurl='';
$nameteacher='';
$token = "19439395395349534953495934593476pG5";

<table id="empTable" class="table table-bordered table-striped" style="width:100%;font-size:9pt;">
<thead>
<tr>
<th>ID</th>
<th>name</th>
<th>account_id</th>
<th>start_at</th>
<th>created date</th>
<th>teacher of course</th>
<th>students of course</th>
</tr>
</thead>
<tbody>
<?php
function curlCall($curl, $endpoint, $token, $pageNo){
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://abc.instructure.com/api/v1' . $endpoint . '?access_token=' . $token . '&sort=name&order=asc&per_page=100&page='.$pageNo
));
}
$endpoint = "/accounts/1/courses";
function curlCall2($curl2, $endpoint2, $token){
curl_setopt_array($curl2, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://abc.instructure.com/api/v1'.$endpoint.'?access_token='.$token.'&type=TeacherEnrollment'
));
}

do{
$pageNo++;
curlCall($curl, $endpoint, $token, $pageNo);
$resp = curl_exec($curl);
$records = json_decode($resp, true);
foreach ($records as $record) {
$endpoint2 = '/courses/'.$record['id'].'/enrollments';
$msg .= "<tr>";
$msg .= "<td>".$record['id']."</td>";
$msg .= "<td>".$record['name']."</td>";
$msg .= "<td>".$record['account_id']."</td>";
$msg .= "<td>".date("d-m-Y",strtotime($record['start_at']))."</td>";
$msg .= "<td>".date("d-m-Y",strtotime($record['created_at']))."</td>";

everything ok if not add code to show name of teacher for each course.

////////////////////////////////error here make page not load/////////////////////////////
///////      curlCall2($curl2, $endpoint2, $token);                             ///////
///////      $resp2 = curl_exec($curl2);                                             ///////
///////      $records2 = json_decode($resp2, true);                          ///////
///////      foreach ($records2 as $record2) {                                   ///////
///////     $nameteacher = $record2['sis_user_id'];                         ///////
///////     }                                                                                       ///////

////////////////////////////////error here make page not load/////////////////////////////

$msg .= "<td>".$nameteacher."</td>";
$msg .= "<td><a style='margin-left:20px;' href='".base_url()."studentofcourse/?courseid=".$record['id']."&name=".$record['name']."'><i class='fas fa-user-graduate'></i></a></td>";
$msg .= "</tr>";
}
}while(sizeof($records) > 0);
$pageNo = 0;
echo $msg;
?>
</tbody>
</table>

who can tell me the problems where in my code?  if I move error code in ////////// to another page (just show name of teacher) then ok, show ok into table.

Smiley Happy

1 Solution
James
Community Champion

Without checking for errors in logic, the thing most likely causing the failure is that there's a misnamed variable in the curlCall2 function. You're passing in $endpoint2 in the parameter list when you define the function, but you're referring to $endpoint (without the 2) when you set the CURLOPT_URL.

Here's what is happening:

Based on your use of camelCase for function names, it appears PHP is not your first programming language and you may be bringing some knowledge from those other programming languages over to PHP. Different languages have their own way of handling things. In PHP, variables declared outside a function are not recognized within the function unless you declare them using the global statement. You don't do that, nor do you want to do that.

Inside the curlCall2 function, $endpoint is undefined since you didn't pass it into the function and didn't declare it within the function. The API call is failing because of the undefined variable in there.

The second thing I see, but it may just be the way that you copy/pasted the code is that the variable definitions at the top need wrapped in the php tag. Since you say everything worked without that code, I'm going to assume you actually have it that way and was just copying that block for reference.

I would encourage you to follow Canvas' advice and not include the token as part of the querystring but make it part of the header. "If possible, using the HTTP Authorization header is recommended." It is supported, but when you do pagination the proper way (you are not) you get an URL that you are supposed to be able to use directly, but you will have to modify it by adding the token back in. There is a whole thread called Handling Pagination that gives examples in various languages. My PHP example includes how to get the token into the header.

Canvas is moving more things towards using bookmarks for pagination to cut down on people taking the approach of blindly putting in page numbers and overwhelming the system. The Enrollments API is going that way this spring (it might have been this month), but since you're only fetching teachers, you shouldn't have to mess with pagination. You may also want to put in a per_page=1 if there is only ever 1 teacher in a course. My experience suggests that it returns the information faster when it can stop looking rather than having to search through all of the enrollments. You may need to use a higher number if you have courses with lots of instructors. However, the way you have it, your code is only returning the name of one teacher anyway.

Another thing you should do is move the declaration of $nameteacher=''; to inside the loop that iterates through the courses and before you make the curlCall2 calll. Otherwise if a course doesn't have a teacher, it remembers the teacher from the previous course.

View solution in original post