iPhone中的简单php帖子不起作用
Posted
技术标签:
【中文标题】iPhone中的简单php帖子不起作用【英文标题】:Simple post in php from iPhone not working 【发布时间】:2016-02-23 08:48:22 【问题描述】:我已经查看了所有内容,并希望从 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);
【问题讨论】:
$code
在哪里? html
在哪里?
在 iOS 中如何发送数据?
对不起@Mr.Engineer 我没有任何HTML代码
【参考方案1】:
检查这个答案,最初发布here。
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)
【讨论】:
那么你可以在这里查看我的答案:***.com/questions/35570676/…我有两种不同的方法来完成它,希望可以奏效。【参考方案2】:也许 iOS 将数据发送到请求的正文中。
尝试file_get_contents('php://input')
进入服务器端。或将Content-Type: application/x-www-form-urlencoded
标头更改为客户端。
在第二种情况下,您应该阅读:POST request using application/x-www-form-urlencoded
【讨论】:
关于内容类型我在客户端有这个设置。我不完全理解你所说的 file_get_contents(www.example.com) 是什么意思。我搜索并发现它会在网站上完成,但我试图在 iPhone 上的帖子上完成。 @C Würtz 你能不能加NSLog(@"%@",myRequestString);
然后在PHP中加var_dump(file_get_contents('php://input'), $_POST);
只是为了检查。
在我的 iOS 输出中是 'string(19) "string=Hello World&" array(1) ["string"]=> string(11) "Hello World" empty' 并且在线还是空的
我不明白输出的结尾。我认为var_dump()
中只有一个论点。是哪一个?以上是关于iPhone中的简单php帖子不起作用的主要内容,如果未能解决你的问题,请参考以下文章