duanjunjie0196 2012-11-18 11:58
浏览 101
已采纳

在Magento中创建新客户(RESTful,PHP)

I am trying to create a new customer in Magento. Here's PHP script I wrote for that

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

I am getting this error

OAuthException: Invalid auth/bad request (got a 405, expected HTTP/1.1 20X or a redirect) in C:\wamp\www\magento\WebServices ewcustomer1.php on line 47 Call Stack #TimeMemoryFunctionLocation 10.0010386016{main}( ).. ewcustomer1.php:0 20.0017391216OAuth->fetch( ).. ewcustomer1.php:47 )

I am new to Oauth and PHP. Will be glad if someone can help.

  • 写回答

3条回答 默认 最新

  • duanmen8491 2014-08-06 13:06
    关注

    This custom API I wrote works like a breeze. You will need to create a SOAP user from Magento Admin Panel and add SOAP user & DB credentials in following code to make it work.

    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());
         }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况