<?php

$link =  mysql_connect('localhost', 'meri_meri', '%~qsKmpaTwC7');
if (!$link){
    die('<div class=\"error\">No pudo conectarse: ' . mysql_error() ."</div>");
}else{
    $db_selected = mysql_select_db('meri_meri', $link);
    if (!$db_selected){
        die ('<div class=\"error\">Can\'t use db : ' . mysql_error() . "</div>");
    }
}

function curl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
$url = "http://mx.bdicentralserver.com/api/categorias";
$categorias = json_decode(curl($url));
foreach ($categorias as $categoria) {
    $rs_cat = mysql_query("SELECT * FROM categorias_mx WHERE id = ".$categoria->id);
    if (mysql_num_rows($rs_cat)==0) {
    	$rs0 = mysql_query("INSERT INTO categorias_mx (id, nombre, parent_id) VALUES(".$categoria->id.",'".$categoria->nombre."',\"".$categoria->parent_id."\")");
    }
}
?>