The last post in this series described the start of a little project to learn more about PHP/Moodle programming in order to get BAM into Moodle. Essentially everything is done, there are two main tasks left:
- Identify how to “properly” retrieve a file over http in PHP/Moodle and figure out how to use it.
- Confirm the phpxml is the best way to parse XML in PHP/Moodle and figure out how to use it.
Once those are done, a rudimentary eStudyGuide block will be complete and I’ll have filled in two of the main holes in my knowledge necessary to put BAM into Moodle.
How to retrieve a file over http in PHP/Moodle
What a difference some time makes. I spent a bit of time Tuesday hunting the web and Moodle for information on this. This morning, apparently, it took 5 minutes. curl seems to be the go.
Here’s a list of questions I think I need to answer around the use of curl, and hopefully the answers I’ve found:
- How do you use curl to get through basic auth?
CURLAUTH_ANY is a ?constant? that says use any HTTP auth method.
- How do you set a mime-type on what’s going back to the client?
The simplest examples simply get the remote file and return it to the browser. If you do this with a non-HTML file there appears to be some issues around the client handling it appropriately.
One solution I’ve found is to use the CURLOPT_FILE option to save what is returned by curl to the file system. Then use the header and readfile functions to set everything up appropriately i.e.
header("Content-type: image/jpeg"); header("Content-Disposition: attachment; filename=imageName.jpg"); readfile("tmpfile");
Would imagine you’d have to use some sort of session variable to keep the filename unique and also remember to remove the file.
Wonder if you can use header without the need for readfile? Yep, the works, use the CURLOPT_RETURNTRANSFER option so that the file is returned as a string and then use the following
header("Content-type: image/jpeg"); header("Content-Disposition: attachment; filename=imageName.jpg"); print $buffer;
Of course the question now becomes what if you are transferring really large files. Won’t that consume “RAM” for the web server and on a heavily used site cause some “issues”? So maybe the file option is better.
- What are the necessary checks etc you should do when using curl?
Seem to be all fairly standard ones, check return values etc, don’t do horrible security stuff. That said, there seems to be some variability within the existing Moodle code that is using curl – some seems to be quite anal about checks.
- What’s TRUE in php?
CURLOPT_BINARYTRANSFER needs to be set to TRUE for transferring binary files. What’s the numeric value for TRUE in PHP? Okay, 0 is false. Somewhat familiar.
Appears, at the moment that the “xmlize” library in Moodle is the simplest method to parse XML. Produces a nested data structure with the content. Pretty similar to what is done at the moment. Is there something better?
Given that parsing XML isn’t a main requirement for BAM, I won’t bother going any further. I think I’ll be using Magpie to parse the RSS that BAM needs to manipulate.
xmlize is simple to use, looks like it is time for lunch. After lunch will be trying to code all this up. I want a working eStudyGuide block by the end of the day.