Created
January 12, 2010 21:57
-
-
Save boriscy/275664 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
$response = curl_getinfo( $ch ); | |
curl_close($ch); | |
return $content; | |
} | |
/** | |
* 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'], | |
'importar[archivo_tmp]' => "@$archivo", | |
'importar[archivo_nombre]' => $archivo | |
); | |
// Areas | |
foreach($params['areas'] as $k => $v) { | |
$arr["importar[areas][$k]"] = $v; | |
} | |
return $arr; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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/importares", "json"); | |
$resp = $rest->postDatos($_POST, realpath($target)); | |
print_r(json_decode($resp)); | |
} | |
} | |
?> | |
<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="areas[]" 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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment