Magento 2 REST API调用以登录客户ID



我想从Magento外部(但在同一域)进行休息电话,以获取当前登录的客户ID。我不希望他们必须再次登录或提供密码,我只需要获取他们的ID,这样我就可以根据其ID重定向它们。

我在URL中看到了这个终点:

http://.../rest/V1/customers/me

但是当我卷曲那个URL时,我会得到:

Consumer is not authorized to access %resources

即使它是匿名的,并且基于会话,我仍然需要获得令牌才能访问它吗?如果是这样,这个PHP调用是什么样的?

我只需要证明他们已登录并抓住他们的ID即可。

应该有效,考虑到您将phpsessid与您的请求一起发送。正如您所说的,您正在使用卷发,可能不是这样。

您可以通过对API进行AJAX调用来轻松实现这一目标,这与以下几个类似:

jQuery.ajax({
    url: 'http://dev.magento2.com/rest/V1/customers/me',
    type: 'get',
    error: function() {
        alert('User not Logged In');
    },
    success: function() {
        alert('User logged in');
    }
});

如果您需要将请求保留在服务器端,则应将phpsessid添加到您的请求中:

$ch = curl_init('http://dev.magento2.com/rest/V1/customers/me');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_COOKIE, 'PHPSESSID=' . $_COOKIE['PHPSESSID']);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$json = json_decode($result);
if (isset($json->id)) {
    echo 'User logged in';
} else {
    echo 'User not logged in';
}

(curl请求的来源:https://community.magento.com/t5/programming-questions/rest-api-call-call-call-call-to-get-to-logged-in-customer-id/m-d/m-p/62092#m1813)

最新更新