php – Magento REST API示例代码404s [复制]
发布时间:2020-12-13 22:53:07 所属栏目:PHP教程 来源:网络整理
导读:参见英文答案 Magento Rest API Oauth URL Returning 404????????????????????????????????????5个 从这里按照文档 http://www.magentocommerce.com/api/rest/introduction.html 当404尝试调用urls / oauth / initiate和时,示例代码低于404s /管理/ oauth_au
|
参见英文答案 >
Magento Rest API Oauth URL Returning 404????????????????????????????????????5个
从这里按照文档 http://www.magentocommerce.com/api/rest/introduction.html 当404尝试调用urls / oauth / initiate和时,示例代码低于404s / api / rest工作正常,因为我在.htaccess中有当前规则 RewriteRule ^api/rest api.php?type=rest [QSA,L] 我还缺少其他规则吗?根据我的理解,magento REST api应该可以解决这个问题.或者这个问题可能与网址重写无关? 我已经创建了适当的REST角色和属性,并将消费者密钥/秘密放在示例代码中,但没有骰子. 只是为了澄清,使用休息客户端或浏览器访问api / rest时,guest角色可以正常工作.但是,由于上述原因,尝试使用以下示例代码设置身份验证会导致我出现问题. <?php
/**
* Example of simple product POST using Admin account via Magento REST API. OAuth authorization is used
*/
$callbackUrl = "http://yourhost/oauth_admin.php";
$temporaryCredentialsRequestUrl = "http://magentohost/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://magentohost/admin/oauth_authorize';
$accessTokenRequestUrl = 'http://magentohost/oauth/token';
$apiUrl = 'http://magentohost/api/rest';
$consumerKey = 'yourconsumerkey';
$consumerSecret = 'yourconsumersecret';
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/products";
$productData = json_encode(array(
'type_id' => 'simple','attribute_set_id' => 4,'sku' => 'simple' . uniqid(),'weight' => 1,'status' => 1,'visibility' => 4,'name' => 'Simple Product','description' => 'Simple Description','short_description' => 'Simple Short Description','price' => 99.95,'tax_class_id' => 0,));
$headers = array('Content-Type' => 'application/json');
$oauthClient->fetch($resourceUrl,$productData,OAUTH_HTTP_METHOD_POST,$headers);
print_r($oauthClient->getLastResponseInfo());
}
} catch (OAuthException $e) {
print_r($e);
} ?>
解决方法
这部分是$callbackUrl =“http://yourhost/oauth_admin.php”;作品?如果它不起作用,请解决此问题.请记住用正确的值替换值http://yourhost/oauth_admin.php并首先在浏览器中尝试.
确保yourhost和magentohost本地或两者都是远程服务器.例如,如果您的magentohost是远程服务器而您的主机是本地的,则重定向将失败,您将收到404错误. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
