> 使用 Javascript
var username = "user1";
var password = "user1";
jQuery.ajax({
url : "http://domainname.com/rest/user/login.json",
type : 'post',
data : 'username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password),
dataType : 'json',
error : function(data) { console.log("error"); console.error(data); },
success : function(data) { console.log("success"); console.info(data); }
});
> 使用 PHP/Curl
// Method: POST, PUT, GET etc
// Data: array("param" => "value") ==> index.php?param=value
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
//curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
return curl_exec($curl);
}
// Example
$method = "POST";
$url = "http://domainname.com/rest/user/login.json";
$rurl = "http://domainname.com/";
$postdata = array('username'=>'editor1', 'password'=>'editor1');
$response = CallAPI($method, $url, $postdata );
print "<pre>";
print_r($response);
结果:
{"sessid":"R03y8-40VBh6FDEIfi9Lnm2kh2SikCxY3105egCynDY","session_name":"SESS2063f746500d52bace78ba433478bd2d","user": ......
我正在尝试使用两个不同的脚本登录,一个来自 JQuery,另一个来自 PHP/Curl。
当我触发 JQuery 时,它会自动登录成功。但是,当我尝试从 PHP/Curl 自动登录时,它会给我如上所述的成功响应,但是当打开 URL 时不会登录,任何人都可以解释问题所在。
根据我的假设,Session_name/Sessid 可能存储在 cookie 中?