Cocoa POST& Php问题

时间:2016-06-07 18:06:23

标签: php cocoa post

我没有多少使用cocoa和Php的经验

目前,当我想将数据从Ipad传递到我的服务器时,我必须解决一个问题,当然它无法正常工作。

当我检查服务器上的error_log文件时,我发现该行有错误... [07-Jun-2016 12:10:38] PHP警告:为/ home中的foreach()提供的参数无效/futchoco/public_html/WebService/subir_datos.php在第32行,就在行 //循环后,他将json对象插入到mysql中

<?php



class RedeemAPI {
private $db;
// Constructor - open DB connection
function __construct() {
    $this->db = new mysqli('localhost', 'xxxx', '999', 'futchoco');
   /* verificar la conexión */
   if (mysqli_connect_errno()) {
    printf("Conexión fallida: %s\n", mysqli_connect_error());
    exit();
  }
    $this->db->autocommit(FALSE);
}

// Destructor - close DB connection
function __destruct() {
    $this->db->close();
}
// Main method to redeem a code
function redeem() {
// Check for required parameters
    $json = file_get_contents('php://input');
    $obj = json_decode($json,true);
    error_log(print_r($obj,true));
    // prepare the query
    $stmt = $this->db->prepare('INSERT INTO det_encuentro_appcedula (id_cliente,id_sucursal,id_torneo,id_jornada,id_juego,id_equipo,enc_locvis,id_jugador,denc_minuto,denc_gol,denc_roja,denc_amarilla,id_arbitro) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');    
    $stmt->bind_param('iiiiiiiiiiiii',$id_cliente,$id_sucursal,$id_torneo,$id_jornada,$id_juego,$id_equipo,$enc_locvis,$id_jugador,$denc_minuto,$denc_gol,$denc_roja,$denc_amarilla,$id_arbitro);
    // loop througght he json objects to insert into mysql
    foreach($obj as $assocArray) {
        error_log(print_r($obj,true));
        //$id = $assocArray['value1'];
        //$name = $obj['nombre'];
        $id_cliente = $obj['id_cliente'];
        $id_sucursal = $obj['id_sucursal'];
        $id_torneo = $obj['id_torneo'];
        $id_jornada = $obj['id_jornada'];
        $id_juego = $obj['id_juego'];
        $id_equipo = $obj['id_equipo'];
        $enc_locvis = $obj['enc_locvis'];
        $id_jugador = $obj['id_jugador'];
        $denc_minuto = $obj['denc_minuto'];
        $denc_gol = $obj['denc_gol'];
        $denc_roja = $obj['denc_roja'];
        $denc_amarilla = $obj['denc_amarilla'];
        $id_arbitro = $obj['id_arbitro'];
        //error_log(print_r($id,true));
        //error_log(print_r($name,true));
        if (!$stmt->execute()) // execute the query´
        {
           error_log(print_r('Execute failed to run',true));
        }else{ 
           $stmt = $this->db->prepare('COMMIT');
           $stmt->execute();
        }
    }
}
}


// This is the first thing that gets called when this page is loaded
// Creates a new instance of the RedeemAPI class and calls the redeem method
$api = new RedeemAPI;
$api->redeem();

?>

我想这个问题出在可可代码上,当我运行这段代码时

NSLog(@"JSON summary: %@", [[NSString alloc] initWithData:self.json
                                                           encoding:NSUTF8StringEncoding]);

我有这个数据

2016-06-07 10:15:34.388 CedulaArbitral[3760:60b] JSON summary: [
{
"gol" : "1",
"id_jugador" : "10",
"sucursal" : "1",
"id_arbitro" : "33",
"locvis" : "L",
"amarilla" : "0",
"id_juego" : "2",
"cliente" : "1",
"id_torneo" : "11",
"id_equipo" : "34",
"roja" : "0",
"id_jornada" : "14",
"minuto" : "1"
},
{
"gol" : "1",
"id_jugador" : "1",
"sucursal" : "1",
"id_arbitro" : "33",
"locvis" : "V",
"amarilla" : "0",
"id_juego" : "2",
"cliente" : "1",
"id_torneo" : "11",
"id_equipo" : "36",
"roja" : "0",
"id_jornada" : "14",
"minuto" : "1"
},
{
"gol" : "1",
"id_jugador" : "6",
"sucursal" : "1",
"id_arbitro" : "33",
"locvis" : "V",
"amarilla" : "0",
"id_juego" : "2",
"cliente" : "1",
"id_torneo" : "11",
"id_equipo" : "36",
"roja" : "0",
"id_jornada" : "14",
"minuto" : "1"
 },
 {
"gol" : "0",
"id_jugador" : "6",
"sucursal" : "1",
"id_arbitro" : "33",
"locvis" : "V",
"amarilla" : "1",
"id_juego" : "2",
"cliente" : "1",
"id_torneo" : "11",
"id_equipo" : "36",
"roja" : "0",
"id_jornada" : "14",
"minuto" : "25"
}
]

所以,Cocoa代码将数据发送到服务器

-(void) request
{
RequestPost *request = [[RequestPost alloc] init];
request.delegado = self;
NSString *postStr = [NSString stringWithFormat:@"datos=%@",self.json];
NSString *strUrl = @"http://www.futcho.com/WebService/subir_datos.php";
[request descargar:strUrl datosPost:postStr conId:100];
}

-(void)descargar:(NSString*)direccion datosPost:(NSString*)datos conId(NSInteger)id{
self.id = id;

NSURL *url            = [NSURL URLWithString:direccion];

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

NSString *strLength = [NSString stringWithFormat:@"%lu", (unsigned long)datos.length];

[req addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

[req addValue:strLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[datos dataUsingEncoding:NSUTF8StringEncoding]];


self.conexion = [[NSURLConnection alloc] initWithRequest:req delegate:self];

if(self.conexion){
    self.buffer = [NSMutableData data];
}
}

我必须将关于JSON格式的数据发送到Php界面,对吗?但是我认为我发送了NSString数据,你能帮帮我吗?

问候

0 个答案:

没有答案