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

标签 rest api magento oauth product

如何创建REST API以及如何获取Magento 产品信息..

上面有可用的演示吗?

最佳答案

产品特定的 Magento REST API 请求
检索产品列表,创建、更新或删除产品。您将像这样调用 Magento REST API:
http://www.my-magento-store.com/api/rest/products

产品类别 检索分配给产品的类别列表,向特定产品分配类别以及从特定产品取消分配类别。您将像这样调用 Magento REST API:
http://www.my-magento-store.com/api/rest/products/:productId/categories

产品图片
检索分配给产品的图像列表,向特定产品添加、更新和删除图像。您将像这样调用 Magento REST API:
http://www.my-magento-store.com/api/rest/products/:productId/images

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

正如您在上面给出的代码中看到的,您必须在顶部声明您的连接、身份验证 token 。由于此示例使用 oAuth 身份验证,因此您需要在调用 http://www.my-magento-store.com/api/rest/ 之前指定它在主机、使用者和 key 上的位置。 。如果一切正常,您可以创建一个简单产品的 JSON 数组,准备好推送。

现在,让我们看另一个例子

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


上面的代码将通过 Magento REST API 以客户身份检索所有产品的列表。请记住,管理员和客户用户类型需要授权 header 。如果您为访客启用 REST,您可以这样做:
http://www.my-magento-store.com/api/rest/products?limit=1

这将产生以下 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>

同样,您可以调用 REST API URL 来获取带有限制参数的特定 XML 数据,默认每次请求 10 个产品,但一次请求最多只能请求 100 个产品。要获取下一组结果,请像这样调用:
http://www.my-magento-store.com/api/rest/products?page=2&limit=10
我希望这足以开始使用 Magento REST API。

关于rest - 如何在 Magento 中使用 REST API 获取产品信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40734545/

相关文章:

rest - 如何为Yii2-basic-template创建REST API

rest - 访问 grails 4.0.1 中的 REST Web 服务

api - 如何使用 Twitter 搜索 API 返回与我的搜索查询匹配的所有推文,仅在最近五秒内发布?

magento - Google sitemap.xml 和重复内容问题?

magento - 如何通过代码编辑magento主页?

Magento 管理 URL 不起作用 : 404 after changing it, 无法返回

javascript - javascript中的JSON解析问题

java - ReSTLet 中的 session ?

java - 从 Java 计算的 HMAC 值与 Ruby 代码不匹配

c - 设计一个带有编译时选项的 API,以删除大多数函数的第一个参数并使用全局参数