我最近开始使用PHP curl()
函数,我正在尝试将检索到的JSON对象转换为关联的数组。任何人都能指出我正确的方向吗?谢谢!
<?php
$ch = curl_init("https://canvas.instructure.com/api/v1/courses?access_token=7~8SXvaXHjMFZFHAdU5yU0pxNmVwAj40sjW7jRHw1Bvzq09QTFWrJRFxTu4pHAqSZU");
curl_exec($ch);
curl_close($ch);
?>
回应:
[{"account_id":81259,"course_code":"CS50","default_view":"feed","id":870674,"name":"CS50","start_at":"2014-08-05T18:29:18Z","end_at":null,"public_syllabus":false,"storage_quota_mb":250,"apply_assignment_group_weights":false,"calendar":{"ics":"https://canvas.instructure.com/feeds/calendars/course_6QRRvAKDngrrXtTBhzCA5Oz46g3aLgfRt7PNH0NN.ics"},"enrollments":[{"type":"student","role":"StudentEnrollment","enrollment_state":"active"}],"hide_final_grades":false,"workflow_state":"available"}]
答案 0 :(得分:1)
使用CURLOPT_RETURNTRANSFER
以字符串形式捕获结果,这是您传递给json_encode
的结果。我认为您已将$ch
传递给json_decode
,这不是您想要的。 (正如错误消息所述,$ch
是一个资源,json_decode
期望传递一个字符串。)
$ch = curl_init("https://canvas.instructure.com/api/v1/courses?access_token=7~8SXvaXHjMFZFHAdU5yU0pxNmVwAj40sjW7jRHw1Bvzq09QTFWrJRFxTu4pHAqSZU");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// ...
$response = curl_exec($ch);
// $response will be false if the curl call failed
if($response) {
var_dump(json_decode($response, true));
}
有关详细信息,请参阅curl_setopt
文档。