2014-02-11 133 views
0

我跑Magento的示例代码的REST API (http://www.magentocommerce.com/api/rest/introduction.html添加新产品

我实现了正确的认证,与认证令牌,和POST之后我得到的反馈使用此消息正确上传:

Array ( 
    [url] => http://magentohost.pt/magento/api/rest/products 
    [http_code] => 200 
    [download_content_length] => 37 
    [content_type] => text/html 
    [size_download] => 37 
    [size_upload] => 250 
) 

但是,发送的数据不会出现在数据库/ magento中。

在Magento API REST页面的任何示例代码中,我都会与API进行通信,但不会与数据库的产品/客户数据发生更改。

我在做什么错?

回答

0

这是如何使用REST API创建产品

<?php 
/** 
* Example of simple product POST using Admin account via Magento REST API. OAuth authorization is used 
*/ 
$callbackUrl = "http://yourhost/oauth_admin.php"; 
$temporaryCredentialsRequestUrl = "http://magentohost/oauth/initiate?oauth_callback=" . urlencode($callbackUrl); 
$adminAuthorizationUrl = 'http://magentohost/admin/oauth_authorize'; 
$accessTokenRequestUrl = 'http://magentohost/oauth/token'; 
$apiUrl = 'http://magentohost/api/rest'; 
$consumerKey = 'yourconsumerkey'; 
$consumerSecret = 'yourconsumersecret'; 

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'    => 'Simple Product', 
      'description'  => 'Simple Description', 
      'short_description' => 'Simple Short Description', 
      'price'    => 99.95, 
      '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); 
} 

参考:http://www.magentocommerce.com/api/rest/introduction.html#RESTAPIIntroduction-CreateasimpleproductasanAdminuserwithOAuthauthentication

+0

我很抱歉,但是这是不是非常有帮助,因为这是我已经使用做出POST代码就像我在我的问题中所说的那样。 –

+0

您是否为这两个变量$ consumerSecret,$ consumerKey提供了正确的值? –

+0

证书看起来是正确的,因为使用chrome的休息客户端扩展并使用相同的OAuth授权进行GET请求可以正常工作。还要注意,我用这个例子得到的响应状态代码是200,所以一切似乎都奏效了。但是,数据库或系统中的任何其他位置不会有明显变化。 –

相关问题