2012-03-06 122 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); 
}