iOS与php进行简单的post交互
来源:互联网 发布:离散最优化 编辑:程序博客网 时间:2024/05/21 09:31
最近研究了一下这方面的知识,现在贴出代码做一个总结
首先是iOS端的
- (void)viewDidLoad{ [super viewDidLoad]; mydata = [[NSMutableData alloc]initWithCapacity:0]; NSURL *url = [NSURL URLWithString:@"http://localhost/test/postDemo.php"]; //创建请求 NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60]; [request setHTTPMethod:@"POST"]; NSNumber *s = [NSNumber numberWithInt:22]; NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:[@"com.xinmei365.font" stringByEncodingURIComponent],@"package",s,@"version",@"11",@"channel",@"22",@"type",@"zh",@"lang", nil]; NSString *param = [dict JSONString]; NSData *data = [param dataUsingEncoding:NSUTF8StringEncoding]; //加入json参数 [request setHTTPBody:data]; NSURLConnection *contect = [[NSURLConnection alloc]initWithRequest:request delegate:self];// [contect start]; }- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ mydata.length = 0;}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [mydata appendData:data];}- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSString *str = [[NSString alloc]initWithData:mydata encoding:NSUTF8StringEncoding]; NSDictionary *dict = [str objectFromJSONString]; NSLog(@"%@",dict); }- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"error");}
下面是php,相当于传上去,再传回来
<?php if (!empty($GLOBALS['HTTP_RAW_POST_DATA'])) { $command = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input"); $j = json_decode($command,TRUE);//true,转化成数组 } echo json_encode($j);
注意:先设置php.ini中的always_populate_raw_post_data值设为On,这样表示可以接受data数据(这个php.ini是隐藏在etc目录下的,一般的搜索可能也许也搜索不到,需要自己用终端搞定这个问题,如果没有设置的话,可以参考下面的代码)
if (!empty($GLOBALS['HTTP_RAW_POST_DATA'])) { $command = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input"); $j = json_decode($command,TRUE); echo json_encode($j); }else if(file_get_contents("php://input")){ $command = file_get_contents("php://input"); $j = json_decode($command,TRUE); echo json_encode($j); }else{ echo json_encode("error!!!!!!!!!"); }
- iOS与php进行简单的post交互
- Ajax通过POST方式与PHP进行交互
- iOS客户端与PHP服务端的简单交互
- html中表单post方法与php还有sql交互的最简单demo
- iOS --- 使用RestKit与RESTful web服务器进行简单交互
- PHP与服务器文件系统的简单交互
- ios前台与php后台的交互
- 简单的PHP之GET与POST
- html网页与iOS的简单交互
- iOS与js 的简单交互
- unity与iOS之间的简单交互
- unity3d与ios进行交互
- jquery与php交互之GET、 POST
- jquery与php交互之GET、 POST
- 简单js与php交互
- iOS中UIWebView与Javascript进行交互的要点记录
- iOS与unity3D简单交互
- php表单的交互(post方法)
- object references an unsaved transient instance - save the transient instance before flushing
- Windows下Oracle10g的服务
- WordPress博客推荐
- log4j配置文件经典
- POJ P2524 Ubiquitous Religions 基础并查集
- iOS与php进行简单的post交互
- 聚合和组合
- ZOJ 1045 HangOver
- find 函数和string :: npos 的用法
- 斐波那契数列及其运用
- thinkpad x200 自己更换风扇的步骤和更换后的调整
- Study Flex《AS3中Object与Dictionary的区别》
- 10年内sinox开发者将成为IT技术领袖
- android 隐藏输入法 显示光标