简体   繁体   English

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

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

the following is the beginning part of my target php file in the server.以下是我在服务器中的目标 php 文件的开始部分。

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

Unfortunately nothing is printed on that page in browser.不幸的是,浏览器的该页面上没有打印任何内容。

The following is part of my iphone side programming以下是我的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");
}

the connect object is not nil.连接对象不为零。 But I'm not sure whether the app has sent the request POST message to the php page.但我不确定应用程序是否已将请求 POST 消息发送到 php 页面。

And I wrote some code in that php file to test the connection.我在那个 php 文件中写了一些代码来测试连接。 It seems nothing is received from the iphone.似乎没有从 iphone 收到任何信息。 So what's happening?那么发生了什么? I test it for already several hours!我已经测试了几个小时!
Hope someone can help me!希望可以有人帮帮我! Thanks!谢谢!

Setup your NSURLConnection delegate methods .设置您的NSURLConnection委托方法 This will help you better diagnose the issue and see if it's on the iPhone or server side.这将帮助您更好地诊断问题并查看它是在 iPhone 还是服务器端。

Also, if you sending a POST request.此外,如果您发送 POST 请求。 So you don't need to read from PHP's input stream.所以你不需要从 PHP 的输入流中读取。 Use the $_POST super global .使用$_POST超级全局.

You can also ensure your PHP script is correct by sending it a request from the command line.您还可以通过从命令行发送请求来确保您的 PHP 脚本正确。 For example curl on *nix.例如在 *nix 上curl

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

This would only echo something if you viewed it from the browser that posted to input, which is not what is happening if your iOS posts to PHP and then you visit the PHP after.如果您从发布到输入的浏览器中查看它,这只会回显某些内容,如果您的 iOS 发布到 PHP,然后您访问 PHP,则不会发生这种情况。

Generally you can't validate it this way at all.通常,您根本无法以这种方式验证它。 You must design a response in PHP and set up your delegate methods to handle whatever the PHP returns.您必须在 PHP 中设计一个响应并设置您的委托方法来处理 PHP 返回的任何内容。

Such as:如:

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

This way you could decode the response in iOS with NSJSONSerializer and view what you posted was the same as what was returned.通过这种方式,您可以使用 NSJSONSerializer 在 iOS 中解码响应,并查看您发布的内容与返回的内容相同。 } }

Try ngrep.试试ngrep。 It might help.它可能会有所帮助。

Start by trying to setup homebrew首先尝试设置自制软件

Then do a然后做一个

brew install ngrep

and then进而

sudo ngrep host mywebsite.com port 80

This should display the data that's sent and received between the device and the server at mywebsite.com.这应该显示在设备和服务器之间在 mywebsite.com 上发送和接收的数据。

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

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