【问题标题】:Posting JSON from iOS to PHP将 JSON 从 iOS 发布到 PHP
【发布时间】:2012-12-02 22:06:57
【问题描述】:

我一直在尝试通过 php 页面将 JSON 数据从 iOS 应用程序发送到 mySQL 数据库。由于某种原因,我的 POST 数据在 php 页面中不可用。

- (IBAction)jsonSet:(id)sender {   
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"firstvalue", @"firstkey", @"secondvalue", @"secondkey", nil];
    NSData *result =[NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
    NSURL *url = [NSURL URLWithString:@"http://shred444.com/testpost.php"];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", jsonRequestData.length] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:jsonRequestData];

    NSURLResponse *response = nil;
    NSError *error = nil;

    NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

我知道 php 页面被调用,写入数据库得到确认,

我的 php 文件中的前几行获取 POST 数据

<?php
// Put parameters into local variables
$email = $_POST["firstkey"];
...

但由于某种原因,$email 也是一个空字符串。我感觉问题出在 iOS 代码中,因为我可以使用 APIkitchen.com 来测试我的页面并且我可以确认它有效(仅当我排除 Content-type 和 Content-Length 字段时)

【问题讨论】:

    标签: php ios json post


    【解决方案1】:
    <?php
    
    $handle = fopen('php://input','r');
    $jsonInput = fgets($handle);
    $decoded = json_decode($jsonInput,true);
    print_r($decoded['firstkey']);
    
    ?>
    

    【讨论】:

    • 请尝试添加更多关于此代码如何以及为什么工作的解释。另外,请尝试指出为什么 OP 的代码不起作用。
    【解决方案2】:

    这对我有用:

    $jsonString = file_get_contents('php://input');
    $jsonArray = json_decode($jsonString, true);
    
    // with [] instead of ()
    $email = $jsonArray['firstkey']; 
    

    【讨论】:

      【解决方案3】:

      Valera 的回答中有一个小错字

      $jsonString = file_get_contents('php://input');
      $jsonArray = json_decode($jsonString, true);
      
      $email = $jsonArray('firstkey');
      

      【讨论】:

        【解决方案4】:

        似乎可行的是:

        $handle = fopen('php://input','r');
        $jsonInput = fgets($handle);
        // Decoding JSON into an Array
        $decoded = json_decode($jsonInput,true);
        

        【讨论】:

          【解决方案5】:

          PHP 不会将 JSON POST 正文解码为 $_POST 数组(因此您不能使用 $email = $_POST["firstkey"];)。您需要将传入数据提取到数组(或对象)。 PHP文件的代码行:

          $jsonString = file_get_contents('php://input');
          $jsonArray = json_decode($json_string, true);
          

          $jsonArray 将代表您发送的 JSON 结构。

          【讨论】:

          • 所以只需添加这两行,我就可以使用 $jsonArray['firstkey'] 访问数据?似乎没有工作
          • 我不知道您在$jsonArray 中的密钥是什么。使用var_dump($jsonArray); 来查看它们。你的问题解决了吗?
          • 我发现这可行,但不能正确处理传入的 utf-8。有什么建议吗?
          • 我知道这已经很老了,但是 IOS 端没有办法在 POST var 上绑定 JSON 字符串。这样php端就用:json_decode($_POST["jsonString"]);
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-05-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-28
          • 1970-01-01
          相关资源
          最近更新 更多