在php sdk中解析初始化和维护会话

时间:2014-09-15 08:05:17

标签: php wordpress parse-platform

我在WordPress中制作一个插件。我已经用我的插件安装了PHP-SDK。我正在通过插件的设置初始化我的解析。我的问题是,我每次都必须通过发送一个带有我的表单的查询来初始化解析。我希望解析器初始化一次,直到用户更改app_id,rest_api和master_key。如何维护解析初始化一次。我想填写表单一次,直到用户自己更改。

我的表格代码:

<form method="post" 
action="http://.../wp-content/plugins/wp-link-with-parse/php-sdk/test.php"> 
<?php settings_fields( 'wp-parse-settings-group' ); ?>
<?php do_settings_sections( 'wp-parse-settings-group' ); ?>
<table class="form-table">
<p class="wp_parse_required_fields">* Required Field</p>
<tr valign="top">
<th scope="row">Application ID* </th>
<td><input type="text" required name="app_id" value="<?php $options['app_id'] ?>/>
</td>
</tr>
<tr valign="top">
<th scope="row">REST API Key* </th>
<td><input type="text" required name="rest_api" value="<?php $options['rest_api'] ?>" />
</td>
</tr><tr valign="top">
<th scope="row">Master Key</th>
<td><input type="text" name="master_key" 
value="<?php $options['master_key'] ?>" /></td>
</tr></table>
<input  name="submit" type="submit" class="button-primary" value="save options" />
</form>

test.php此表单提交的位置

<?php
require 'vendor/autoload.php';
use Parse\ParseClient;
if(isset($_POST['submit']))
{
$app_id = $_POST['app_id'];
$rest_api = $_POST['rest_api'];
$master_key = $_POST['master_key'];
ParseClient::initialize($app_id, $rest_api, $master_key);
header("location:http://....com/?page_id=40");
}
use Parse\ParseUser;
if(isset($_POST['login']))
{
$username = $_POST['username'];
$password = $_POST['password'];
try {
$user = ParseUser::logIn($username, $password);
header("location:http://...com/?page_id=2");
// Do stuff after successful login.
} catch (ParseException $error) {
// The login failed. Check error to see why.
echo "Something getting Wrong";
}
}
?>

我的第二个问题:

如果我已成功初始化解析。我将如何在WordPress中为解析用户维护我的用户会话。基本上我想使用我的wordpress插件从解析中检索数据。

提前致谢。我希望你能理解我的问题。

1 个答案:

答案 0 :(得分:0)

要保存会话,您可以按照分析教程进行操作,但为了确保存储会话,首先需要设置ParseSessionStorage。最终结果将是:

require 'assets/vendor/autoload.php';
session_start(); // Make Sure you Put the session_start(); after you put the Autoload !

use Parse\ParseClient;
use Parse\ParseUser;
use Parse\ParseSessionStorage;

ParseClient::initialize('xxx', 'xxx', 'xxx');

//The Parse PHP docs are not that well covered and they missed a very important part.  you should setup the storage before. This is the Answer
ParseClient::setStorage( new ParseSessionStorage() );

//You get the current User
$user = ParseUser::getCurrentUser();

//You can then get the session token as follows:
$sessionToken = ParseUser::getCurrentUser()->getSessionToken();