2016-11-22 68 views

回答

-1

所以你必須只通過產品ID您可以在下面使用GET產品的細節,在這裏:productId動態變量,或者您也可以通過SKU

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

1

產品的具體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); 
} 

正如你可以在上面給出的代碼中看到,你必須聲明你的連接上,認證令牌。由於此示例使用oAuth身份驗證,因此在撥打http://www.my-magento-store.com/api/rest/之前,您需要在主機,客戶和密鑰上指定它的位置。如果一切正常,您可以創建一個簡單產品的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客戶。請記住,管理員和客戶用戶類型需要授權標頭。如果要爲客人實現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。

相關問題