php – Magento REST API示例代码404s [复制]

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Magento REST API示例代码404s [复制]脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Magento Rest API Oauth URL Returning 404                                    5个
从这里按照文档
http://www.magentocommerce.com/api/rest/introduction.html

当@R_403_2480@尝试调用urls / oauth / inITiate和时,示例代码低于@R_403_2480@s
/管理/ oauth_authorize.

/ 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']) &amp;& $_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错误.

脚本宝典总结

以上是脚本宝典为你收集整理的php – Magento REST API示例代码404s [复制]全部内容,希望文章能够帮你解决php – Magento REST API示例代码404s [复制]所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。