You are here

protected function InstagramApiTrait::getInstagramResponceContents in Instagram Feeds 8

Sends request to Instagram and validates response status code.

Parameters

string $url: Url to get contents for.

bool $is_json_expected: Set true, in order to decode JSON data from response.

Return value

string|array Response body as string if $is_json_expected is false, or JSON decoded body.

Throws

\Exception

2 calls to InstagramApiTrait::getInstagramResponceContents()
CronHandler::getMedia in src/CronHandler.php
Gets 25 recent posts created by Instagram user.
InstagramPostsObtainedSubscriber::getAvatarFromPostPermalink in src/EventSubscriber/InstagramPostsObtainedSubscriber.php
Scrape the Instagram post owner avatar URL using permalink URL.

File

src/InstagramApiTrait.php, line 50

Class

InstagramApiTrait
Instagram API Trait.

Namespace

Drupal\instagram_feeds

Code

protected function getInstagramResponceContents($url, $is_json_expected = FALSE) {
  try {

    /** @var \Psr\Http\Message\ResponseInterface $response */
    $response = $this
      ->httpClient()
      ->get($url);
    if ($response
      ->getStatusCode() !== 200) {
      throw new \Exception(t('Invalid responce code @code from Instagram.', [
        '@code' => $response
          ->getStatusCode(),
      ]));
    }
    $body = $response
      ->getBody()
      ->getContents();
    return $is_json_expected ? Json::decode($body) : $body;
  } catch (\Exception $e) {
    $this
      ->logger()
      ->error($e
      ->getMessage());
  }
  return '';
}