关于php与app的简单数据处理

APP端实现代码:


- (IBAction)uploadAction:(id)sender {
    mutData = [[NSMutableData alloc] init];
    
    NSURL *url = [NSURL URLWithString:@"http://192.168.222.111/Local/PostDemo.php"];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
    
    [request setHTTPMethod:@"POST"];
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"1.1.1", @"version",@"1",@"type",@"open", @"sw", nil];
    NSData *postData = [NSJSONSerialization dataWithJSONObject:dict options:1 error:nil];
    [request setHTTPBody:postData];
    
    [NSURLConnection connectionWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"%s", __func__);
    mutData.length = 0;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"%s", __func__);
    [mutData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"%s", __func__);
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:mutData options:1 error:nil];
    NSLog(@"%@",dic);
    
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"%s", __func__);
}



服务端实现代码:


<?
if (!empty($GLOBALS['HTTP_RAW_POST_DATA']))  
    {  
        $data =  isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");  
        $json = json_decode($data,TRUE);  
        echo json_encode($json);  
    }else if(file_get_contents("php://input")){  
        $data = file_get_contents("php://input");  
        $json = json_decode($data,TRUE);  
        echo json_encode($json);  
    }else{  
        echo json_encode("Error!");  
    }  
?>


上传后再返回原本数据DICTIONARY.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值