Ver Mensaje Individual
  #3 (permalink)  
Antiguo 04/03/2013, 09:07
Avatar de difilippocarlos
difilippocarlos
 
Fecha de Ingreso: junio-2010
Mensajes: 109
Antigüedad: 13 años, 10 meses
Puntos: 1
Respuesta: Consultas contra una BD SQL Web

no me entendiste...

lo que quiero es conectarme a una api de sql, y guardar en una matriz el resultado...
Drupal hace algo como esto pero no me esta funcionando
Código:
    $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($data)
    );

    if (isset($uri['user'])) {
        $defaults['Authorization'] = 'Authorization: Basic ' . base64_encode($uri['user'] . (!empty($uri['pass']) ? ":" . $uri['pass'] : ''));
    }

    foreach ($headers as $header => $value) {
        $defaults[$header] = $header . ': ' . $value;
    }

    $request = $method . ' ' . $path . " HTTP/1.0\r\n";
    $request .= implode("\r\n", $defaults);
    $request .= "\r\n\r\n";
    if ($data) {
        $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;
    return $result;