這應該可行,考慮到您將PHPSESSID與您的請求一起發送。正如你所說你正在使用cURL,情況可能並非如此。
您可以輕鬆地實現通過調用Ajax的API,類似於下面的內容:
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';
}
(源捲曲請求:https://community.magento.com/t5/Programming-Questions/REST-API-call-to-get-logged-in-customer-ID/m-p/62092#M1813)