使用 magento(1.7.0.2) 的soap api 更新用户信息的时候发现不能够更新用户的密码。
我们首先看一下 如何调用api 更新用户信息(参考自:magentocommerce)
<?php
$api_url_v1 = "http://192.168.1.162/trunk/index.php/api/soap/?wsdl";
$username = 'moshi';
$password = 'moshiPass';
//连接 SOAP
$client = new SoapClient($api_url_v1);
//获取登入后的 Session ID
$session_id = $client->login($username, $password);
//调用 API 中的方法
$result = $client->call(
$session_id, 'customer.update', array(
'customerId' => '70', 'customerData' => array('firstname' => 'mo',
'lastname' => 'shi', 'email' => 'moshi@moshi.com',
'group_id' => '10', 'password' => '123456')));
var_dump ($result);
?>
但是仅仅这么做更新不了用户的密码。
网上的做法:
找到文件
app\code\core\Mage\Customer\Model\Customer\Api.php
修改 update 方法 在 代码
$customer->save(); 上面添加代码:
if(isset($customerData['password'])){
$customer->setPassword($customerData['password']);
}
当然最好重写这个模块。
我采用的方法是:
修改 调用api 的 call 函数
把其中的 password 修改为 password_hash 数据即可(当然是加密后的)
我们 可以在 api 调用magento 的update 方法中 找到代码:
foreach ($this->getAllowedAttributes($customer) as $attributeCode=>$attribute) {
if (isset($customerData[$attributeCode])) {
$customer->setData($attributeCode, $customerData[$attributeCode]);
}
}
这里其实就是根据 被允许的属性来决定哪些属性的值是可以被修改的。
打印 $attributeCode 发现 并没有password 所以直接传送 password 数据这么做是不可行的。
但是我们可以看到 我们是可以修改 pasword_hash 这个值的
当然修改这个值,你首先需要知道 你的magento 系统 加密方式是怎样的。
假设没有更改过,默认的加密方式是 md5(key+password)
所以 你只要在 调用 $client->call() 之前得出 password_hash 值即可。