2012-11-18 5 views
3

Magento에서 새로운 고객을 만들려고합니다. 여기에 내가 그Magento (RESTful, 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'  => '[email protected]', 
      '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); 
} 
?> 

나는이 오류를 얻고 위해 쓴 PHP 스크립트입니다

OAuthException : 잘못된 인증/잘못된 요청 (있어 405, HTTP/1.1 20 배 또는 리디렉션 예상) C의를 : \ wamp \ www \ magento \ WebServices \ newcustomer1.php on line 47 호출 스택 #TimeMemoryFunctionLocation 10.0010386016 {주} ( ) .. \ newcustomer1.php : 0 20.0017391216OAuth-> fetch ( ) .. \ newcustomer1.php : 47)

Oauth와 PHP를 처음 사용합니다. 누군가가 도울 수 있다면 기쁠 것입니다. 주변에 라인 (36)

답변

0

맞춤 API 나는 바람과 같은 작품을 썼다. Magento Admin Panel에서 SOAP 사용자를 만들고 SOAP 사용자 & DB 자격 증명을 다음 코드에 추가하여 작동하게해야합니다.

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

:

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

가 마지막에 create를 제거합니다. 그래서 지금 :

$resourceUrl = "$apiUrl/customers/"; 

은 이미 REST이 동사를 지정 OAUTH_HTTP_METHOD_POST 있습니다. 그 결과로 생성되는 리소스 URL은 http://localhost/magento/api/rest/customers/

이어야합니다. HTTP 405 Method Not Allowed은 끝 점이 POST을 허용하지 않거나 알 수 없다는 의미입니다. 귀하의 경우, 그것은 후자입니다.

여기에 자원 엔드 포인트에 대한 자세한 내용 : http://www.magentocommerce.com/api/rest/Resources/resources.html

+0

나머지 고객은 out oauth를 사용하여 추가 할 수 있습니까? – zamil

+0

magento에 내장 된 REST API를 사용하고 있습니까? __no .__ 자신의 REST API를 롤아웃하거나 Inchoo_Api를 사용하는 경우 예/아마도. –

+0

정보 주셔서 감사합니다. – zamil

0

가 공식 젠토 휴식 API는 사용자가 관리자 권한으로 고객을 만들 수 있습니다. 관리자 권한을 사용하여이를 수행하거나 게스트 권한을위한 사용자 정의 권한을 구현하십시오.