Call Moodle Library in an external php Page (MOODLE 3.7+)

Call Moodle Library in an external php Page (MOODLE 3.7+)

by Edgar Conrado Hernandez -
Number of replies: 4

Hello,

I tried to create a page in PHP using the Moodle libraries, this page is located in different path that my moodle instalacion, but is in the same server. When I try to call the Moodle Libraries my PHP page redirect to the Moodle Dashboard. This is my code.

<?php 

require_once "../../moodle/config.php";
require_once "../../moodle/lib/adminlib.php";
require_once "../../moodle/lib/gdlib.php";

$OGP=new get_pictures();

class get_pictures
{        
    /**
     * Constructor
     */      
    function get_pictures()
    {

        global $DB;
        $user = $DB->get_record('user', array ('idnumber' => 10252, 'deleted' => 0));

        //Save picture of the employee in Moodle System
        if ($newrev = $this->my_save_profile_image($user->id, $file))
        {
            $DB->set_field('user', 'picture', $newrev, array('id'=>$user->id));
        }
    }
}
?>

I do not know if I am missing something in my code or I need to configure something in my Moodle, I will really appreciate any idea.

Thanks
Average of ratings: -
In reply to Edgar Conrado Hernandez

Re: Call Moodle Library in an external php Page (MOODLE 3.7+)

by Benjamin Ellis -
Picture of Particularly helpful Moodlers
Edgar,

Are you calling the script in the same domain as Moodle?

Ben
In reply to Benjamin Ellis

Re: Call Moodle Library in an external php Page (MOODLE 3.7+)

by Edgar Conrado Hernandez -
Hi Ben, 

Thanks, yes is the the same domain but diferrent path

Moodle:   http://localhost/moodle
My PHPScript: http://localhost/ole-plugins/get_ole_user_photo/get_ole_user_photo.php

Regards
In reply to Edgar Conrado Hernandez

Re: Call Moodle Library in an external php Page (MOODLE 3.7+)

by Neill Magill -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
The moment you add config.php your page must be inside of the Moodle directory, i.e. http://localhost/moodle/...

If you want to use Moodle functionality outside of that you will either need to use an existing external webservice function, or create a Moodle plugin that implements a web service function that does what you want.
Average of ratings: Useful (1)