簡體   English   中英

來自iPhone的php中的簡單文章不起作用

[英]Simple post in php from iPhone not working

我四處張望,想從iPhone應用程序中進行字符串的簡單上傳。 我已經在線使用了所有代碼。 iPhone可以使用。 它從php文件中檢索代碼,並將其打印到控制台。 但是php腳本中的值返回空。

<?php

$string = $_POST["string"];

    if(strlen ($string)==0){
       echo"empty";
    }else{
       echo "The string is: ". $string;  
}
?>

這是iOS代碼

 - (void) sendCode{
// Create your request string with parameter name as defined in PHP file
NSString *myRequestString = [NSString stringWithFormat:@"string=%@&",signInCode.text];

// Create Data from request
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://artistfolio.net/signin.php"]];
// set Request Type
[request setHTTPMethod: @"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
// Now send a request and get Response
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn)
{
    NSLog(@"Good");
}
else
{
    NSLog(@"Bad");
}
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",response);

}

也許iOS將數據發送到請求的主體中。 嘗試將file_get_contents('php://input')放入服務器端。 或將Content-Type: application/x-www-form-urlencoded標頭更改為客戶端。

在第二種情況下,您應該閱讀: 使用application / x-www-form-urlencoded的POST請求

檢查此答案,最初在此處發布。

PHP代碼

<?php
 $title = $_POST['title'];
 $description = $_POST['description'];
 $city = $_POST['city'];

 echo "Title: ". $title;
 echo "Description: ". $description;
 echo "City: ". $city;
?>

iOS端編碼:

目標C

// Create your request string with parameter name as defined in PHP file
NSString *myRequestString = [NSString stringWithFormat:@"title=%@&description=%@&city=%@",eventTitle.text,eventDescription.text,eventCity.text];

// Create Data from request
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://www.youardomain.com/phpfilename.php"]];
// set Request Type
[request setHTTPMethod: @"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
// Now send a request and get Response
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",response);

迅速

// Create your request string with parameter name as defined in PHP file
var myRequestString: String = "title=\(eventTitle.text!)&description=\(eventDescription.text!)&city=\(eventCity.text!)"
// Create Data from request
var myRequestData: NSData = NSData.dataWithBytes(myRequestString.UTF8String(), length: myRequestString.characters.count)
var request: NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: "http://www.youardomain.com/phpfilename.php")!)
// set Request Type
request.HTTPMethod = "POST"
// Set content-type
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
// Set Request Body
request.HTTPBody = myRequestData
// Now send a request and get Response
var returnData: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
// Log Response
var response: String = String(bytes: returnData.bytes(), length: returnData.characters.count, encoding: NSUTF8StringEncoding)
NSLog("%@", response)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM