我的最终目标是创建一个android / ios应用程序,该应用程序要求用户登录/或在我们的magento商店中创建一个帐户(如果他没有帐户)。
要实现此目的,我需要使用magento 1.9的脚本和REST API来创建magento帐户。
我有这个脚本:
<?php
$callbackUrl = "https://www.site.eu/webservices/NewCustomer1.php";
$temporaryCredentialsRequestUrl = "https://www.site.eu/oauth/initiate?oauth_callback=". urlencode($callbackUrl);
$adminAuthorizationUrl = 'https://www.site.eu/admin/oauth_authorize/';
$accessTokenRequestUrl = 'https://www.site.eu/oauth/token';
$apiUrl = 'https://www.site.eu/api/rest';
$consumerKey = 'myconsumerkeyfromadminpanel';
$consumerSecret = 'mysecretfrommagentoadmin';
session_start();
if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
$_SESSION['state'] = 0;
}
try {
$authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
$oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
$oauthClient->enableDebug();
if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
$requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
$_SESSION['secret'] = $requestToken['oauth_token_secret'];
$_SESSION['state'] = 1;
header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']);
//exit;
} else if ($_SESSION['state'] == 1) {
$oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
$accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
$_SESSION['state'] = 2;
$_SESSION['token'] = $accessToken['oauth_token'];
$_SESSION['secret'] = $accessToken['oauth_token_secret'];
header('Location: ' . $callbackUrl);
//exit;
} else {
$oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
$resourceUrl = "$apiUrl/customers";
$customerData = json_encode(array(
'firstname' => 'demofirstname',
'lastname' => 'demolastname',
'email_address' => 'demo@gmail.com',
'password' => 'demo1234',
'confirmation' => 'demo1234',
));
$headers = array('Content-Type' => 'application/json');
$oauthClient->fetch($resourceUrl, $customerData, OAUTH_HTTP_METHOD_POST, $headers);
print_r($oauthClient->getLastResponseInfo());
}
} catch (OAuthException $e) {
print_r($e);
}
?>
我完全不明白为什么它不能正常工作或创建任何帐户。
我已经在Magento管理面板中成功创建了一个应用程序,并且Ive得到了我的api密钥/令牌,我使用Firefox的REST客户端完成了此操作,我已经测试了密钥,并且它们确实可以接收数据。
我还将Magento管理面板中的所有权限都授予了用户/令牌/密钥,所以这不是问题。
当我在上面的代码中运行时,除了这些错误,我没有得到任何其他错误:
PHP Notice: Undefined index: state in /data/www/site.eu/createAcc.php on line 16
PHP Notice: Undefined index: state in /data/www/site.eu/createAcc.php on line 20