3

我正在尝试在 Magento 中创建一个新客户。这是我为此编写的 PHP 脚本

<?php
$callbackUrl = "http://localhost/magento/webservices/NewCustomer1.php";
$temporaryCredentialsRequestUrl = "http://localhost/magento/oauth/initiate?oauth_callback=". urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://localhost/magento/admin/oauth_authorize';
$accessTokenRequestUrl = 'http://localhost/magento/oauth/token';
$apiUrl = 'http://localhost/magento/api/rest';
$consumerKey = 's3xt7w8lwhfrrfzrfvwm3lrilkf66d5n';
$consumerSecret = 'vr3eq1x899pz1cf4zzxjzx3q03t66r3n';


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/customers/create";
        $customerData = json_encode(array(
            'firstname'           => 'demofirstname',
            'lastname'            => 'demolastname',
            'email_address'       => 'demo@gmail.com',
            'password'            => 'demo1234',
            'confirmation'        => 'demo1234',

        ));
        $headers = array('Content-Type' => 'application/json');
        $oauthClient->fetch($resourceUrl, $customerData, OAUTH_HTTP_METHOD_POST, $headers);
        print_r($oauthClient->getLastResponseInfo());
    }
} catch (OAuthException $e) {
    print_r($e);
}
?>

我收到此错误

OAuthException:第 47 行调用堆栈#TimeMemoryFunctionLocation 10.0010386016{main}( ) 中的 C:\wamp\www\magento\WebServices\newcustomer1.php 中的无效身份验证/错误请求(获得 405,预期 HTTP/1.1 20X 或重定向)。 .\newcustomer1.php:0 20.0017391216OAuth->fetch()..\newcustomer1.php:47)

我是 Oauth 和 PHP 的新手。如果有人可以提供帮助,我会很高兴。

4

3 回答 3

2

在第 36 行附近:

 $resourceUrl = "$apiUrl/customers/create";

去掉create最后的。现在是这样:

 $resourceUrl = "$apiUrl/customers/";

您已经必须OAUTH_HTTP_METHOD_POST为 REST 指定此动词。所以生成的资源 url 应该是:http://localhost/magento/api/rest/customers/

您收到的错误HTTP 405 Method Not Allowed意味着您的端点不允许POST或不知道它。在你的情况下,是后者。

在此处阅读有关资源端点的更多信息:http: //www.magentocommerce.com/api/rest/Resources/resources.html

于 2012-12-24T13:09:19.667 回答
0

官方的 magento rest api 只允许您创建具有管理员权限的客户。使用管理员权限来做到这一点,或者只是为来宾权限实现一个自定义权限。

于 2013-12-06T05:11:27.830 回答
0

我编写的这个自定义 API就像轻而易举。您需要从 Magento 管理面板创建一个 SOAP 用户,并在以下代码中添加 SOAP 用户和数据库凭据以使其工作。

require_once('../app/Mage.php' );
    $dbhost = 'localhost';
    $dbUsername = 'root';
    $dbPassword = 'password';
    $dbName = 'magento';
    $soapUsername = 'soapuser';
    $soapPassword = 'password'; 
    Mage::app("default");
    $store = Mage::app()->getStore();
    $storeId = $store->getId();
    $websiteId = Mage::app()->getStore()->getWebsiteId();
    $email = $_POST['email'];
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $password = $_POST['password'];
    $proxy = new SoapClient('http://yourdomain/magento/index.php/api/?wsdl'); // TODO : change url
    $session = $proxy->login($soapUsername, $soapPassword);
    try{
    $result = $proxy->call($session,'customer.create',array(array('email' => "$email", 'firstname' => "$firstname", 'lastname' => "$lastname", 'password' => "$password", 'website_id' => "$websiteId", 'store_id' => "$storeId", 'group_id' => 1)));
    echo json_encode("New Customer created with Id-$result");
    $custId = $result;
    $shoppingCartId = $proxy->call($session, 'cart.create', array(1));
    $customer = array(
        'firstname' => "$firstname",
        'lastname' => "$lastname",
        'email' => "$email",
        'password' => md5("$password"),
        'customer_id' => "$custId",
        'mode' => 'customer',
        'website_id' => "$websiteId", 
        'store_id' => "$storeId",
        'group_id' => 1
    );
    $resultCustomerSet = $proxy->call($session, 'cart_customer.set', array( $shoppingCartId, $customer, $storeId) );
    $db_handle = mysql_connect($dbhost, $dbUsername, $dbPassword) or die(mysql_error());
    $db_found = mysql_select_db($dbName, $db_handle);
    mysql_query("UPDATE `sales_flat_quote` SET `is_active` = '1' WHERE `customer_email` = '$email' ");
    mysql_query("INSERT INTO `wishlist` VALUES('','$custId','0','','$timestamp')");
    }
    catch( Exception $e ) {
     echo json_encode($e->getMessage());
     }
于 2014-08-06T13:06:54.153 回答