Данные JSON с iOS на php

0

Я хочу отправить данные json из iOS в код php.my ios ниже:

 NSURLResponse *response = nil;
    NSError *error = nil;

    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"ishu", @"123", @"hari", @"135", nil];
    NSData *result =[NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
    NSURL *url = [NSURL URLWithString:@"http://192.168.1.4:8888/iostomysql11.php"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d",result.length] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:result];

     NSData *result1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

и мой php-код ниже:

<?php 
$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
$decoded = json_decode($jsonInput,true);
var_dump($jsonInput);
?>

но я получил выход вроде этого. bool(false).i Не знаю, php, что много. Какая ошибка в моем коде? Пожалуйста, помогите мне.

Теги:

3 ответа

0

Вы можете сделать следующее:

$json= file_get_content('php://input');
$decoded = json_decode($json,true);
var_dump($jsonInput);

Удостоверьтесь, что вы регистрируете result и dict чтобы убедиться, что все в порядке и попытайтесь проверить наличие ошибок.

0

Вы можете использовать этот код:

<?php 
$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
$decoded = json_decode($jsonInput,true);
?>
0

Я успешно выполнил ту же задачу:

Следующий мой код. Надеюсь, это поможет вам

NSURL *url = [NSURL URLWithString:m_url];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req addValue:@"getModule" forHTTPHeaderField:@"METHOD"];

NSMutableDictionary *dicCredentials=[[NSMutableDictionary alloc]init];
[dicCredentials setValue:userid forKey:@"user_id"];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setValue:@"user" forKey:@"moduleName"];
[dictionary setValue:@"getFavourite" forKey:@"methodName"];
[dictionary setValue:dicCredentials forKey:@"data"];
NSLog(@"Sent data : %@",dictionary);

//      serialize the dictionary data as json
NSError *error = nil;

NSData *result = [NSJSONSerialization dataWithJSONObject:[dictionary copy] options:kNilOptions error:&error];
if(error){
    NSLog(@"%@",error);
}else{
    [req setHTTPBody:result]; //set the data as the post body

}
[req addValue:[NSString stringWithFormat:@"%lu",(unsigned long)result.length] forHTTPHeaderField:@"Content-Length"];

conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
    webData = [[NSMutableData data]retain];
}

Код PHP:

$jsonRequest = file_get_contents('php://input');

$jsonArr = json_decode($jsonRequest, true)
  • 0
    вывод равен NULL.
  • 0
    Исправьте эту строку в своем коде. Это может помочь. NSData * result = [NSJSONSerialization dataWithJSONObject: [копия словаря] опции: kNilOptions error: & error]; Установите параметры для kNilOptions, а не nil.
Показать ещё 1 комментарий

Ещё вопросы

Сообщество Overcoder
Наверх
Меню