【问题标题】:Retrieve Magento order comments through API通过 API 检索 Magento 订单评论
【发布时间】:2016-12-30 20:59:11
【问题描述】:

我需要从另一个 Web 应用程序中检索 Magento 订单 cmets。

Magento API 文档似乎有两种方法可以做到这一点,通过RESTSOAP

REST 文档很少。它只是说URL结构是这样的:http://magentohost/api/rest/orders/:orderid/comments

另外,默认的 REST 响应是 XML 格式的,但我需要它是 JSON 格式的。

我已经在 Magento 中设置了一个 API 用户,并且我已经使用 SOAP 从这个“其他”网络应用程序创建了 cmets。

我的问题,使用 REST:

  1. 如何以 JSON 而非 XML 格式获取响应?
  2. 如何进行身份验证?

【问题讨论】:

    标签: rest magento soap


    【解决方案1】:

    如何使用 REST Api 检索 Magento 订单评论

    这是 REST API 身份验证和检索订单注释的示例代码。

    Magento 测试文件

    <?php
    session_start();
    $mageFilename = '../app/Mage.php';
    
    require $mageFilename;  
    Mage::app();
    
    // $callbackUrl is a path to your file with OAuth authentication example for the Admin user
    $callbackUrl = "http://magentohost/extrafiles/oauth-test.php";
    //oAuth Initiate URL
    $temporaryCredentialsRequestUrl = "http://magentohost/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
    //oAuth Authorize
    $adminAuthorizationUrl = 'http://magentohost/admin/oauth_authorize';
    //oAuth Token URL
    $accessTokenRequestUrl = 'http://magentohost/oauth/token';
    //API Url
    $apiUrl = 'http://magentohost/api/rest';
    
    //Consumer Key
    $consumerKey = '{{PUT_YOUR_CONSUMER_KEY}}';
    //Consumer Secret
    $consumerSecret = '{{CONSUMER_SECRET}}';
    
    
    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/orders/21833/comments";
    
           $oauthClient->fetch($resourceUrl, array(), 'GET', array('Accept' => '*/*'));
           echo $oauthClient->getLastResponse();
        }
    } 
    catch (OAuthException $e) 
    {
        print_r($e->getMessage());
        echo "<br/>";
        print_r($e->lastResponse);
    }
    
    exit;
    ?>
    

    回复:

    [
      {
        "created_at": "2016-12-06 15:53:28",
        "comment": "On order, Following Activity is done By: XXX Captured amount of $2,669.97 online. Transaction ID: \"XXXXXXX\".",
        "is_customer_notified": "2",
        "is_visible_on_front": "0",
        "status": "processing"
      },
      {
        "created_at": "2016-12-06 15:53:28",
        "comment": "Order is Created with following : Order is Created By: XXX Grand Total: $2669.97",
        "is_customer_notified": null,
        "is_visible_on_front": "0",
        "status": "processing"
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-29
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多