PHP后台2:客户端(IOS/Android)Post请求以及JSON数据编解码

本文介绍iOS客户端通过Post请求与PHP服务器进行交互的方法,包括普通表单数据和JSON数据的发送与接收,并展示了具体的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)IOS客户端Post请求+PHP服务器Post响应

PHP服务器代码:

<?php

    if($_POST) {

        foreach($_POST as $index => $value) echo "$_POST[$index] = $value", "<BR>";

        foreach($_GET as $index => $value) echo "$index = $value", "<BR>";

    }

?>

IOS客户端代码:

NSString *urlAsString = @"http://localhost/testPost.php?id=1&password=abc";

    

    NSURL *url = [NSURL URLWithString:urlAsString];

    

    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];

    [urlRequest setTimeoutInterval:30.0f];

    [urlRequest setHTTPMethod:@"POST"];

    

    NSString *body = @"bodyParam1=BodyValue1&bodyParam2=BodyValue2";

    [urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];

    

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    

    [NSURLConnection

     sendAsynchronousRequest:urlRequest

     queue:queue

     completionHandler:^(NSURLResponse *response,

                         NSData *data,

                         NSError *error) {

         

         if ([data length] >0  && error == nil){

             NSString *html =

             [[NSString alloc] initWithData:data

                                   encoding:NSUTF8StringEncoding];

             NSLog(@"HTML = %@", html);

         }

}

输出结果:

HTML = BodyValue1 = BodyValue1<BR>

BodyValue2 = BodyValue2<BR>

id = 1<BR>

password = abc<BR>

注意这里在发送Post请求的时候。并不是所有的数据都放在Post中,还有一部分是以Get的形式发送的,所以这里既打印出了Post的内容,也打印出了Get的内容。

(2)IOS客户端Post请求(Json)+PHP服务器Post响应(Json编解码)

PHP服务器代码:

<?php

    if($_POST) {

        $dataJson = file_get_contents("php://input");

        $dataContent = json_decode($dataJson, TRUE);

        if($dataContent["id"] == "abc" && $dataContent["password"] == "123"){

            $marray = array();

            $marray["name"] = "apple";

            $marray["price"] = "15.8";

            $mJsonContent = json_encode($marray);

            echo "$mJsonContent";

        }

    }

?>

IOS客户端代码:

NSString *urlAsString = @"http://localhost/testPostJSONReturn.php";

    

    NSURL *url = [NSURL URLWithString:urlAsString];

    

    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];

    [urlRequest setTimeoutInterval:30.0f];

    [urlRequest setHTTPMethod:@"POST"];

    [urlRequest addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    NSMutableDictionary * tempDic = [NSMutableDictionary dictionary];

    [tempDic setValue:@"abc" forKey:@"id"];

    [tempDic setValue:@"123" forKey:@"password"];

    NSData *postBody = [NSJSONSerialization dataWithJSONObject:tempDic options:NSJSONWritingPrettyPrinted error:nil];


    

    [urlRequest setHTTPBody:postBody];

    

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    

    [NSURLConnection

     sendAsynchronousRequest:urlRequest

     queue:queue

     completionHandler:^(NSURLResponse *response,

                         NSData *data,

                         NSError *error) {

         

         if ([data length] >0  && error == nil){

            id  dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

             if([dic isKindOfClass:[NSArray class]]){

                 NSLog(@"array");

             }else if([dic isKindOfClass:[NSDictionary class]]){

                 NSLog(@"dic");

                 NSDictionary *mdic = (NSDictionary*)dic;

                 NSString *item = @"";

                 for (item in mdic.allKeys) {

                    NSLog(@"%@=%@",item,[mdic valueForKey:item]);

                 }

             }

         } 

     }];

输出结果:

dic

name=apple

price=15.8

这里使用"php://input" 那么客户端的Content-Type 需要指定:application/x-www-form-urlencoded

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值