キー、sha1、または好みのアルゴリズムを作成します。Web サービスが https 経由でのみアクセスできることを確認してから、キーをリクエスト ヘッダーに埋め込みます。はい ASIHTTPRequest は減価償却されていますが、AFNetworking は優れた代替品https://github.com/AFNetworking/AFNetworkingです。PHP 側で、最初にヘッダーの認証キーが有効であることを確認します。有効な場合は、POST 経由で渡されたユーザー名とパスワードの検証に進みます。これで問題が解決するはずです。まだサポートが必要な場合は、明日コードを投稿できます...
更新 - 以下のコードを追加
これを試して...
// setup httpClient
NSURL *baseURL = [NSURL URLWithString:@"https://www.yourWebsite.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
// setup required wsapi parameters
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:username_ forKey:@"username"];
[parameters setObject:password_ forKey:@"password"];
request = [httpClient requestWithMethod:@"POST" path:@"/path/to/your/webservice/authFile.php" parameters:parameters];
// setup request headers
[request setValue:@"68489233957fd9028kd9adf40119c1c93a98c00b80h94lk2jsdo9234720" forHTTPHeaderField:@"wsapiAccessToken"];
// setup request operation blocks
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *dictionary = [json_ parseJSONResponse:operation.responseData];
// do something with your response here...
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// handle error
}];
[operation start];
サーバー側では、PHPを使用してこのようなものを使用します...
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/wsapi/functions/authFunctions.php');
if(accessTokenIsValid())
{
if((isset($_POST['username'])) && (isset($_POST['password'])))
{
// validate passed username and password and do whatever else you need to do.
// be sure to respond back with something so that your app can do something
// useful with the response like give access to the app on success or show
// an error message on failure
}
else
{
$responseArray = array('authResponse'=>'__PARAMSERROR__');
echo json_encode($responseArray);
exit;
}
}
else
{
$responseArray = array('authResponse'=>'__AUTHERROR__');
echo json_encode($responseArray);
exit;
}
?>
ここaccessTokenIsValid
では、以下に示す関数を使用していることに注意してください。
function getWsapiAccessToken()
{
$tokenString = "Some string here";
// add some salt if you like - make this harder to guess
return sha1($tokenString);
}
function accessTokenIsValid()
{
if( $_SERVER['HTTP_WSAPIACCESSTOKEN'] == getWsapiAccessToken() )
{
return true;
}
else
{
return false;
}
}
お役に立てれば!