public function request(TreasureData_API_Request $request)
 {
     $context = stream_context_create(array('http' => array('method' => $request->getRequestMethod(), 'header' => $request->getHeadersAsString(), 'proxy' => $request->getProxy(), 'content' => $request->getContentBody(), 'request_fulluri' => $request->hasProxy(), 'ignore_errors' => true)));
     /* Note: stream_socket_client does not support http(s) protocol. we have to use fopen here. */
     $socket = fopen($request->getUrl(), 'r', false, $context);
     if (!is_resource($socket)) {
         throw new Exception("can't create stream socket.");
     }
     $headers = array();
     $this->socket = $socket;
     $meta_data = stream_get_meta_data($socket);
     if (isset($meta_data['wrapper_data'])) {
         foreach ($meta_data['wrapper_data'] as $value) {
             if (strpos($value, "HTTP/") === 0) {
                 list($dummy, $status, $dummy) = explode(" ", $value, 3);
                 $headers['HTTP_STATUS'] = $status;
             } else {
                 list($key, $value) = explode(":", $value, 2);
                 $headers[$key] = $value;
             }
         }
     }
     if ($headers['HTTP_STATUS'][0] != 2) {
         throw new TreasureData_API_Exception_HTTPException(sprintf("API Server returns %s code: %s", $headers['HTTP_STATUS'], fread($this->socket, 8192)));
     }
     $stream = new TreasureData_API_Stream_InputStream($this->socket);
     $response = new TreasureData_API_Response($request, $stream, $headers);
     return $response;
 }
 public function testRequestBuilder()
 {
     $builder = new TreasureData_API_RequestBuilder();
     $builder->setQuery("/job/status/1");
     $result = array("scheme" => "https", "port" => 443, "host" => "https://api.treasure-data.com");
     $result['request_method'] = $builder->getRequestMethod();
     $info = parse_url($builder->getEndPoint());
     if (isset($info['scheme'])) {
         $result['scheme'] = $info['scheme'];
     }
     if (isset($info['host'])) {
         $result['host'] = $info['host'];
         //$address = gethostbyname($info['host']);
         //$request->addAddress($address);
     }
     if (isset($info['port'])) {
         $result['port'] = $info['port'];
     } else {
         if ($result['scheme'] == 'http') {
             $result['port'] = 80;
         }
     }
     $result['params'] = $builder->getParams();
     if ($builder->getAuthentication()) {
         $result['headers']['Authorization'] = $builder->getAuthentication()->getAsString();
     }
     if ($builder->getUserAgent()) {
         $result['headers']["User-Agent"] = $this->getUserAgent();
     }
     if ($builder->isPost()) {
         $data = http_build_query($builder->getParams());
         $query = '/' . $builder->getApiVersion() . '/' . ltrim($builder->getQuery(), "/");
         $result['query_string'] = $query;
         $result['headers']['Content-Type'] = "application/x-www-form-urlencoded";
         $result['headers']['Content-Length'] = strlen($data);
         $result['content_body'] = $data;
     } else {
         if ($builder->hasParams()) {
             $query = '/' . $builder->getApiVersion() . '/' . ltrim($builder->getQuery(), "/") . '?' . http_build_query($builder->getParams());
         } else {
             $query = '/' . $builder->getApiVersion() . '/' . ltrim($builder->getQuery(), "/");
         }
         $result['query_string'] = $query;
     }
     $result['url'] = sprintf("%s://%s%s", $result['scheme'], $result['host'], $query);
     $result['gzip_hint'] = $builder->getGzipHint();
     $request = new TreasureData_API_Request($result);
     $this->assertEquals("/v3/job/status/1", $request->getQueryString());
     $this->assertEquals("GET", $request->getRequestMethod());
     $this->assertEquals("https", $request->getScheme());
     $this->assertEquals(sprintf("%s://%s%s", $result['scheme'], $result['host'], $query), $request->getUrl());
 }
 public function request(TreasureData_API_Request $request)
 {
     $curl = curl_copy_handle($this->curl);
     curl_setopt($curl, CURLOPT_URL, $request->getUrl());
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($curl, CURLOPT_PORT, $request->getPort());
     $headers = array();
     foreach ($request->getHeaders() as $key => $value) {
         $headers[] = sprintf("%s: %s", $key, $value);
     }
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     if ($request->isPost()) {
         curl_setopt($curl, CURLOPT_POST, 1);
         curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($request->getParams()));
     }
     $result = curl_exec($curl);
     $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     if ($http_code[0] == "4") {
         throw new TreasureData_API_Exception_HTTPException(sprintf("API Server returns %s code: %s", $http_code, $result));
     }
     $headers = array();
     $response = new TreasureData_API_Response($request, new TreasureData_API_Stream_InputStream($this->socket), $headers);
     return $response;
 }