Rya*_*yan 3 php arrays objective-c http-post
过去几天我一直在努力奋斗; 我试图将数组发布到PHP.我可以成功发送它,但它没有使用后变量(我试图使用变量键"json"...使用此代码,我在php中收到数组:
Objective-C的
NSError *error;
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: @"one", @"two", @"three", nil] forKeys: [NSArray arrayWithObjects: @"a", @"b", @"c", nil]];
NSArray *jsonArray = [NSArray arrayWithObject:jsonDictionary];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonArray options:NSUTF8StringEncoding error:&error];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"somewebservicelocation/arrayTest.php?json="]];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:jsonData];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *response = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"response: %@",response);
Run Code Online (Sandbox Code Playgroud)
PHP
$handle = fopen('php://input','r');
$array = fgets($handle);
echo $array;
if(isset($array))
{
echo "success";
}
else
{
echo "failure";
}
Run Code Online (Sandbox Code Playgroud)
如果我使用这个PHP,使用_POST,我得不到爱:
$rawJsonData = $_POST['json'];
$array = json_decode(stripslashes($rawJsonData),true);
echo $array;
if(isset($array))
{
echo "success";
}
else
{
echo "failure";
}
Run Code Online (Sandbox Code Playgroud)
...我已经使用了好几天 - 遍布Stack Overflow,并且理解我需要在请求的主体中包含变量和数据,但我无法让它工作.我究竟做错了什么?你如何区别对待这个?救我脱离这个头痛......
在PHP方面,我使用了与你的第一个例子类似的东西:
<?php
$handle = fopen("php://input", "rb");
$http_raw_post_data = '';
while (!feof($handle)) {
$http_raw_post_data .= fread($handle, 8192);
}
fclose($handle);
// do what you want with it
//
// For diagnostic purposes, I'm just going to decode, make sure I got an array,
// and respond with JSON that includes status, code, and the original request
$post_data = json_decode($http_raw_post_data,true);
if (is_array($post_data))
$response = array("status" => "ok", "code" => 0, "original request" => $post_data);
else
$response = array("status" => "error", "code" => -1, "original_request" => $post_data);
$processed = json_encode($response);
echo $processed;
?>
Run Code Online (Sandbox Code Playgroud)
然后在iOS方面,我使用:
// create the dictionary (or array)
NSDictionary *dictionary = @{@"a": @"One", @"b": @"Two", @"c": @"Three"};
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
if (error)
NSLog(@"%s: JSON encode error: %@", __FUNCTION__, error);
// create the request
NSURL *url = [NSURL URLWithString:@"your.url.here"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:data];
// issue the request
NSURLResponse *response = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error)
NSLog(@"%s: NSURLConnection error: %@", __FUNCTION__, error);
// examine the response
NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"responseString: %@",responseString);
Run Code Online (Sandbox Code Playgroud)
我刚刚测试了这次往返,它运行正常.
如果您决定使用该_POST技术,对我来说有用的是设置数据json=%@,例如:
NSDictionary *dictionary = ...
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (error)
NSLog(@"%s: JSON encode error: %@", __FUNCTION__, error);
NSURL *url = [NSURL URLWithString:@"your.url.here"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *params = [NSString stringWithFormat:@"json=%@",
[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *paramsData = [params dataUsingEncoding:NSUTF8StringEncoding];
[request addValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"];
[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:paramsData];
// now send the request, like before
Run Code Online (Sandbox Code Playgroud)
用来解析它的PHP就像你一样:
$http_raw_post_data = $_POST['json'];
$post_data = json_decode(stripslashes($http_raw_post_data),true);
if (is_array($post_data))
$response = array("status" => "ok", "code" => 0, "original request" => $post_data);
else
$response = array("status" => "error", "code" => -1, "original_request" => $post_data);
$processed = json_encode($response);
echo $processed;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3672 次 |
| 最近记录: |