class InstagramPostCollector in Social Feed 8
Class InstagramPostCollector.
@package Drupal\socialfeed
Hierarchy
- class \Drupal\socialfeed\Services\InstagramPostCollector
 
Expanded class hierarchy of InstagramPostCollector
File
- src/
Services/ InstagramPostCollector.php, line 12  
Namespace
Drupal\socialfeed\ServicesView source
class InstagramPostCollector {
  /**
   * Instagram application api key.
   *
   * @var string
   */
  protected $apiKey;
  /**
   * Instagram application api secret.
   *
   * @var string
   */
  protected $apiSecret;
  /**
   * Instagram application redirect Uri.
   *
   * @var string
   */
  protected $redirectUri;
  /**
   * Instagram application access token.
   *
   * @var string
   */
  protected $accessToken;
  /**
   * Instagram client.
   *
   * @var \EspressoDev\InstagramBasicDisplay\InstagramBasicDisplay
   */
  protected $instagram;
  /**
   * InstagramPostCollector constructor.
   *
   * @param string $apiKey
   *   Instagram API key.
   * @param string $apiSecret
   *   Instagram API secret.
   * @param string $redirectUri
   *   Instagram Redirect URI.
   * @param string $accessToken
   *   Instagram Access token.
   * @param \EspressoDev\InstagramBasicDisplay\InstagramBasicDisplay|null $instagram
   *   Instagram client.
   *
   * @throws \Exception
   */
  public function __construct($apiKey, $apiSecret, $redirectUri, $accessToken, InstagramBasicDisplay $instagram = NULL) {
    $this->apiKey = $apiKey;
    $this->apiSecret = $apiSecret;
    $this->redirectUri = $redirectUri;
    $this->accessToken = $accessToken;
    $this->instagram = $instagram;
    $this
      ->setInstagramClient();
  }
  /**
   * Set the Instagram client.
   *
   * @throws \Exception
   */
  public function setInstagramClient() {
    if (NULL === $this->instagram) {
      $this->instagram = new InstagramBasicDisplay([
        'appId' => $this->apiKey,
        'appSecret' => $this->apiSecret,
        'redirectUri' => $this->redirectUri,
      ]);
      $this->instagram
        ->setAccessToken($this->accessToken);
    }
  }
  /**
   * Retrieve user's posts.
   *
   * @param int $numPosts
   *   Number of posts to get.
   * @param string $user_id
   *   The user id from whom to get media. Defaults to the user that the access
   *   token was created for.
   *
   * @return array
   *   An array of Instagram posts.
   */
  public function getPosts($numPosts, $user_id = 'me') {
    $posts = [];
    $response = $this->instagram
      ->getUserMedia($user_id, $numPosts);
    if (isset($response->data)) {
      $posts = array_map(function ($post) {
        return [
          'raw' => $post,
          'media_url' => $post->media_url,
          'type' => $post->media_type,
          'children' => $post->children ?? NULL,
        ];
      }, $response->data);
    }
    return $posts;
  }
}Members
| 
            Name | 
                  Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| 
            InstagramPostCollector:: | 
                  protected | property | Instagram application access token. | |
| 
            InstagramPostCollector:: | 
                  protected | property | Instagram application api key. | |
| 
            InstagramPostCollector:: | 
                  protected | property | Instagram application api secret. | |
| 
            InstagramPostCollector:: | 
                  protected | property | Instagram client. | |
| 
            InstagramPostCollector:: | 
                  protected | property | Instagram application redirect Uri. | |
| 
            InstagramPostCollector:: | 
                  public | function | Retrieve user's posts. | |
| 
            InstagramPostCollector:: | 
                  public | function | Set the Instagram client. | |
| 
            InstagramPostCollector:: | 
                  public | function | InstagramPostCollector constructor. |