Get magento product feed using Mangento rest api but OAuth error [closed]

有些话、适合烂在心里 提交于 2019-12-25 03:07:08

问题


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.

I am using this code

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

 ?>

But I am facing this error

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

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


回答1:


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

this is very useful



来源:https://stackoverflow.com/questions/21542947/get-magento-product-feed-using-mangento-rest-api-but-oauth-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!