> 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 からの 2 つの異なるスクリプトを使用してログインしようとしています。
JQueryを起動すると、自動ログインが正常に機能します。しかし、PHP/Curl から自動ログインしようとすると、上記のように成功の応答が返されますが、URL を開いてログインしないと、誰でも問題の原因を説明できます。
私の仮定によれば、Session_name/Sessid が Cookie に保存される可能性はありますか?