我在尝试登录(身份验证)magento 客户时遇到问题。我找不到使用电子邮件和密码登录客户的正确方法。那么,你能建议我如何使用 SOAP API 在 magento 商店中进行身份验证或登录客户吗? 我尝试了下面给出的代码
env.dotNet = false;
env.xsd = SoapSerializationEnvelope.XSD;
env.enc = SoapSerializationEnvelope.ENC;
SoapObject request = new SoapObject(NAMESPACE, "login");
request.addProperty("username", "xyz");
request.addProperty("apiKey", "xyz");
env.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
androidHttpTransport.call("", env);
Object result = env.getResponse();
Log.d("sessionId", result.toString());
// making call to get list of customers
String sessionId = result.toString();
request = new SoapObject(NAMESPACE, "customerCustomerInfo");
request.addProperty("sessionId", sessionId);
request.addProperty("customerId", "2032");
request.addProperty("email", "[email protected]");
request.addProperty("password", "password");
env.setOutputSoapObject(request);
androidHttpTransport.call(SOAP_ACTION, env);
androidHttpTransport.debug = true;
result = env.getResponse();
Log.d("Customer List", result.toString());
}
但这对我没有帮助,所以有人能解决我的问题吗?
谢谢你....
客户没有直接登录magento的选项。您从API检索的密码是hash_password,您无法检查密码的相等性。但您可以使用下面解释的方法登录magento。
创建一个外部 php 文件并在那里访问 magento 登录名
require_once('../magentosite/app/Mage.php'); //Magento 的路径 掩码(0); 法师::app();
$id = 1;
尝试{ $result = Mage::getModel('客户/客户')->setWebsiteId($id)->authenticate($email, $password); }catch( 异常 $e ){ $结果=假; }
使用 JSON 将用户名和密码从 android 发送到该 php 页面,并获取“结果”。
如果结果为“true”,则用户名和密码存在于数据库中。
其实你可以使用SOAP来检查/登录用户,你只需要稍微扩展一下即可。在 app/code/core/Mage/Customer/Model/Customer/Api.php 添加新功能
public function login($email, $password){
/** @var $session Mage_Customer_Model_Session */
$session = Mage::getSingleton( 'customer/session' );
Mage::app()->getStore()->setWebsiteId(1);
try
{
$session->login( $email, $password );
$customer = $session->getCustomer();
return json_encode(array('status' => 'OK', 'userData' => $this->info($customer->getId())));
}
catch( Exception $e )
{
return json_encode(array('status' => 'error', 'message' => $e->getMessage()));
}
}
在app/code/core/Mage/Customer/etc/api.xml
<config>
<api>
<resources>
<customer translate="title" module="customer">
<methods>
...
<login translate="title" module="customer">
<title>Login customer</title>
<acl>customer/login</acl>
</login>
也在最后
<acl>
<resources>
<customer translate="title" module="customer">
...
<login translate="title" module="customer">
<title>Login</title>
</login>
在这里您可以测试您的登录功能
<?php
$host = "http://youmagentohost/index.php";
$client = new SoapClient($host."/api/soap/?wsdl"); //soap handle
$apiuser= "apiuser"; //webservice user login
$apikey = "apikey"; //webservice user pass
$action = "customer.login";
try {
$sess_id= $client->login($apiuser, $apikey);
$params = array('email'=>'[email protected]', 'password'=>'password');
print_r($client->call($sess_id, $action, $params));
}
catch (Exception $e) { //while an error has occured
echo "==> Error: ".$e->getMessage();
exit();
}
?>
这不是一个很好的解决方案,但总比没有好。不要忘记重写所有核心文件;)
实际上你可以使用 SOAP 检查/登录用户 在app/code/core/local/Envato/Masterapi/etc/api.xml
<?xml version="1.0"?>
<config>
<api>
<resources>
<masterapi_loginmodel translate="title" module="masterapi">
<model>masterapi/loginmodel_api</model>
<title>Demo Custommoduleapi API</title>
<acl>masterapi/loginmodel</acl>
<methods>
<!-- <list translate="title" module="masterapi">
<title>List of masterapi</title>
<method>clogin</method>
</list> -->
<customerlogin translate="title" module="masterapi">
<title>List of masterapi</title>
</customerlogin>
</methods>
</masterapi_loginmodel>
</resources>
<resources_alias>
<loginmodel>masterapi_loginmodel</loginmodel>
</resources_alias>
<v2>
<resources_function_prefix>
<loginmodel>masterapiLoginmodel</loginmodel>
</resources_function_prefix>
</v2>
<acl>
<resources>
<masterapi translate="title" module="masterapi">
<title>Loginmodel</title>
<sort_order>5</sort_order>
<loginmodel translate="title" module="masterapi">
<title>loginmodel data</title>
</loginmodel>
</masterapi>
</resources>
</acl>
</api>
</config>
在app/code/core/local/Envato/Masterapi/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Mycustom_Masterapi>
<version>1.0</version>
</Mycustom_Masterapi>
</modules>
<global>
<models>
<masterapi>
<class>Mycustom_Masterapi_Model</class>
</masterapi>
</models>
<helpers>
<masterapi>
<class>Mycustom_Masterapi_Helper</class>
</masterapi>
</helpers>
</global>
</config>
在app/code/core/local/Envato/Masterapi/etc/wsdl.xml
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns:typens="urn:{{var wsdl.name}}" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/"
name="{{var wsdl.name}}" targetNamespace="urn:{{var wsdl.name}}">
<types>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:Magento">
<import namespace="http://schemas.xmlsoap.org/soap/encoding/" schemaLocation="http://schemas.xmlsoap.org/soap/encoding/" />
<complexType name="fieldInfo">
<sequence>
<element name="entity_id" type="xsd:string"/>
<element name="name" type="xsd:string"/>
</sequence>
</complexType>
<complexType name="fieldInfoArray">
<complexContent>
<restriction base="soapenc:Array">
<attribute ref="soapenc:arrayType" wsdl:arrayType="typens:fieldInfo[]" />
</restriction>
</complexContent>
</complexType>
</schema>
</types>
<message name="masterapiLoginmodelListRequest">
<part name="sessionId" type="xsd:string" />
<part name="user" type="xsd:string" />
<part name="pass" type="xsd:string" />
</message>
<message name="masterapiLoginmodelListResponse">
<part name="customlogin" type="xsd:string" />
</message>
<portType name="{{var wsdl.handler}}PortType">
<operation name="masterapiLoginmodelCustomerlogin">
<documentation>List of masterapi</documentation>
<input message="typens:masterapiLoginmodelListRequest" />
<output message="typens:masterapiLoginmodelListResponse" />
</operation>
</portType>
<binding name="{{var wsdl.handler}}Binding" type="typens:{{var wsdl.handler}}PortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="masterapiLoginmodelCustomerlogin">
<soap:operation soapAction="urn:{{var wsdl.handler}}Action" />
<input>
<soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
</binding>
<service name="{{var wsdl.name}}Service">
<port name="{{var wsdl.handler}}Port" binding="typens:{{var wsdl.handler}}Binding">
<soap:address location="{{var wsdl.url}}" />
</port>
</service>
</definitions>
在应用程序/代码/本地/Mycustom/Masterapi/Model/Loginmodel/Api.php
<?php
// app/code/local/Mycustom/Masterapi/Model/Loginmodel/Api.php
class Mycustom_Masterapi_Model_Loginmodel_Api
{
public function customerlogin($user,$pass)
{
/** @var $session Mage_Customer_Model_Session */
$session = Mage::getSingleton( 'customer/session' );
Mage::app()->getStore()->setWebsiteId(1);
try
{
$session->login( $user, $pass);
$customer = $session->getCustomer();
return json_encode(array('status' => 'valid', 'userData' => $customer->getId()));
}
catch( Exception $e )
{
return json_encode(array('status' => 'invalid', 'userData' => $e->getMessage()));
}
}
}
在/app/code/local/Mycustom/Masterapi/Model/Loginmodel/Api/V2.php
<?php
//app/code/local/Envato/Customapimodule/Model/Product/Api/V2.php
class Mycustom_Masterapi_Model_Loginmodel_Api_V2 extends Mycustom_Masterapi_Model_Loginmodel_Api
{
}
在安卓端: 致电肥皂请求:
properties.put("sessionId", sessionId);
properties.put("user", "[email protected]");
properties.put("pass","pass");
request = new SoapObject(NAMESPACE, "masterapiLoginmodelCustomerlogin");
env.setOutputSoapObject(request);
androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call("", env);
以客户身份登录的最简单方法是:
这是代码:
email = "[email protected]";
string_password = "xyz@123";
request = new SoapObject(NAMESPACE, "customerCustomerList");
request.addProperty("sessionId",sessionId );
env.setOutputSoapObject(request);
androidHttpTransport.call("urn:Magento/customerCustomerList", env);
SoapObject response = (SoapObject) env.getResponse();
for(int i=1;i<lengthofResponse;i++)
{
SoapObject CustomerList = (SoapObject)response.getProperty(i);
Object email_id=CustomerList.getProperty(6);
int CustomerList_length = CustomerList.getPropertyCount();
Object password=CustomerList.getProperty((CustomerList_length));
selected_email_ids.add(email_id.toString());
response_password=(password.toString().split(":"));
selected_password.add(response_password[0]);
selected_salt.add(response_password[1]);
//comparing password
if(email.equals(selected_email_ids.get(j)))
{
System.out.println("Email is currect");
hash = md5( response_password[1] + string_password);
if(hash.equals(selected_password.get(j)))
{
System.out.println("Welcome");
}
}
}