Do you mean you want to write PHP code that directly updates the Moodle gradebook?
If so my experience is that it is quite complex involving creating an instance of the grade_item class and
calling its methods.
e.g.
$gradeitem = new grade_item(array('id' => $gradeitemid, 'courseid' => $courseid));
$gradeitem->someMethodCall($param,$param,$param)
My function for doing something like that had about 75 lines of code in it.