I am using Google people API client library in PHP.
After successfully authenticating, I want to get email
Help me to find out the problem from my code below.
Can i use multiple scopes to get email, beacuse when i m using different scope it gives me error
function getClient() {
$client = new Google_Client();
$client->setApplicationName('People API');
$client->setScopes(Google_Service_PeopleService::CONTACTS);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
$str = file_get_contents('credentials.json');
$json = json_decode( $str, true );
$url = $json['web']['redirect_uris'][0];
if (isset($_GET['oauth'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = $url;
header('Location: ' . filter_var($redirect_uri,FILTER_SANITIZE_URL));
} else if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$people_service = new Google_Service_PeopleService($client);
} else {
$redirect_uri = $url.'/?oauth';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
return $people_service;
}
$optParams = array(
'pageSize' => 100,
'personFields' => 'addresses,ageRanges,biographies,birthdays,braggingRights,coverPhotos,emailAddresses,events,genders,imClients,interests,locales,memberships,metadata,names,nicknames,occupations,organizations,phoneNumbers,photos,relations,relationshipInterests,relationshipStatuses,residences,sipAddresses,skills,taglines,urls,userDefined',
);
$people_service = getClient();
$connections = $people_service->people_connections-
>listPeopleConnections('people/me', $optParams);
get attributes from the URI using Slim 2 Framwork
http://localhost:8080/serverEndModule1/v1/varifyid=OQ==&code=1e212ffc375e52a9c6e7debe8adcc17
using the following Code:
$app->get('/varify', function () use ($app) {
$id = $app->request->get('id');
$paramValue2 = $app->request->get('code');
$db = new DbOperation();
$response = array();
$db->userVarify($id, $code); // send to database
help me to implement the following $app->get route .
Thanks I got it now it' working
$app->get('/varify', function() use ($app){
$req = $app->request();
$id = $req->get('id'); //
$code = $req->get('code');
$db = new DbOperation();
$response = array();
$res= $db->userVarify($id, $code);
}
I need to upload files into a forum in moodle through Web Services, I have been searching and I found that exist a WS called core_files_upload(). I looked at the code of this WS and the context level of this WS is only (block, course, coursecat, system, user or module), the context forum not exist and this WS is deprecated.
I also found that in Moodle 3.0 there are 2 new WS related to forums, they are mod_forum_add_discussion and mod_forum_add_discussion_post but I also looked at the code and they don't allow upload.
Then I decided that the best solution would be to create a new one, but I don't know much about uploading files in Moodle and can't find much information. I'm a little bit familiar with the structure of the WS in Moodle because I have made 2 very simple WS but I don't know how to do this.
If anyone knows how to do it or have any examples or documentation that may be useful, it would be helpful.
Thanks in advance.
You can look up for the file
/webservice/upload.php
this file is for uploading files
$params = array('token'=>$token, 'filepath' => $filepath
, 'filearea'=>'draft') + $_FILES ;
$serverurl = <domainname>. '/webservice/upload.php'. '?token=' . $token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, $serverurl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($ch);
or this can code can help you, that will create a file in moodle data and record in file table from temp file.
$fs = get_file_storage();
$target_path = <target-path>;
$context = get_context_instance(CONTEXT_USER, $userID, MUST_EXIST);
$cm = get_coursemodule_from_instance('forum', $forum->id);
$modcontext = context_module::instance($cm->id, MUST_EXIST);
//saving in draft
$item = rand();
$file_record = array('contextid'=>$context->id, 'component'=>'user', 'filearea'=>'draft', 'itemid'=>$item,
'filepath'=>"/", 'filename'=><filename>, 'userid'=>$userID);
$fs->create_file_from_pathname($file_record, $CFG->dirroot."/".$target_path.<filename>);
// saving for post
$file_record = array('contextid'=>$modcontext->id, 'component'=>'mod_forum', 'filearea'=>'post', 'itemid'=>$post->id,
'filepath'=>"/", 'filename'=><filename>, 'userid'=>$userID);
$fs->create_file_from_pathname($file_record, $CFG->dirroot."/".$target_path.$imagefileEnc);
At the request of #insightful i I'm going to publish my code, I hope this helps somebody
First the externallib.php of the WS
externallib.php
require_once($CFG->libdir . "/externallib.php");
require_once($CFG->libdir . "/filelib.php");
class local_upload_tfg_external extends external_api {
/**
* Returns description of method parameters
* #return external_function_parameters
*/
public static function upload_tfg_parameters() {
return new external_function_parameters(
array('community_key' => new external_value(PARAM_TEXT, 'Comunidad a la que se subira el TFG o TFM.', VALUE_REQUIRED, '', NULL_NOT_ALLOWED),
'username' => new external_value(PARAM_TEXT, 'Usuario que sube el archivo.', VALUE_REQUIRED, '', NULL_NOT_ALLOWED),
'folder_name' => new external_value(PARAM_TEXT, 'Directorio donde se subira el archivo.', VALUE_REQUIRED, '', NULL_NOT_ALLOWED))
);
}
public static function upload_tfg($community_key, $username, $folder_name) {
global $CFG, $DB, $USER;
require_once($CFG->dirroot . "/mod/forum/lib.php");
$params = self::validate_parameters(self::upload_tfg_parameters(),
array(
'community_key' => $community_key,
'username' => $username,
'folder_name' => $folder_name
));
//Context validation
//OPTIONAL but in most web service it should present
$contextoUser = get_context_instance(CONTEXT_USER, $USER->id);
self::validate_context($contextoUser);
$fs = get_file_storage();
$warnings = array();
//We get the course to which the file must be uploaded
$sql = "SELECT id FROM mdl_course WHERE idnumber = ?";
$result = $DB->get_records_sql($sql, array($community_key));
foreach ( $result as $n ) {
$courseid = $n->id;
}
if( is_null($courseid)){
throw new moodle_exception('no_course','No existe el curso');
}
//e get the forum to upload the file
$sql = "SELECT f.id FROM mdl_forum f , mdl_course c WHERE c.idnumber = ? AND c.id = f.course AND f.name = ?";
$result = $DB->get_records_sql($sql, array($community_key, $folder_name));
foreach ( $result as $n ) {
$forumid = $n->id;
}
if( is_null($forumid)){
throw new moodle_exception('no_forum','No existe el foro');
}
//We get the user who wants to upload the file
$sql = "SELECT id FROM mdl_user WHERE username = ?";
$result = $DB->get_records_sql($sql, array($username));
foreach ( $result as $n ) {
$userid = $n->id;
}
if( is_null($userid)){
throw new moodle_exception('no_user','No existe el usuario');
}
//We check if the user belongs to the course
$contextCourse = context_course::instance($courseid);
$enrolled = is_enrolled($contextCourse, $userid, '', true);
if( !$enrolled){
throw new moodle_exception('user_no_enrolled','El usuario no está en el curso');
}
//Files received
$numFiles = count($_FILES);
if($numFiles > 0){
// Request and permission validation.
$forum = $DB->get_record('forum', array('id' => $forumid), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($forum, 'forum');
$context = context_module::instance($cm->id);
self::validate_context($context);
// Normalize group.
if (!groups_get_activity_groupmode($cm)) {
// Groups not supported, force to -1.
$groupid = -1;
} else {
// Check if we receive the default or and empty value for groupid,
// in this case, get the group for the user in the activity.
if ($groupid === -1 ) {
$groupid = groups_get_activity_group($cm);
} else{
$groupid = -1;
}
}
if (!forum_user_can_post_discussion($forum, $groupid, -1, $cm, $context)) {
throw new moodle_exception('cannotcreatediscussion', 'forum');
}
$thresholdwarning = forum_check_throttling($forum, $cm);
forum_check_blocking_threshold($thresholdwarning);
foreach ($_FILES as $fieldname=>$uploaded_file) {
// check upload errors
if (!empty($_FILES[$fieldname]['error'])) {
switch ($_FILES[$fieldname]['error']) {
case UPLOAD_ERR_INI_SIZE:
throw new moodle_exception('upload_error_ini_size', 'repository_upload');
break;
case UPLOAD_ERR_FORM_SIZE:
throw new moodle_exception('upload_error_form_size', 'repository_upload');
break;
case UPLOAD_ERR_PARTIAL:
throw new moodle_exception('upload_error_partial', 'repository_upload');
break;
case UPLOAD_ERR_NO_FILE:
throw new moodle_exception('upload_error_no_file', 'repository_upload');
break;
case UPLOAD_ERR_NO_TMP_DIR:
throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
break;
case UPLOAD_ERR_CANT_WRITE:
throw new moodle_exception('upload_error_cant_write', 'repository_upload');
break;
case UPLOAD_ERR_EXTENSION:
throw new moodle_exception('upload_error_extension', 'repository_upload');
break;
default:
throw new moodle_exception('nofile');
}
}
/*$file = new stdClass();
$file->filename = clean_param($_FILES[$fieldname]['name'], PARAM_FILE);
// check system maxbytes setting
if (($_FILES[$fieldname]['size'] > get_max_upload_file_size($CFG->maxbytes))) {
// oversize file will be ignored, error added to array to notify web service client
$file->errortype = 'fileoversized';
$file->error = get_string('maxbytes', 'error');
} else {
$file->filepath = $_FILES[$fieldname]['tmp_name'];
// calculate total size of upload
//$totalsize += $_FILES[$fieldname]['size'];
}
$files[] = $file;*/
$filename = $_FILES[$fieldname]['name'];
$filepath = $_FILES[$fieldname]['tmp_name'];
// Create the discussion.
$discussion = new stdClass();
$discussion->course = $course->id;
$discussion->forum = $forum->id;
$discussion->message = "<p>".$filename."</p>";
$discussion->messageformat = FORMAT_HTML; // Force formatting for now.
$discussion->messagetrust = trusttext_trusted($context);
$discussion->itemid = 0;
$discussion->groupid = $groupid;
$discussion->mailnow = 0;
$discussion->subject = $filename;
$discussion->name = $discussion->subject;
$discussion->timestart = 0;
$discussion->timeend = 0;
if ($discussionid = forum_add_discussion($discussion)) {
$discussion->id = $discussionid;
//We update the user of the discursion to the one of the user received
$sql = "UPDATE mdl_forum_discussions SET userid= ? WHERE id = ?";
$DB->execute($sql, array($userid, $discussionid));
/*
// Trigger events and completion.
$params = array(
'context' => $context,
'objectid' => $discussion->id,
'other' => array(
'forumid' => $forum->id,
)
);
$event = \mod_forum\event\discussion_created::create($params);
$event->add_record_snapshot('forum_discussions', $discussion);
$event->trigger();
$completion = new completion_info($course);
if ($completion->is_enabled($cm) && ($forum->completiondiscussions || $forum->completionposts)) {
$completion->update_state($cm, COMPLETION_COMPLETE);
}
$settings = new stdClass();
$settings->discussionsubscribe = false; //discussionsubscribe (bool); subscribe to the discussion?, default to true
forum_post_subscription($settings, $forum, $discussion
//se guardara el fichero
// Get any existing file size limits.
//$maxareabytes = FILE_AREA_MAX_BYTES_UNLIMITED;
//$maxupload = get_user_max_upload_file_size($context, $CFG->maxbytes);
// Check the size of this upload.
//if ($maxupload !== USER_CAN_IGNORE_FILE_SIZE_LIMITS && $totalsize > $maxupload) {
// throw new file_exception('userquotalimit');
//}
*/
//We get the course to which the file must be uploaded
$sql = "SELECT id FROM mdl_forum_posts WHERE discussion = ? ORDER BY id ASC LIMIT 1";
$result = $DB->get_records_sql($sql, array($discussionid));
foreach ( $result as $n ) {
$postid = $n->id;
}
if( is_null($postid)){
throw new moodle_exception('no_post','No existe el post');
}
//We update the post to put the user that they send us and indicate that they have a file
$sql = "UPDATE mdl_forum_posts SET userid= ?, attachment= 1 WHERE id = ?";
$DB->execute($sql, array($userid, $postid));
$user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
$file_record = new stdClass;
$file_record->component = 'mod_forum';
$file_record->contextid = $context->id;
$file_record->userid = $userid;
$file_record->filearea = 'attachment';
$file_record->filename = $filename;
$file_record->filepath = '/';
$file_record->itemid = $postid;
$file_record->license = $CFG->sitedefaultlicense;
$file_record->author = fullname($user);
$file_record->source = $filename;
//Check if the file already exist
$existingfile = $fs->file_exists($file_record->contextid, $file_record->component, $file_record->filearea,
$file_record->itemid, $file_record->filepath, $file_record->filename);
if ($existingfile) {
throw new file_exception('filenameexist');
} else {
$stored_file = $fs->create_file_from_pathname($file_record, $filepath);
}
} else {
throw new moodle_exception('couldnotadd', 'forum');
}
}
}else{
throw new moodle_exception('nofile');
}
$result = array();
$result['discussionid'] = $discussionid;
$result['warnings'] = $warnings;
return $result;
}
/**
* Returns description of method result value
* #return external_multiple_structure
*/
public static function upload_tfg_returns() {
return new external_single_structure(
array(
'discussionid' => new external_value(PARAM_INT, 'New Discussion ID'),
'warnings' => new external_warnings()
)
);
}
}
And an example to call this WS maybe this:
$target_url = "http://localhost/pruebaMoodle/webservice/rest/server.php";
//$target_url = "http://localhost/upload.php";
$username = "raula"; //User uploading the file (must belong to the course)
$community_key = "c15c033a43976gPT"; //custom identifier of course
$folder_name = "Course 2016"; //Name of the forum to which the file will be uploaded
$fname = 'Prueba.txt'; //Name of the file or path where it is located but is in the same directory as this file
$token = "28b054f102a75a0eab2a0053fd930d62"; //token created for ws
$file_name_with_full_path = realpath($fname);
$cfile = new CURLFile($file_name_with_full_path);
$post = array (
'username' => $username,
'community_key' => $community_key,
'folder_name' => $folder_name,
'wstoken' => $token,
'wsfunction' => 'upload_tfg_tfm',
'moodlewsrestformat' => 'json',
'upload_file' => $cfile
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_HTTPHEADER,array('User-Agent: Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15','Referer: http://localhost','Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec ($ch);
if ($result === FALSE) {
echo "Error sending " . $fname . " " . curl_error($ch) . "\n";
curl_close ($ch);
}else{
curl_close ($ch);
echo "<br><br>Result: " . $result. "<br>";
}
I hope this can serve you
I am using php api's in my website (to make a profile page)
I am using the below code:
$action = $_REQUEST["action"];
switch($action){
case "fblogin":
include 'facebook.php';
$appid = "**********";
$appsecret = "*************************";
$facebook = new Facebook(array(
'appId' => $appid,
'secret' => $appsecret,
'cookie' => TRUE,
));
$fbuser = $facebook->getUser();
if ($fbuser) {
try {
$user_profile = $facebook->api('/me');
}
catch (Exception $e) {
echo $e->getMessage();
exit();
}
$user_fbid = $fbuser;
$user_email = $user_profile["email"];
$user_fnmae = $user_profile["first_name"];
$user_image = "https://graph.facebook.com/".$user_fbid."/picture?type=large";
$check_select = mysql_num_rows(mysql_query("SELECT * FROM `fblogin` WHERE email = '$user_email'"));
if($check_select > 0){
mysql_query("INSERT INTO `fblogin` (fb_id, name, email, image, postdate) VALUES ('$user_fbid', '$user_fnmae', '$user_email', '$user_image', '$now')");
}
}
break;
}
It gives an error saying : Undefined index: action
I am using wamp server as localhost. Please provide a solution
Everything else works fine except for the location. The actual city/state does not come across. Instead the word Array is displayed. My field type in the database is text but I've tried every field type through mysql. The HTML file is JSON where it comes to the field names. I am very new at this so any help is appreciated.
This is my php file:
<? ob_start(); ?>
<?php
define('FACEBOOK_APP_ID', '');
define('FACEBOOK_SECRET', '');
// No need to change function body
function parse_signed_request($signed_request, $secret) {
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
// decode the data
$sig = base64_url_decode($encoded_sig);
$data = json_decode(base64_url_decode($payload), true);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
error_log('Unknown algorithm. Expected HMAC-SHA256');
return null;
}
// check sig
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
if ($sig !== $expected_sig) {
error_log('Bad Signed JSON signature!');
return null;
}
return $data;
}
function base64_url_decode($input) {
return base64_decode(strtr($input, '-_', '+/'));
}
if ($_REQUEST) {
$response = parse_signed_request($_REQUEST['signed_request'],
FACEBOOK_SECRET);
/*
echo "<pre>";
print_r($response);
echo "</pre>"; // Uncomment this for printing the response Array
*/
$name = $response["registration"]["name"];
$email = $response["registration"]["email"];
$gender = $response["registration"]["gender"];
$prosecutor = $response["registration"]["prosecutor"];
$location = $response["registration"]["location"];
// Connecting to Database
$con = mysql_connect("my_hosting_site","Database","password");
if (!$response)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Database", $con);
mysql_query("INSERT INTO my_table (name, email, gender, prosecutor, location)
VALUES ('$name', '$email', '$gender', '$prosecutor', '$location')");
mysql_close($con);
}
$URL="https://www.facebook.com";
header ("Location: $URL");
?>
<? ob_flush(); ?>
Facebook
Basically, it's an array.
[location] => Array
(
[name] => Delhi, India
[id] => 1.1604xxxxxx286E+14
)
Use this for getting location stored in your db, like I have written below:
$location = $response["registration"]["location"]["name"];