/** * @brief Conexão HTTP * @details Recupera um objeto de conexão HTTP para ser utilizado * nas chamadas às operações da API. * @return HTTPConnection */ public function getHTTPConnection() { $httpConnection = new HTTPConnection(); $httpConnection->setAuthenticator($this->httpAuthenticator); $httpConnection->setCookieManager(new HTTPCookieManager()); $httpConnection->initialize($this->host, $this->secure, $this->port); return $httpConnection; }
/** * @param string * @param array (string key => mixed value, ...) * @param string "POST"|"GET" * @return array XML DOM * @throws HTTPException * @throws XMLParserException */ public function call($url, $params = null, $method = 'GET') { if ($params && is_array($params)) { $vv = array(); foreach ($params as $k => $v) { if (is_array($v)) { foreach ($v as $v2) { $vv[] = rawurlencode($k) . '=' . rawurlencode($v2); } } else { $vv[] = rawurlencode($k) . '=' . rawurlencode($v); } } $params = implode('&', $vv); } else { $params = ''; } $conn = new HTTPConnection(strval($url)); $conn->method = $method; $conn->curlOptions[CURLOPT_USERAGENT] = 'RESTService $Id$'; $conn->curlOptions[CURLOPT_CONNECTTIMEOUT] = 10; # short connect timeout if ($method != 'POST' && $params) { $conn->url .= '?' . $params; $params = null; } if ($this->debug) { print 'DEBUG>> RESTService->call(' . $service_id . ':' . $name . '): ' . ($conn->method == 'POST' ? 'POST' : 'GET') . ' ' . $conn->url . "<br/>\n"; if ($conn->method == 'POST') { print 'DEBUG>> POST data: ' . var_export($params, 1) . "\n<br/>"; } } # Send request $data = $conn->connect($params); if ($this->debug) { print 'DEBUG>> RESTService->call(' . $service_id . ':' . $name . '): Response: HTTP/' . $conn->responseProtocol . ' ' . $conn->responseStatus . "<br/>\n{$data}\n<br/>"; } # Parse response $data = XML::loadString($data); # Handle response errors if (substr(strval($conn->responseStatus), 0, 1) != '2') { throw new HTTPException($conn->responseStatusName, $conn->responseStatus); } return $data; }
/** * @brief Constroi o objeto de conexão HTTP. */ public function __construct() { if (self::$userAgent == null) { $locale = setlocale(LC_ALL, null); if (function_exists('posix_uname')) { $uname = posix_uname(); self::$userAgent = sprintf('Mozilla/4.0 (compatible; %s; PHP/%s; %s %s; %s)', PHP_SAPI, PHP_VERSION, $uname['sysname'], $uname['machine'], $locale); } else { self::$userAgent = sprintf('Mozilla/4.0 (compatible; %s; PHP/%s; %s; %s)', PHP_SAPI, PHP_VERSION, PHP_OS, $locale); } } $this->requestHeader = array(); $this->requestParameter = array(); }
/** * @brief Constroi o objeto que representa uma API do Correios * @param ECT $ect */ public function __construct(ECT $ect) { $this->ect = $ect; $this->httpConnection = $ect->getHTTPConnection(); $this->httpConnection->initialize($this->getTargetHost()); }
/** * @see HTTPRequest::open() */ public function open(HTTPConnection $httpConnection) { if (function_exists('curl_init')) { /** * Fechamos uma conexão existente antes de abrir uma nova */ $this->close(); $curl = curl_init(); /** * Verificamos se o recurso CURL foi criado com êxito */ if (is_resource($curl)) { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLINFO_HEADER_OUT, 1); if (($timeout = $httpConnection->getTimeout()) != null) { curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); } if (($connectionTimeout = $httpConnection->getConnectionTimeout()) != null) { curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $connectionTimeout); } $headers = array(); foreach ($this->requestHeader as $header) { $headers[] = sprintf('%s: %s', $header['name'], $header['value']); } curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); $this->curlResource = $curl; $this->httpConnection = $httpConnection; $this->openned = true; } else { throw new RuntimeException('Não foi possível iniciar cURL'); } } else { throw new RuntimeException('Extensão cURL não está instalada.'); } }
/** * @return HTTPConnection */ public function getHTTPConnection() { $httpConnection = new HTTPConnection(); $httpConnection->addHeader( 'X-PAYPAL-APPLICATION-ID' , $this->applicationId ); $httpConnection->addHeader( 'X-PAYPAL-REQUEST-DATA-FORMAT' , $this->requestDataFormat ); $httpConnection->addHeader( 'X-PAYPAL-RESPONSE-DATA-FORMAT' , $this->responseDataFormat ); $httpConnection->addHeader( 'X-Target-URI' , $this->targetHost ); $httpConnection->setAuthenticator( $this->authenticator ); $httpConnection->setCookieManager( new HTTPCookieManager() ); $httpConnection->initialize( $this->targetHost , true ); return $httpConnection; }
public function open(HTTPConnection $httpConnection) { $error = null; $errno = 0; $this->handler = fsockopen($httpConnection->getHost(), $httpConnection->getPort(), $errno, $error, $httpConnection->getConnectionTimeout()); }
/** * @brief Conexão HTTP * @details Recupera um objeto de conexão HTTP para ser utilizado * nas chamadas às operações da API. * @return HTTPConnection */ public function getHTTPConnection() { $httpConnection = new HTTPConnection(); $httpConnection->setCookieManager(new HTTPCookieManager()); return $httpConnection; }
/** * @brief Efetua a chamada à operação da API * @return stdClass */ public function call() { $this->httpResponse = $this->httpConnection->execute('/json-api/' . $this->getOperationName()); return json_decode($this->httpResponse->getContent()); }
/** * Returns the size of the request. * * @access public * @return integer */ public function get_request_size() { return $this->connection->get_bytes_read(); }