编辑:我已经编辑了OP,其中包含了我从人们那里得到的所有建议,所以这是最新的(它仍然无效).
我有以下代码,它应该将一些数据POST到我拥有的.php文件(然后到数据库,但这超出了这个问题的范围).
对于我的方法,我传递一个包含一些字符串的数组.
-(void)JSON:(NSArray *)arrayData {
//parse out the json data
NSError *error;
//convert array object to data
NSData* JSONData = [NSJSONSerialization dataWithJSONObject:arrayData
options:NSJSONWritingPrettyPrinted
error:&error];
NSString *JSONString = [[NSString alloc] initWithData:JSONData
encoding:NSUTF8StringEncoding];
NSString *URLString = [NSString stringWithFormat:@"websitename"];
NSURL *URL = [NSURL URLWithString:URLString];
NSLog(@"URL: %@", URL);
NSMutableURLRequest *URLRequest = [NSMutableURLRequest requestWithURL:URL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSData *requestData = [NSData dataWithBytes:[JSONString UTF8String] length:[JSONString length]];
NSString *requestDataString = [[NSString alloc] initWithData:requestData
encoding:NSUTF8StringEncoding];
NSLog(@"requestData: %@", requestDataString);
[URLRequest setHTTPMethod:@"POST"];
NSLog(@"method: %@", URLRequest.HTTPMethod);
[URLRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[URLRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[URLRequest setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[URLRequest setHTTPBody: requestData];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:URLRequest delegate:self];
if (connection) {
NSMutableData *receivedData = [[NSMutableData data] retain];
NSString *receivedDataString = [[NSString alloc] initWithData:receivedData
encoding:NSUTF8StringEncoding];
NSLog(@"receivedData: %@", receivedDataString);
}
//potential response
NSData *response = [NSURLConnection sendSynchronousRequest:URLRequest returningResponse:nil error:nil];
//convert response so that it can be LOG'd
NSString *returnString = [[NSString alloc] initWithData:response encoding:NSASCIIStringEncoding];
NSLog(@"RETURN STRING: %@", returnString);
}
Run Code Online (Sandbox Code Playgroud)
注意:我在代码中有我的网站的实际URL,以便任何人都可以测试他们是否愿意.
然后是.php方面的东西,我想要做的就是寻找任何输入(但我什么都没得到).
<html>
<body>
<?php
echo "Connection from iOS app to MySQL database<BR>";
echo '<pre>'.print_r($GLOBALS,true).'</pre>';
?>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我现在真正想做的就是确认数据正在通过,通过回显到页面,然后从我的应用程序中读取页面(到目前为止数据尚未显示).
.php的输出:
<html>
<body>
Connection from iOS app to MySQL database<BR>'Array
(
)
'<pre>Array
(
[_GET] => Array
(
)
[_POST] => Array
(
)
[_COOKIE] => Array
(
)
[_FILES] => Array
(
)
[GLOBALS] => Array
*RECURSION*
)
</pre> </body>
</html>
Run Code Online (Sandbox Code Playgroud)
Esa*_*ija 10
你正在发布JSON而不是application/x-www-form-urlencoded.
在PHP $_POST中只填写了 application/x-www-form-urlencoded-REQUESTS.如果您像这样直接发送json,那么您应该:
<?php
$posted_json = json_decode(file_get_contents("php://input"));
print_r($posted_json);
Run Code Online (Sandbox Code Playgroud)
请注意,如果json无效,则无效.您始终可以使用以下命令调试请求正文:
<?php
echo file_get_contents("php://input");
Run Code Online (Sandbox Code Playgroud)
这里是$ _POST和$ _GET的一般解释http://www.php.net/manual/en/language.variables.external.php:
它们旨在与普通的html表单一起使用,但您可以使用适当的编码和内容类型标头轻松模拟此类请求.但它们不适用于JSON,因为这不是正常的html表单的工作方式.
小智 8
我不知道Objective-C,假设您正在尝试发送以下JSON数据(即JSONString如下):
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
Run Code Online (Sandbox Code Playgroud)
那么你的请求应该是这样的:
POST /yourwebsite.php HTTP/1.1
Host: cs.dal.ca
User-Agent: USER-AGENT
Accept: text/html,application/json,*/*
Connection: close
Content-Type: application/x-www-form-urlencoded
Content-Length: 624
data=%7B%0A++++%22firstName%22%3A+%22John%22%2C%0A++++%22lastName%22%3A+%22Smith%22%2C%0A++++%22age%22%3A+25%2C%0A++++%22address%22%3A+%7B%0A++++++++%22streetAddress%22%3A+%2221+2nd+Street%22%2C%0A++++++++%22city%22%3A+%22New+York%22%2C%0A++++++++%22state%22%3A+%22NY%22%2C%0A++++++++%22postalCode%22%3A+%2210021%22%0A++++%7D%2C%0A++++%22phoneNumber%22%3A+%5B%0A++++++++%7B%0A++++++++++++%22type%22%3A+%22home%22%2C%0A++++++++++++%22number%22%3A+%22212+555-1234%22%0A++++++++%7D%2C%0A++++++++%7B%0A++++++++++++%22type%22%3A+%22fax%22%2C%0A++++++++++++%22number%22%3A+%22646+555-4567%22%0A++++++++%7D%0A++++%5D%0A%7D%0A%0A%0A
Run Code Online (Sandbox Code Playgroud)
注意,Content-Type: application/json是不是对发布数据的方式,因为你已经使用,并注意数据已经进行了urlencoded,它不应该是很难这个,我发现了大约在Objective-C这样做此链接:URL编码用Objective-C的字符串
发送上述请求,我得到了这个:
php代码:
<?php
$raw_json_data = $_POST['data'];
$json_data = json_decode($raw_json_data);
print_r($json_data);
Run Code Online (Sandbox Code Playgroud)
结果:
stdClass Object
(
[firstName] => John
[lastName] => Smith
[age] => 25
[address] => stdClass Object
(
[streetAddress] => 21 2nd Street
[city] => New York
[state] => NY
[postalCode] => 10021
)
[phoneNumber] => Array
(
[0] => stdClass Object
(
[type] => home
[number] => 212 555-1234
)
[1] => stdClass Object
(
[type] => fax
[number] => 646 555-4567
)
)
)
Run Code Online (Sandbox Code Playgroud)
这就是你想要的!
注意:我应该提到您在http://yourwebsite.php上的脚本无法正常工作,我甚至无法提交正常的表单!可能存在服务器配置错误或类似问题,但使用Apache 2.2和PHP 5.4.4以及上面提到的代码,我得到了它的工作!
| 归档时间: |
|
| 查看次数: |
811 次 |
| 最近记录: |