/**
  * Calls Twitter and gets a bearer token. This bearer token is valid until it gets invalidated.
  *
  * @link https://dev.twitter.com/docs/auth/application-only-auth Bearer Token Description
  *
  * @return $this AppConnection
  */
 public function createBearerToken()
 {
     //get bearer token credentials - to be used for getting the bearer token from Twitter.
     $bearerCredentials = $this->createBearerCredentials();
     //Required Headers
     $headers = array('Authorization' => 'Basic ' . $bearerCredentials, 'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8');
     //Required Body
     $body = 'grant_type=client_credentials';
     //Send a Post request to `oauth2/token` and convert the resulting JSON to assoc-array.
     $data = $this->guzzleClient->post(Config::get('oauth2_token'), array('headers' => $headers, 'body' => $body))->json();
     //Set the bearer token in the AppCredentials object
     $this->credentials->setBearerToken($data['access_token']);
     //Return the current object
     return $this;
 }
 /**
  * Upload media to Twitter and return a comma separated string containing their
  * media ID's to send with a status.
  *
  * @param  array $filepaths should be a maximum of 4
  * @param  GuzzleHttp\Client $client Optional. To inject your own instance of Guzzle. The base_url of the injected client should be set to Config::get('base_upload_url').
  *
  * @return string|false If a ServerException occurs, return false.
  */
 public function uploadMedia($filepaths, $client = null)
 {
     //maximum number of media files that a user can upload
     $maxMediaIds = Config::get('max_media_ids');
     //if number of media files supplied is larger than $maxMediaIds, throw exception.
     if (count($filepaths) > $maxMediaIds) {
         throw new MediaUploadLimitException("You cannot upload more than {$maxMediaIds} media files in a tweet!");
     }
     //array list of media id's uploaded
     $mediaIds = array();
     //create a new Guzzle client, if the user hasn't injected anything!
     if (is_null($client)) {
         $client = $this->createGuzzleClient(Config::get('base_upload_url'));
     }
     //prepend Twitter's API version to the endpoint
     $endpoint = $this->prependVersionToEndpoint("media/upload.json", Config::get('api_version'));
     //iterate over each filepath
     foreach ($filepaths as $filepath) {
         //contruct an options array to configure the request
         $options = $this->constructRequestOptions(array(), $client);
         //add body options to the POST request
         $options['body'] = array('media' => new PostFile('media', fopen($filepath, 'r')));
         //make the POST request to the endpoint with the constructed options.
         $response = $client->post($endpoint, $options);
         //add media_id to array
         array_push($mediaIds, $response->json()['media_id_string']);
     }
     //return all media ID's as a string (comma seperated)
     return implode(",", $mediaIds);
 }
 /**
  * Make a POST request to the endpoint. Also appends query params to the URL.
  *
  * @param  string $endpoint The endpoint to send a POST request to.
  * @param  array $params   associative array for the query parameters.
  *
  * @link   http://guzzle.readthedocs.org/en/latest/quickstart.html#using-responses ResponseInterface details.
  *
  * @return GuzzleHttp\Message\ResponseInterface API Response. Contains a lot of information.
  */
 public function post($endpoint, $params)
 {
     //prepend Twitter's API version to the endpoint
     $endpoint = $this->prependVersionToEndpoint($endpoint, Config::get('api_version'));
     //contruct an options array to configure the request
     $options = $this->constructRequestOptions($params);
     //make the GET request to the endpoint with the constructed options.
     $response = $this->guzzleClient->post($endpoint, $options);
     //return response
     return $response;
 }