[英]How to make my HTTP POST XML request in PHP actually send XML data to my vendor's Server?
[英]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.