MAMP PRO + 前后臺(oc|php)數(shù)據(jù)交互

寫在前面

MAMP PRO 軟件里面集成了apache、mysql、php的功能。當(dāng)然也可以在Mac電腦上自己搭建apache、mysql、php(命令行即可實現(xiàn),如有需要可點擊)。下面講解MAMP PRO如何使用:


1.下載軟件

[LS]( https://pan.baidu.com/s/1hr8cE7U 密碼 r53k)

2.設(shè)置General選項卡中的端口,右下角保存
port設(shè)置.png
3.設(shè)置Hosts選項卡
Hosts.png
Start.png
  • document root就是web的根目錄,用來存放后臺的php文件等,xcode項目你想放在哪里就放在哪里.
  • Server name就是根目錄對應(yīng)的網(wǎng)址,通過輸入 網(wǎng)址+根目錄中的php文件名就可以向后臺發(fā)送網(wǎng)絡(luò)請求了.
  • document root和 Server name等價于
Apache系統(tǒng)級的根目錄及對應(yīng)網(wǎng)址是:
/Library/WebServer/Documents/ http://localhost

用戶級的根目錄及對應(yīng)網(wǎng)址是:
~/Sites http://localhost/~zhangbin/

4.咱們來測試一下

101.39.gif

經(jīng)過上面的測試,環(huán)境已經(jīng)搭建好了,現(xiàn)在進(jìn)入主題,開始我們今天的核心內(nèi)容
前后臺(oc|php)數(shù)據(jù)交互。

(一)Get請求


前臺發(fā)送請求Get請求

#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    NSString *url = @"http://www.youku.com/GetRequest.php";
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 1. responseObject為json數(shù)據(jù)。這里將json數(shù)據(jù)先轉(zhuǎn)為NSData,
        NSData *JsonToData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:nil];
        //  2. 再將NSData轉(zhuǎn)為NSString
        NSString *DataToString = [[NSString alloc] initWithData:JsonToData encoding:NSUTF8StringEncoding];
       // 1+2的形式可以解決打印的內(nèi)容為亂碼的問題。因為我們在控制臺打印打印DataToString數(shù)據(jù)是為了更好的看到服務(wù)器返回的是什么內(nèi)容,并不是要把DataToString顯示在手機(jī)上的。顯示在手機(jī)上我們直接將responseObject進(jìn)行字典轉(zhuǎn)模型即可。
        NSLog(@"%@", DataToString);

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"%@",error);
}];

}

@end

后臺GetRequest.php文件接收到Get請求

<?php
    
    $data = array(
                  '' => 100,
                  'name' => 'Get請求的結(jié)果哦CoderZb',
                  'website' => ' Get www.youku.com');
    
    $response = array(
                      'code'    => 553828497,
                      'message' => 'Get請求成功',
                      'data'    => $data,
                      );
    //  json_encode將數(shù)組轉(zhuǎn)為Json,然后返回到前臺
    echo json_encode($response);
 ?>

前臺得到請求結(jié)果,在xocode控制臺打印如下內(nèi)容:

GetRequest.png

動態(tài)截圖:

http://www.bejson.com/ 驗證后臺(php)的數(shù)據(jù)和前臺(xcode)的數(shù)據(jù)是否一致,結(jié)果一致

101.31.gif

(二)Post請求


前臺發(fā)送請求Post請求

#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
     NSString *url = @"http://www.youku.com/PostRequest.php";
    NSDictionary *parameters = @{@"type" : @(1)};

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
     manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
        [manager POST:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            // responseObject為json數(shù)據(jù)。這里將json數(shù)據(jù)先轉(zhuǎn)為NSData,
            NSData *JsonToData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:nil];
            // 再將NSData轉(zhuǎn)為NSString
            NSString *DataToString = [[NSString alloc] initWithData:JsonToData encoding:NSUTF8StringEncoding];
            NSLog(@"%@", DataToString);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
        }];
}
@end

后臺PostRequest.php文件接收到Post請求

<?php
    $type = $_POST['type'];
    $data = '';
    
    if (isset($type) && is_numeric($type) && $type >= 0) {
        if ($type == 1) {
            $data = array(
                          'result' => 'Post請求成功',
                          'type' => $type,
                          'name' => '簡書CoderZb',
                          'website' => 'www.itdecent.cn');
        } else if ($type == 2) {
          
            $data = array(
                          'result' => 'Post請求成功',
                          'type' => $type,
                          'name' => '百度貼吧',
                          'website' => 'www.tieba.baidu.com');
        }
        
        $response = array(
                          'message' => 'Post請求成功',
                          'qq'    => 553828497,
                          'Data'    => $data,
                          );
        
        echo json_encode($response);
        return;
    } 
    else{
    $response = array(
                      'qq'    => 1234567890,
                      'message' => ' POST請求參數(shù)錯誤',
                      'Data'    => $data,
                      );
        
     
    echo json_encode($response);
        return;
    }
    

?>
PostRequest.png

動態(tài)截圖:

http://www.bejson.com/ 驗證后臺(php)的數(shù)據(jù)和前臺(xcode)的數(shù)據(jù)是否一致,結(jié)果不一致

101.32.gif

不一致的原因:

  • 在xcode發(fā)送網(wǎng)絡(luò)請求,將@{@"type" : @(1)}傳遞給后臺的php文件,滿足
 if ($type == 1) { 
$data = array( 'result' => 'Post請求成功',
 'type' => $type, 'name' => '簡書CoderZb', 
'website' => 'www.itdecent.cn'); } 

所以返回到前臺能打印出上面的數(shù)據(jù)。

 else{
    $response = array(
                      'qq'    => 1234567890,
                      'message' => ' POST請求參數(shù)錯誤',
                      'Data'    => $data,
                      );
        
     
    echo json_encode($response);
        return;
    }

拓展:對于(二)Post請求,如何可以實現(xiàn)在瀏覽器中能打印出和xcode中一樣的內(nèi)容呢?
  • 解決辦法:將PostRequest.php文件中的_POST改為GET

101.33.gif

  • 對拓展的總結(jié):
  • get請求如果需要請求參數(shù)的話,那么必須把請求參數(shù)type=1暴露在外面(附著在url的后面),外界只要竊取到url,就能獲取到你的請求參數(shù),所以能輸出type == 1中的內(nèi)容,這個內(nèi)容你就理解為私密的數(shù)據(jù)。
  • 利用post請求數(shù)據(jù),就不能將type=1放在url后面,即使你放在了后面,也沒用的[上面的動態(tài)截圖已驗證]。所以會打印出請求參數(shù)錯誤,這樣的話重要的信息($type == 1和$type == 2中的內(nèi)容)都沒有被別人看到。

[LS](https://pan.baidu.com/s/1i5AYHvR 密碼 g9v9)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容