繁体   English   中英

我的 iphone 应用程序如何将数据发送到服务器的 php 文件?

[英]How can my iphone app send data to the server's php file?

以下是我在服务器中的目标 php 文件的开始部分。

$xmlFile = file_get_contents("php://input");
echo $xmlFile."<br>";

不幸的是,浏览器的该页面上没有打印任何内容。

以下是我的iphone端编程的一部分

NSURL *url = [NSURL URLWithString:@"http://www.mywebsite.com/target.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content- Type"];

NSMutableData *xmlData = [NSMutableData data];
[xmlData appendData: [[NSString stringWithFormat: @"<?xml version=\"1.0\"   encoding=\"UTF-8\" ?>"] dataUsingEncoding: NSUTF8StringEncoding]];
[xmlData appendData: [[NSString stringWithFormat: @"<Packet xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance iphone_schema.xsd\" xmlns=\"http://www.mywebsite.com/iphone.xsd\">"] dataUsingEncoding: NSUTF8StringEncoding]];
[xmlData appendData: [[NSString stringWithFormat: @"<info1>%@<info1>",self.uuid] dataUsingEncoding: NSUTF8StringEncoding]];

//....append xml data strings in the same way
[request setHTTPBody:xmlData];
NSURLConnection *connect = [NSURLConnection connectionWithRequest:request delegate:self];
if(connect != nil){
    NSLog(@"valid request");
}

连接对象不为零。 但我不确定应用程序是否已将请求 POST 消息发送到 php 页面。

我在那个 php 文件中写了一些代码来测试连接。 似乎没有从 iphone 收到任何信息。 那么发生了什么? 我已经测试了几个小时!
希望可以有人帮帮我! 谢谢!

设置您的NSURLConnection委托方法 这将帮助您更好地诊断问题并查看它是在 iPhone 还是服务器端。

此外,如果您发送 POST 请求。 所以你不需要从 PHP 的输入流中读取。 使用$_POST超级全局.

您还可以通过从命令行发送请求来确保您的 PHP 脚本正确。 例如在 *nix 上curl

$xmlFile = file_get_contents("php://input"); 回声 $xml 文件。”
”;

如果您从发布到输入的浏览器中查看它,这只会回显某些内容,如果您的 iOS 发布到 PHP,然后您访问 PHP,则不会发生这种情况。

通常,您根本无法以这种方式验证它。 您必须在 PHP 中设计一个响应并设置您的委托方法来处理 PHP 返回的任何内容。

如:

$xmlFile = file_get_contents("php://input");
if ($xmlFile) {
echo "{\"file\":\"$xmlFile\"}";
}

通过这种方式,您可以使用 NSJSONSerializer 在 iOS 中解码响应,并查看您发布的内容与返回的内容相同。 }

试试ngrep。 它可能会有所帮助。

首先尝试设置自制软件

然后做一个

brew install ngrep

进而

sudo ngrep host mywebsite.com port 80

这应该显示在设备和服务器之间在 mywebsite.com 上发送和接收的数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM