在我的iOS應用程序代碼中,我傳遞了一個NSBictionary在HTTPBody 所以我的PHP可以檢查用戶是否存在於數據庫中。PHP break'file_get_contents'into array
-(void)checkUser
{
NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:
sessionId, @"sessionId",
sessionName, @"sessionName",
nil];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userDict
options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
// initialize and open the stream
NSURL *url = [NSURL URLWithString:@"http://www.mysite.com/userValidate.php"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection connectionWithRequest:request delegate:self];
}
在PHP文件IM USIG
$raw = file_get_contents('php://input');
並取回
post = "{\n \"sessionId\" : \"132156\",\n \"sessionName\" : \"My Name\"\n}";
我跟着這個帖子https://stackoverflow.com/a/5078426/1333294這種方法添加到我的PHP文件
function getTestPostData()
{
$post_data = explode("\n", file_get_contents('php://input'));
$result = array();
foreach($post_data as $post_datum) {
$pair = explode(":", $post_datum);
$result[urldecode($pair[0])] = urldecode($pair[1]);
}
return $result;
}
現在我「M越來越
explode = {
" \"sessionId\" " = " \"132156\",";
" \"sessionName\" " = " \"My Name\"";
"{" = "";
"}" = "";
};
我怎麼能 '乾淨' 的陣列的任何進一步所以我ARRY會像
"sessionId" = "132156";
"sessionName" = "My Name";
感謝
擺脫一個通過使用'文件爆炸()' – 2013-04-09 11:12:20
你應該'json_decode'那個字符串,*如果它是有效的JSON。 – deceze 2013-04-09 11:12:32