考虑到您将 PHPSESSID 与请求一起发送,这应该可行。正如您所说,您正在使用 cURL,但情况可能并非如此。
您可以通过对 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-to-get-logged-in-customer-ID/m-p/62092#M1813 https://community.magento.com/t5/Programming-Questions/REST-API-call-to-get-logged-in-customer-ID/m-p/62092#M1813)