06/03/2013, 05:51
|
| | | Fecha de Ingreso: junio-2010
Mensajes: 109
Antigüedad: 14 años, 6 meses Puntos: 1 | |
Respuesta: Consultas contra una BD SQL Web No pude hacerlo como me lo habias pasado vos... pero te dejo como lo hice...
Cree 2 funciones
Código:
function leerApiSQL($url) {
$cabeceras = array();
$metodo = 'GET';
$datos = NULL;
$intentos = 3;
$result = new stdClass();
$uri = parse_url($url);
switch ($uri['scheme']) {
case 'http':
$port = isset($uri['port']) ? $uri['port'] : 80;
$host = $uri['host'] . ($port != 80 ? ':' . $port : '');
$fp = @fsockopen($uri['host'], $port, $errno, $errstr, 15);
break;
case 'https':
// Note: Only works for PHP 4.3 compiled with OpenSSL.
$port = isset($uri['port']) ? $uri['port'] : 443;
$host = $uri['host'] . ($port != 443 ? ':' . $port : '');
$fp = @fsockopen('ssl://' . $uri['host'], $port, $errno, $errstr, 20);
break;
default:
$result->error = 'invalid schema ' . $uri['scheme'];
return $result;
}
if (!$fp) {
$result->error = trim($errno . ' ' . $errstr);
$result->code = -$errno;
return $result;
}
$path = isset($uri['path']) ? $uri['path'] : '/';
if (isset($uri['query'])) {
$path .= '?' . $uri['query'];
}
$defaults = array(
'Host' => "Host: $host",
'User-Agent' => 'User-Agent: Drupal (+http://drupal.org/)',
'Content-Length' => 'Content-Length: ' . strlen($datos)
);
if (isset($uri['user'])) {
$defaults['Authorization'] = 'Authorization: Basic ' . base64_encode($uri['user'] . (!empty($uri['pass']) ? ":" . $uri['pass'] : ''));
}
foreach ($cabeceras as $header => $value) {
$defaults[$header] = $header . ': ' . $value;
}
$request = $metodo . ' ' . $path . " HTTP/1.0\r\n";
$request .= implode("\r\n", $defaults);
$request .= "\r\n\r\n";
if ($datos) {
$request .= $data . "\r\n";
}
$result->request = $request;
fwrite($fp, $request);
$response = '';
while (!feof($fp) && $chunk = fread($fp, 1024)) {
$response .= $chunk;
}
fclose($fp);
list($split, $result->data) = explode("\r\n\r\n", $response, 2);
$split = preg_split("/\r\n|\n|\r/", $split);
list($protocol, $code, $text) = explode(' ', trim(array_shift($split)), 3);
$result->headers = array();
while ($line = trim(array_shift($split))) {
list($header, $value) = explode(':', $line, 2);
if (isset($result->headers[$header]) && $header == 'Set-Cookie') {
$result->headers[$header] .= ',' . trim($value);
} else {
$result->headers[$header] = trim($value);
}
}
$responses = array(
100 => 'Continue', 101 => 'Switching Protocols',
200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content',
300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect',
400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large', 415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable', 417 => 'Expectation Failed',
500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Time-out', 505 => 'HTTP Version not supported'
);
if (!isset($responses[$code])) {
$code = floor($code / 100) * 100;
}
switch ($code) {
case 200: // OK
case 304: // Not modified
break;
case 301: // Moved permanently
case 302: // Moved temporarily
case 307: // Moved temporarily
$location = $result->headers['Location'];
if ($retry) {
$result = drupal_http_request($result->headers['Location'], $headers, $method, $data, --$retry);
$result->redirect_code = $result->code;
}
$result->redirect_url = $location;
break;
default:
$result->error = $text;
}
$result->code = $code;
$doc = simplexml_load_string($result->data);
return $doc;
}
function leerXML($doc, $var, $long) {
$path = $doc->xpath($var);
if (isset($path[0])) {
$fact = (string) $path[0];
$fact = substr($fact, -$long, $long);
} else {
$fact = 1;
}
return $fact;
}
y las llamo desde mi funcion principal
Código:
function obtenerCliente($cc) {
$resultado = FALSE;
$nrocliente = $cc;
$url = 'http://200.43.54.146:88/coop-api/CustomerTransactions/lastInvoice/';
$url .= $nrocliente;
$doc = leerApiSQL($url);
$dato = '//@CustomerID'; //Simpre usar el formato //@Dato
$fact = leerXML($doc, $dato, 5); //el numero del final es la cantidad de caracteres q queremos
if ($fact == $nrocliente) {
$resultado = TRUE;
}
return $resultado;
}
|