如何使用 AFNetworking 2.0 检查 Wordpress 登录信息?

Posted

技术标签:

【中文标题】如何使用 AFNetworking 2.0 检查 Wordpress 登录信息?【英文标题】:How do I check Wordpress login information using AFNetworking 2.0? 【发布时间】:2014-04-05 15:47:30 【问题描述】:

目前我正在尝试使用wp_check_password 来简单地返回10 来表示登录是否有效。但是,每当我尝试 require_once 'wp-blog-header.php' 或任何其他 php 文件时,结果都是:

Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: not found (404)" UserInfo=0xf6c4720 NSErrorFailingURLKey=http://www.**site**.com/BBT/appLogin.php, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xf6a5a40>  URL: http://www.**site**.com/BBT/appLogin.php   status code: 404, headers 
"Cache-Control" = "no-cache, must-revalidate, max-age=0";
Connection = close;
"Content-Encoding" = gzip;
"Content-Length" = 21;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Sat, 05 Apr 2014 15:37:26 GMT";
Expires = "Wed, 11 Jan 1984 05:00:00 GMT";
Pragma = "no-cache";
Server = "Apache/2.2.25 (Unix) mod_ssl/2.2.25 OpenSSL/1.0.0-fips mod_bwlimited/1.4";
Vary = "Accept-Encoding,User-Agent";
"X-Pingback" = "http://www.**site**.com/wp/xmlrpc.php";
"X-Powered-By" = "PHP/5.3.26"; , NSLocalizedDescription=Request failed: not found (404)

我的xcode函数是:

-(void) submitLogin 
//check information online either show alert or seque to management.
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Info"
                                                  message:@""
                                                 delegate:nil
                                        cancelButtonTitle:@"OK"
                                        otherButtonTitles:nil];

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager manager] initWithBaseURL:[self.globalVars appBaseStringAsURL]];

manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

NSDictionary *params = @@"username": self.usernameTextField.text,
                         @"password": self.passwordTextField.text;
[manager POST:[self.globalVars appLoginString] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) 

    NSLog(@"%@",[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
    BOOL success = [[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding] boolValue];

    if (success) 
        [self performSegueWithIdentifier:@"toEventManagement" sender:self];
     else 
        [message setMessage:[NSString stringWithFormat:@"Incorrect login information"]];
        [message show];
    
 failure:^(AFHTTPRequestOperation *operation, NSError *error) 
    NSLog(@"%@",error);

    [message setMessage:[NSString stringWithFormat:@"Error connecting to server"]];
    [message show];
];

我的php代码是:

<?php

    $username = $password = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") 
        require_once('../wp/wp-blog-header.php');

        $username = clean_data($_POST['username']);
        $password = clean_data($_POST['password']);

        $user = get_user_by('login',$username);

        if ( $user and wp_check_password($password, $user->data->user_pass, $user->ID) )
            echo 1;
         else 
            echo 0;
        
      
?>

当然,如果有人能指导我找到更好的方法来完成这项工作,我将不胜感激。

编辑:同时我将使用异步 NSURLConnection

【问题讨论】:

【参考方案1】:

您是否尝试过 require_once 'wp-load.php' ?那对我有用。我也尝试了 wp-blog-header.php 并得到了同样的错误。 wp-load.php 似乎工作。

【讨论】:

谢谢,以后会记住的

以上是关于如何使用 AFNetworking 2.0 检查 Wordpress 登录信息?的主要内容,如果未能解决你的问题,请参考以下文章

使用 AFNetworking 2.0 如何使用 JSON 测试我的 POST 请求的大小?

带有 OAuth2 令牌的 AFNetworking 2.0

AFNetworking 2.0:isReachable 总是返回 False

如何在旧的 AFNetworking 中使用 AFNetworking 2.0+?

如何使用 AFNetworking-2.0 执行 JSON 编码的 GET 请求?

如何使用 AFNetworking 2.0 下载文件