我有一个应用程序,允许用户通过Facebook登录,所有注册的用户都将他们的Facebook ID存储在我们的数据库中,这样我们就可以知道谁注册了我们的应用程序,谁没有注册。非常标准的东西,这部分都很好,已经过测试等。
接下来,我需要让用户看到他们的哪些朋友已经拥有该应用程序(即哪些朋友已经注册)。
我从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中,我使用了以下内容(我将省略连接到DB等的部分,因为这一切都很有效,我认为这是所有应用程序的标准):
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请求在浏览器中运行,只是想看看当我向PHP代码发送JSON字符串时会发生什么(我只是让它回显结果)。我得到一个414错误(URI太长)。这是否意味着这不是检索我想要的结果的合适方法?我在JSON字符串中有大约200个朋友的ID,但很多人的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];
最终使用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方法令人满意。
-(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);
}