简体   繁体   English

如何在Magento中使用REST API获取产品信息

[英]HOW To Get Product Info Using REST API In Magento

How To Create REST API And How To Fetch The Magento Products Info .. 如何创建REST API以及如何获取Magento 产品信息 ..

Is there any demo available for above? 上面有可用的演示吗?

Products Specific Magento REST API Requests 产品特定的Magento REST API请求
Retrieve the list of products, create, update, or delete a product. 检索产品列表,创建,更新或删除产品。 You will call Magento REST API like this: 您将像这样调用Magento REST API:
http://www.my-magento-store.com/api/rest/products http://www.my-magento-store.com/api/rest/products

Product Categories Retrieve the list of categories assigned to the product, assign, and unassign the category to/from the specific product. 产品类别检索分配给产品的类别列表,为特定产品分配和取消分配类别。 You will call Magento REST API like this: 您将像这样调用Magento REST API:
http://www.my-magento-store.com/api/rest/products/:productId/categories http://www.my-magento-store.com/api/rest/products/:productId/categories

Product Images 产品图片
Retrieve the list of images assigned to the product, add, update, and remove an image to/from the specific product. 检索分配给产品的图像列表,添加,更新和从特定产品中删除图像。 You will call Magento REST API like this: 您将像这样调用Magento REST API:
http://www.my-magento-store.com/api/rest/products/:productId/images http://www.my-magento-store.com/api/rest/products/:productId/images

Magento REST API examples: Magento REST API示例:

$callbackUrl = "http://www.my-magento-store.com/oauth_admin.php";
$temporaryCredentialsRequestUrl = "http://www.my-magento-store.com/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://www.my-magento-store.com/admin/oauth_authorize';
$accessTokenRequestUrl = 'http://www.my-magento-store.com/oauth/token';
$apiUrl = 'http://www.my-magento-store.com/api/rest';
$consumerKey = '{Consumer Key}';
$consumerSecret = '{Consumer Secret}';

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";
        $productData = json_encode(array(
            'type_id'           => 'simple',
            'attribute_set_id'  => 4,
            'sku'               => 'simple' . uniqid(),
            'weight'            => 1,
            'status'            => 1,
            'visibility'        => 4,
            'name'              => 'My Product Name',
            'description'       => 'My Product Description',
            'short_description' => 'My Products Short Description',
            'price'             => 6.99,
            '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);
}

As you can see in the code given above, at the top you have to declare your connection, authetication tokens. 正如您在上面给出的代码中所看到的,在顶部您必须声明您的连接,验证令牌。 Since this example is using oAuth authentication you need to specify it's location on your host, consumer and secret key before you make a call to http://www.my-magento-store.com/api/rest/ . 由于此示例使用oAuth身份验证,因此您需要在调用http://www.my-magento-store.com/api/rest/之前在主机,使用者和密钥上指定它的位置。 If everything is alright you can create a JSON array of your simple product ready to push live. 如果一切顺利,您可以创建一个简单产品的JSON数组,准备推送。

Now, let's look at another example 现在,让我们看另一个例子

$callbackUrl = "http://www.my-magento-store.com/oauth_customer.php";
$temporaryCredentialsRequestUrl = "http://www.my-magento-store.com/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://www.my-magento-store.com/oauth/authorize';
$accessTokenRequestUrl = 'http://www.my-magento-store.com/oauth/token';
$apiUrl = 'http://www.my-magento-store.com/api/rest';
$consumerKey = '{Consumer Key}';
$consumerSecret = '{Consumer Secret}';

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


The above code will retrieve the list of all products as a customer via Magento REST API. 以上代码将通过Magento REST API检索所有产品的列表。 Keep in mind that authorization headers are required for Admin and Customer user types. 请记住,管理员和客户用户类型需要授权标头。 If you are enabling REST for guests you can do like this: 如果您为guest虚拟机启用REST,您可以这样做:
http://www.my-magento-store.com/api/rest/products?limit=1 http://www.my-magento-store.com/api/rest/products?limit=1

This will result in the following XML output 这将导致以下XML输出

<?xml version="1.0"?>
    <magento_api>
      <data_item>
        <entity_id>18</entity_id>
        <type_id>simple</type_id>
        <sku>SKU Number</sku>
        <description>Your Product Description
    </description>
        <meta_keyword>Meta Keywords </meta_keyword>
        <short_description>Short Description</short_description>
        <name>Product Name</name>
        <meta_title>Product Title</meta_title>
        <meta_description>Meta Desciption</meta_description>
        <regular_price_with_tax>Regular Price of the product </regular_price_with_tax>
        <regular_price_without_tax>Price without Tax</regular_price_without_tax>
        <final_price_with_tax>Final Price With Tax</final_price_with_tax>
        <final_price_without_tax>Final Price without Tax</final_price_without_tax>
        <is_saleable>1</is_saleable>
        <image_url>Path of the product image</image_url>
      </data_item>
  </magento_api>

Similarly, you can call REST API URL's to get specific XML data with limit parameter, default is 10 products per request but one request can only request a maximum of 100 products. 同样,您可以调用REST API URL来获取带有limit参数的特定XML数据,默认情况下每个请求有10个产品,但一个请求最多只能请求100个产品。 To get the next set of results call like this: 要获得下一组结果,请按以下方式调用:
http://www.my-magento-store.com/api/rest/products?page=2&limit=10 http://www.my-magento-store.com/api/rest/products?page=2&limit=10
I hope this is enough to get started with Magento REST API. 我希望这足以开始使用Magento REST API。

您可以在下面使用获取产品详细信息,这里:productId动态变量,因此您只需传递产品ID或者您也可以通过SKU

GET http://magentohost/api/rest/products/:productId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM