2012-03-06 123 views
0

我有一个应用程序,允许用户通过Facebook登录,并且所有已注册的用户都有他们的Facebook ID存储在我们的数据库中,以便我们知道谁有和谁没有没有注册我们的应用程序。非常标准的东西,这部分工作正常,并已经过测试等。通过PHP从mySQLi数据库检索比较数据到Xcode

接下来,我需要让用户看到他们的哪些朋友已经有应用程序(即哪些已注册)。

我从Facebook上得到他们的朋友列表,制作一个包含所有用户ID的JSON字符串,现在我需要知道如何将这个JSON字符串发送给我的PHP脚本,并将它与所有用户的JSON进行比较该数据库已经存在,并且创建了在JSON字符串和数据库中都可以找到的ID数组。 然后,我需要通过PHP将该数组发送回应用程序(大概再次作为JSON),以便我可以从Facebook朋友的用户列表中筛选出未注册的ID。

要我目前使用以下格式在该应用发送请求注册用户:

NSString *host = @"my.domain"; 
NSString *urlString = [@"myPHP.php?"stringByAppendingString:@"task=register"]; 
//list of args which are sent 
NSURL *url = [[NSURL alloc] initWithScheme:@"http" host:host path:urlString]; 
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; 
NSError *requestError = nil; 
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&requestError]; 

在我的PHP我使用这些方针的东西(我离开了,我连接到部分数据库等,为所有的作品和我想象中是所有应用程序的标准):

function __construct($db, $args=NULL) { 
    $this->db = $db; 
    $this->checkSetup(); 
    $this->apnsData = array(
     'production'=>array(
      'certificate'=>$this->certificate, 
      'ssl'=>$this->ssl, 
      'feedback'=>$this->feedback 
     ), 
     'sandbox'=>array(
      'certificate'=>$this->sandboxCertificate, 
      'ssl'=>$this->sandboxSsl, 
      'feedback'=>$this->sandboxFeedback 
     ) 
    ); 
    if(!empty($args)){ 
     switch($args['task']){ 
      case "register": 
       $this->_registerDevice(
        $args['appname'], 
        $args['devicetoken'], 
        $args['facebookID'] 
       ); 
       break; 
       //etc 

正如我所说的这一切工作正常,我用其他功能类似的东西。然而,这些功能都不需要将任何内容发送回应用程序本身,所以我会非常感谢在此接下来要做的任何帮助。

要总结一下我卡上:

  • 发送JSON字符串PHP脚本。
  • 将所述JSON与包含mySQLi数据库中一列的所有条目的数组进行比较。
  • 将匹配条目数组返回给应用程序。

我会很感激任何人可以给予的帮助,因为我过去对PHP或mySQL做的工作很少,所以我不确定下一步该去哪里。

谢谢大家:-)

编辑:我只是试着做一个假的URL请求,在浏览器中运行,只是为了看看会发生什么,当我发送一个JSON字符串到PHP代码(我只是有它回应结果)。我得到了一个414错误(URI太长)。这是否意味着这不会是一种合适的方法来检索我想要的结果?我在JSON字符串中有大约200个朋友的ID,但是许多人的数字远不止这些。

编辑2:继已经离开了意见,就这样的事情是发送数据的一个更合适的方法:

NSString *jsonString = [friendsIDArray JSONRepresentation]; 
NSMutableURLRequest *mrequest = [[NSMutableURLRequest alloc] init]; 
NSString *post = [NSString stringWithFormat:@"json=%@", jsonString]; 
NSLog(post); 


NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO]; 

[mrequest setURL:[NSURL URLWithString:@"http://plantpot.co/app_Boom_php/apns.php"]]; 
[mrequest setHTTPMethod:@"POST"]; 

[mrequest setHTTPBody:postData]; 

[[NSURLConnection alloc] initWithRequest:mrequest delegate:self]; 
+0

尝试POSTING而不是GET。使用GET可以在查询字符串中推送多少数据是有限制的。 – 2012-03-06 19:33:44

+0

测试请求对多个用户需要多长时间,即使使用POST,您也可能需要将其拆分为多个请求 – miki 2012-03-06 20:00:08

+0

那么NSURLRequest方法是否是GET方法呢? – TheBestBigAl 2012-03-06 20:48:04

回答

0

设法得到它的工作到底使用GET。

NSString *jsonString = [friendsIDArray JSONRepresentation]; 

//send this to the server 
NSString *friendHost = @"ourdomain.com"; 
NSString *friendurlString = [@"/thephp.php?"stringByAppendingString:@"task=getRegistered&jsonList=%@", jsonString]; 
NSURL *friendURL = [[NSURL alloc] initWithScheme:@"http" host:friendHost path:friendurlString]; 
NSURLRequest *friendRequest = [[NSURLRequest alloc] initWithURL:friendURL]; 
NSError *friendRequestError = nil; 

//the reply is the list of ID's which are NOT registered on the database 
NSData *friendReturnData = [NSURLConnection sendSynchronousRequest:friendRequest returningResponse:nil error:&friendRequestError]; 

NSString *stringReply = (NSString *)[[NSString alloc] initWithData:friendReturnData encoding:NSUTF8StringEncoding]; 

//convert this string to an array 
NSArray *unregisteredFriends = [stringReply JSONValue]; 

然后在PHP脚本中,我做了以下内容:

function _returnRegisteredFriends($jsonArg) 
{ 

    $db = new DbConnect(); 
    $column = array(); 
    $result = $db->query("SELECT facebookID FROM registered_users"); 

    //populate column array with all of the facebook user IDs 
    while($row = $result->fetch_array(MYSQLI_ASSOC)){ 
     $column[] = $row['facebookID']; 
    } 


    $newphrase = str_replace('\\', '', $jsonArg);//remove escape characters 
    $decoded = json_decode($newphrase);//from json to array 
    $arrayresult = array_diff($decoded, $column); 
    echo json_encode($arrayresult); 
} 

这得到所有用户的ID从我的数据库,并将其与通过它($ jsonArg)传递一个数组。然后区别出来,然后echo将它作为friendReturnData返回给应用程序。

由于那些建议使用POST方法的家伙,所有人似乎都在工作,结果发现GET方法令人满意。

0
-(IBAction)Btn_Login:(id)sender 
{ 
    NSString *Post = [NSString stringWithFormat:@"email=%@&password=%@", Text_Email.text, Text_Pass.text]; 

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@LoginTest.php?%@", ServerPath, Post]]; 

    NSData *Returndata = [NSData dataWithContentsOfURL:url]; 

    NSString *Response = [[NSString alloc] initWithData:Returndata encoding:NSUTF8StringEncoding]; 

    Response = [Response stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

    NSLog(@"%@",Response); 
}