Search code examples
phpcurldrupal-7moodle

having php fatal error Function name must be a string in C:\wamp\www\drupal-7.42\sites\all\modules\form_fun\form_fun.cake.inc on line 78


I am trying to send a curl request to moodle using drupal.But each tiem i run this code i get an error message that function name must be a string.I have tried everything but nothing is working.Can anybody please help me?

    function form_fun_cake_submit(&$form, &$form_state) {        
         $serverUrl=' http://localhost/moodle/my/webservice/rest/server.php?wstoken=d90b5d90db13711d12df525366f15db1';

     $functionName = 'core_user_create_users';
            $user1 = new stdClass();
            $user1->username = 'testusername1';
            $user1->password = 'Uk3@0d5w';
            $user1->firstname = 'testfirstname1';
            $user1->lastname = 'testlastname1';
            $user1->email = '[email protected]';
            $user1->auth = 'manual';
            $user1->idnumber = '';
            $user1->lang = 'en';
            $user1->timezone = 'Australia/Sydney';
            $user1->mailformat = 0;
            $user1->description = '';
            $user1->city = '';
            $user1->country = 'AU';     //list of abrevations is in yourmoodle/lang/en/countries
            $preferencename1 = 'auth_forcepasswordchange';
            $user1->preferences = array(
                array('type' => $preferencename1, 'value' => 'true')
                );

            $users = array($user1);
            $params = array('users' => $users);

            /// REST CALL
    $rest_format = 'json';

        //$server_url = $domain_name . '/webservice/rest/server.php' . '?wstoken=' . $token . '&wsfunction=' . $function_name;
        $server_url = 'localhost/moodle/my' . '/webservice/rest/server.php'. '?wstoken=' . '15bd45a3dab2958b7e8fc237b14f76cd' .'&wsfunction='. $functionName;
        dpm($server_url);
        require_once('curl.inc');
        $curl = new curl;
        $rest_format = ($rest_format == 'json') ? '&moodlewsrestformat=' . $rest_format : '';

  $resp =$curl($server_url . $rest_format, $params); //This is my line no 78
        dpm($rest_format);
        $respRc = json_decode($resp, true);




            dpm($resp);


            echo '</br>************************** Server Response    createUser()**************************</br></br>';
            echo $server_url . '</br></br>';

            var_dump($resp);




    }

Solution

  • Line 78 replace $curl(...) with new curl(..), your object instancing is incorrect. What is telling the php log is that you are using a variable as function name aka $curl (nothing to do with $functionName wich might have generated an error later with curl not on compile if it was wrong).

    $resp = new curl($server_url . $rest_format, $params);