OGeek|极客世界-中国程序员成长平台

标题: php - iOS 9.1 导致我的 JSON NSURL 连接到 PHP 页面时出错 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 12:14
标题: php - iOS 9.1 导致我的 JSON NSURL 连接到 PHP 页面时出错

在 iOS 9.1 之前,我的应用程序已成功与网络上的 .php 文件通信以获取一些数据库信息。所有这些通信都是使用 JSON 完成的,用于存储两者之间的信息。我知道有一个问题似乎与发送到我的 .php 文件的数据包有关。

if($_POST == null){ // Check for proper posting.

    $handle  = file_get_contents('php://input');
    $body = json_decode($handle, true);
}
else{
    $body == $_POST;        
}

上面的代码似乎是故障点。我收到一个不为空的 $_POST

-(void)postAuthorizeNSString*)code NSString*)phone{

   //build up the request that is to be sent to the server
   NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString"http://www.example.com/ex.php"]];

   [request setHTTPMethod"OST"];
   [request addValue"postValues" forHTTPHeaderField"METHOD"];

   //create data that will be sent in the post
   NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
   [dictionary setValue:code forKey"authorize"];
   [dictionary setValue:phone forKey"device"];

   NSLog(@"Dictionary: %@", dictionary);

   //serialize the dictionary data as json
   NSData *data = [[dictionary copy] JSONValue];

   NSLog(@"Data: %@", data);

   [request setHTTPBody:data]; //set the data as the post body
   [request addValue:[NSString stringWithFormat"%lu",(unsigned long)data.length] forHTTPHeaderField"Content-Length"];
   NSLog(@"data length: %lu", (unsigned long)data.length);
   NSLog(@"Request body %@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]);
   NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
   if(!connection){
       NSLog(@"Connection Failed");
   }

}

以上是 iPhone 上的代码。我检查了 URL 连接正文中的 JSON 以确保其格式不正确。好像没有。

这是我得到的错误:

Domain=NSCocoaErrorDomain Code=3840.

但我认为这只是因为 PHP 代码失败了。如果我绕过上面的代码,只发回一个静态值,一切都会顺利。

我真的需要帮助。请如果有人能指出什么是错的。我不知道为什么这已经工作了 2 年,现在它从 iOS 9.0 -> 9.1 中断了

问题出现在我的 PHP 中。我猜苹果在 9.0/9.1 中使用 URL Connection 改变了 $_POST 方法。

$handle  = file_get_contents('php://input');
$body = json_decode($handle, true);



Best Answer-推荐答案


把你的php代码改成这个-

$handle  = file_get_contents('php://input');

if($handle == null || $handle == ""){
    $body = $_POST; 
}else{
    $body = json_decode($handle);
}

关于php - iOS 9.1 导致我的 JSON NSURL 连接到 PHP 页面时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34216768/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4