使用Mangento Rest API获取Magento产品Feed,但OAuth错误 [英] Get magento product feed using Mangento rest api but OAuth error

查看:99
本文介绍了使用Mangento Rest API获取Magento产品Feed,但OAuth错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个核心php网站,客户端在其中创建其他类型的小部件,以图表的形式获取报告. 我想为此目的开发小部件,以使用Magento商店API在php站点上显示Magento订单报告,销售报告等. 有关magento Api的任何人都可以获取产品信息,请告诉我.

I have a core php website where client create a different type of widgets to get reporting in the form of charts. I want to develop widget for that purpose to show Magento orders reporting , sales reporting etc on php site using Magento store API. Anyone know about magento Api to get product information please tell me.

我正在使用此代码

  <?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类的帮助.而什么才是获得我工作的最佳解决方案. 谢谢

Plese help where from add OAuth class. And what will be the best solution to get my job. thanks

推荐答案

使用Soap Api展示Magento商店中的产品是如此简单且省力.

Use Soap Api to show products from Magento store this is so easy and pain less.

<?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);
  ?>

这非常有用

这篇关于使用Mangento Rest API获取Magento产品Feed,但OAuth错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆