【问题标题】:Get magento product feed using Mangento rest api but OAuth error [closed]使用 Mangento rest api 获取 magento 产品提要但 OAuth 错误 [关闭]
【发布时间】:2014-02-04 04:26:54
【问题描述】:

我有一个核心 php 网站,客户端创建不同类型的小部件以图表形式获取报告。 我想为此开发小部件,以使用 Magento 商店 API 在 php 网站上显示 Magento 订单报告、销售报告等。 谁知道magento Api来获取产品信息,请告诉我。

我正在使用此代码

  <?php

  $callbackUrl = "https://air.co.uk/oauth_customer.php";
  $temporaryCredentialsRequestUrl = "https://air.co.uk/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
  $adminAuthorizationUrl = 'https://air.co.uk/oauth/authorize';
  $accessTokenRequestUrl = 'https://air.co.uk/oauth/token';
  $apiUrl = 'https://air.co.uk/api/rest';
  $consumerKey = 'xb4up56a4f95ewl4l5s6xp097wi6g4uwv';
  $consumerSecret = 'ust6oh4t63fw5wcjo8gkqdpgc4tw0iscx';
 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";
    $oauthClient->fetch($resourceUrl);
    $productsList = json_decode($oauthClient->getLastResponse());
    print_r($productsList);
}
} catch (OAuthException $e) {
print_r($e);
 }

 ?>

但是我遇到了这个错误

   Fatal error: Class 'OAuth' not found in /home/air/public_html/mapiuse/index.php on line 18

请帮助从哪里添加 OAuth 类。什么是获得我工作的最佳解决方案。 谢谢

【问题讨论】:

    标签: php magento magento-1.7 shopping-cart magento-1.6


    【解决方案1】:

    使用 Soap Api 展示 Magento 商店中的产品,这非常简单,而且痛苦少。

    <?php
    
     $mage_url = 'https://domain.com/index.php/api/soap/index/wsdl/1'; 
     $mage_user = 'Mohammad'; 
    $mage_api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; 
    
    $soap = new SoapClient( $mage_url ); 
    $session_id = $soap->login( $mage_user, $mage_api_key );
    $resources = $soap->resources( $session_id );
    
     // array to store product IDs
     $productIDs = array();
     $productIndex = array();
    
     // counters
     $i = 0;
     $s = 0;
    
     // API call to get full product list
     $productList = $soap->call($session_id, 'catalog_product.list');
    
    // Sort through returned array and index what array entry relates to each varient code
     if( is_array( $productList ) && !empty( $productList )) { 
      foreach( $productList as $productListSingle ) {
    
        $sku = $productListSingle['sku'];
        $productIndex[$sku] = $s;
    
        $s++;
    
      }
    }
    
    // Sort through the returned product list and populate an array with product IDs
    if( is_array( $productList ) && !empty( $productList )) { 
    foreach( $productList as $productListSingle2 ) {
        $productIDs[$i] = $productListSingle2['product_id']; 
        $i++;
      }
    }
    
    // API call for stock levels of product IDs collected above
    $productStock = $soap->call($session_id, 'product_stock.list', array($productIDs));
    
    echo "<table border=\"1\">";
    echo "<tr>";
    echo "<th width=150>Varient code</th>";
    echo "<th width=350>Varient name</th>";
    echo "<th width=25>Quantity</th>";
    echo "<th width=25>Active</th>";
    echo "</tr>";
    
    echo "<tr>";
    
    
     if( is_array( $productStock ) && !empty( $productStock )) { 
      foreach( $productStock as $productStockSingle ) {
    
        echo "<tr";
    
        if ($productStockSingle['qty'] <= 5)
          echo " style=\"color:#FF0000;\"";
    
        echo ">";
    
        //Varient code
        echo "<td>" . $productStockSingle['sku'] . "</td>";
    
        //Variables to look up the varient name in the index created above
        $skuindex = $productStockSingle['sku'];
        $idindex = $productIndex[$skuindex];
    
        //print varient name using index look up values above
        echo "<td>" . $productList[$idindex]['name'] . "</td>";
    
        // varient Quantity in stock
        echo "<td>" . $productStockSingle['qty'] . "</td>";     
    
        // Is product marked as in stock (active)?
        echo "<td>" . $productStockSingle['is_in_stock'] . "</td>";
    
        echo "</tr>";
      }
     }
    
     echo "</tr>";
     echo "</table>";
    
    $soap->endSession($session_id);
      ?>
    

    这个很有用

    【讨论】:

      猜你喜欢
      • 2017-10-17
      • 2013-01-25
      • 1970-01-01
      • 2019-08-09
      • 2013-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      相关资源
      最近更新 更多