繁体   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