public function batch($requests, $options = []) { $this->lastError = null; $this->lastRequest = null; $this->lastResponse = null; if (empty($this->accessKey)) { $this->lastError = "No access key set. Please set an access key before attempting to fetch forecast data."; } if (!$this->httpClient) { $this->httpClient = new \GuzzleHttp\Client(); } $baseURL = rtrim(self::ENDPOINT, '/') . '/' . $this->accessKey . '/'; $httpRequestPromises = []; foreach ($requests as $idx => $request) { $url = $baseURL . $request['latitude'] . ',' . $request['longitude']; if (!empty($request['time'])) { $url .= ',' . $request['time']; } if ($options) { $url .= '?' . http_build_query($options); } $requests[$idx]['url'] = $url; $httpRequestPromises[$idx] = $this->httpClient->getAsync($url, ['allow_redirects' => true, 'timeout' => $this->timeout, 'connect_timeout' => $this->connectTimeout, 'headers' => ['User-Agent' => 'Tave/ForecastIOWrapper']]); } try { $httpResults = \GuzzleHttp\Promise\unwrap($httpRequestPromises); foreach ($httpResults as $idx => $httpResponse) { if ($httpResponse instanceof \GuzzleHttp\Exception\RequestException) { $this->lastRequest = $requests[$idx]; $this->lastResponse = $httpResponse; $this->lastError = $httpResults[$resultIdx]->getMessage(); $requests[$idx]['error'] = $this->lastError; } else { $requests[$idx]['forecast'] = json_decode($httpResponse->getBody(), true); } } return $requests; } catch (\GuzzleHttp\Exception\RequestException $e) { $this->lastRequest = $e->getRequest(); $this->lastResponse = $e->hasResponse() ? $e->getResponse() : null; $this->lastError = $e->getMessage(); return false; } }
/** * Bridge to connect with Instagram API * * @param string $endpoint Endpoint of Instagram * @param string $useAccessToken OAuth access token * @param array $params Array of params to send in request * @param string $method HTTP Method of request * @return object Response of Instagram */ protected function communicate($endpoint, $useAccessToken = false, $params = [], $method = 'GET') { if (!isset($this->accessToken)) { throw new InstagramException('The access token is undefined'); } $endpoint = self::API_URI . $endpoint; $formParams = []; $headerParams = ['Accept' => 'application/json']; $queryParams = $useAccessToken ? ['access_token' => $this->getAccessToken()] : ['client_id' => $this->getApiKey()]; if (count($params) > 0 && is_array($params)) { if ('GET' === $method) { $queryParams = array_unique(array_merge($queryParams, $params), SORT_REGULAR); } else { $formParams = $params; } } $guzzleData = ['headers' => $headerParams, 'query' => $queryParams]; if (count($formParams) > 0 && 'GET' !== $method) { $guzzleData['form_params'] = $formParams; } $response = $this->client->request($method, $endpoint, $guzzleData); return json_decode($response->getBody()); }
/** * Get HTTP status code for URL * @param string $url The remote URL * @return int The HTTP status code */ protected function getStatusCodeForUrl($url) { $httpResponse = $this->httpClient->options($url); return $httpResponse->getStatusCode(); }