Skip to content

Instantly share code, notes, and snippets.

@boriscy
Created January 8, 2010 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boriscy/272450 to your computer and use it in GitHub Desktop.
Save boriscy/272450 to your computer and use it in GitHub Desktop.
<?php
if($_POST['login']) {
//echo $_FILES['archivo']['tmp_name'];
//print_r($_POST);
include_once('RestMotoEx.php');
$target = 'archivos/'.basename($_FILES['archivo']['name']);
if(move_uploaded_file($_FILES['archivo']['tmp_name'], $target)) {
$rest = new RestMotoEx("http://localhost:3000/importar", "json");
$rest->postDatos($_POST, realpath($target));
}
}
?>
<form method="post" enctype="multipart/form-data" action="testMotoEx.php">
<ul>
<li>
<label>login</label><input type="text" name="login" id="login">
</li>
<li>
<label>Password</label><input type="text" name="password" id="password">
</li>
<li>
<label>Archivo</label><input type="file" name="archivo" id="archivo">
</li>
<li>
<label>Areas</label>
<select name="area[]" multiple="multiple">
<option value="1">Area 1</option>
<option value="2">Area 2</option>
<option value="3">Area 3</option>
</select>
</li>
</ul>
<input type="submit" value="Ingresar" />
</form>
<?php
/**
* Cliente REST para poder consumir el servicio WEB de MotoEx
* @author: Boris Barroso
* @license: GNU/GPL
*/
class RestMotoEx {
public $url, $format;
/**
* Constructor
* @param String url
* @param String format # Formatos de los cuales estan: (json, xml, csv y yaml)
*/
public function __construct($url, $format = 'json') {
if(!function_exists('curl_version')) {
die("Debe isntalar php5-curl y CURL <pre>apt-get install curl<br/>apt-get install php5-culr</pre>");
}
$this->url = $url.'.'.$format;
$this->format = $format;
}
/**
* Envia los datos al url definido
* @parm Array $data
* @param File $archivo
*/
public function postDatos($data, $archivo) {
$ch = curl_init();
$datos = $this->crearDatos($data, $archivo);
//curl_setopt_array($ch, $this->crearCurlOpciones($datos));
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datos);
$content = curl_exec( $ch );
print_r($content); //
$response = curl_getinfo( $ch );
//print_r( $response ); //
curl_close($ch);
}
/**
* Prepara los datos a ser enviados al servicio
* @param Array $params
* @param File $archivo
* @return Array
*/
private function crearDatos($params, $archivo) {
$arr = array(
'login' => $params['login'],
'password' => $params['password'],
'archivo' => "@$archivo",
'archivo_nombre' => $archivo,
);
// Areas
foreach($params['area'] as $k => $v) {
$arr["area[$k]"] = $v;
}
return $arr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment