Example #1
0
 /**
  * Initializes the HTTP Client
  */
 private function initializeClient()
 {
     $this->client = new GuzzleClient($this->config->getBaseUrl());
     $this->client->setDefaultOption('headers', $this->getHeaders());
     $this->client->getConfig()->set('curl.options', array(CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4));
     if ($this->config->getDebug()) {
         $this->client->addSubscriber(LogPlugin::getDebugPlugin());
     }
 }
Example #2
0
 function __construct()
 {
     $client = new Client('https://api.twitter.com/1.1/');
     $oauth = new OauthPlugin(['consumer_key' => 'NkGX5YSyTPPz2ONSA6nnTMjEv', 'consumer_secret' => 'stqZsBkUj6qOu1Jokv1vulwToqSd7ce8XqZEV17s6DVT1tqDoc', 'token' => '110823516-PyPFyDZwRZPDkZBdv3MkQfod8N1k6z6IWdYBocBE', 'token_secret' => 'mjrwp48ujjZ90ljLx3p8EVXAF8FrHPgL878UsoHNo51xT']);
     $client->addSubscriber($oauth);
     $this->client = $client;
 }
 /**
  * Register the service provider.
  *
  * @return void
  */
 public function register()
 {
     $this->app->bind('twitter', function () {
         $client = new Client('https://api.twitter.com/1.1');
         $auth = new OauthPlugin(['consumer_key' => Config::get('twitter.consumer_key'), 'consumer_secret' => Config::get('twitter.consumer_secret'), 'token' => Config::get('twitter.token'), 'token_secret' => Config::get('twitter.token_secret')]);
         $client->addSubscriber($auth);
         return new TwitterAPI($client);
     });
 }
 public function register()
 {
     $this->app->bind('yelp', function () {
         $client = new Client('http://api.yelp.com/v2/');
         $auth = new OauthPlugin(['consumer_key' => Config::get('yelp.consumer_key'), 'consumer_secret' => Config::get('yelp.consumer_secret'), 'token' => Config::get('yelp.token'), 'token_secret' => Config::get('yelp.token_secret')]);
         $client->addSubscriber($auth);
         return new YelpAPI($client);
     });
 }
 public function createEchaleGasClient()
 {
     $client = new Client();
     $client->setDescription(ServiceDescription::factory('config/services/echalegas.json'));
     $logger = new Logger('debug');
     $logger->pushHandler(new StreamHandler('logs/debug.log'));
     $logPlugin = new LogPlugin(new MonologLogAdapter($logger), MessageFormatter::DEBUG_FORMAT);
     $client->addSubscriber($logPlugin);
     return $client;
 }
 protected function getMockedClient(Response $response)
 {
     $operation = new Operation(array('httpMethod' => 'GET', 'name' => 'Mock'));
     $service = new ServiceDescription();
     $service->addOperation($operation);
     $plugin = new MockPlugin();
     $plugin->addResponse($response);
     $client = new Client();
     $client->setDescription($service);
     $client->addSubscriber($plugin);
     return $client;
 }
Example #7
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     \Log::info('Fetch tweets');
     $client = new Client('https://api.twitter.com/1.1/');
     $auth = new Oauth(['consumer_key' => env('TWITTER_CONSUMER_KEY'), 'consumer_secret' => env('TWITTER_CONSUMER_SECRET'), 'token' => env('TWITTER_ACCESS_TOKEN'), 'token_secret' => env('TWITTER_ACCESS_TOKEN_SECRET')]);
     $client->addSubscriber($auth);
     //$response = $client->get('search/tweets.json?q=%23peukpoll&since=' . date('Y-m-d'))->send();
     $response = $client->get('search/tweets.json?q=%23peukpoll')->send();
     $data = $response->json()['statuses'];
     $tweets = array_fetch($response->json()['statuses'], 'text');
     foreach ($tweets as $tweet) {
         $values[] = explode(' ', $tweet);
     }
     for ($i = 0; $i < count($values); $i++) {
         $user = User::where('username', $data[$i]['user']['screen_name'])->first();
         if ($user == null) {
             $user = new User();
             $user->username = $data[$i]['user']['screen_name'];
             $user->save();
         }
         $tweet = Tweet::where('tweet', $data[$i]['id'])->first();
         if ($tweet == null) {
             $tweet = new Tweet();
             $tweet->x = $values[$i][0];
             $tweet->y = $values[$i][2];
             $tweet->score_x = 1;
             $tweet->score_y = 1;
             $tweet->tweet = $data[$i]['id'];
             $tweet->user()->associate($user);
             $tweet->save();
             $numberOfUpcomingTweets = UpcomingTweet::all()->count();
             if ($numberOfUpcomingTweets > 0) {
                 $upcomingTweet = new UpcomingTweet();
                 $upcomingTweet->end = date('Y-m-d H:i:s', strtotime('+ 1 year'));
                 $upcomingTweet->tweet()->associate($tweet);
                 $upcomingTweet->save();
             } else {
                 $upcomingTweet = new UpcomingTweet();
                 $upcomingTweet->end = date('Y-m-d H:i:s', strtotime('+ 1 hour'));
                 $upcomingTweet->tweet()->associate($tweet);
                 $upcomingTweet->save();
             }
         }
     }
 }
Example #8
0
 /**
  * PHP 5.3 compatibility services
  *
  * @deprecated
  *
  * @param Application $app
  */
 private function compat(Application $app)
 {
     // Register a Guzzle ServiceBuilder
     $app['guzzle'] = $app->share(function () use($app) {
         if (!isset($app['guzzle.services'])) {
             $builder = new ServiceBuilder(array());
         } else {
             $builder = ServiceBuilder::factory($app['guzzle.services']);
         }
         return $builder;
     });
     // Register a simple Guzzle Client object (requires absolute URLs when guzzle.base_url is unset)
     $app['guzzle.client'] = $app->share(function () use($app) {
         $client = new ServiceClient($app['guzzle.base_url']);
         foreach ($app['guzzle.plugins'] as $plugin) {
             $client->addSubscriber($plugin);
         }
         return $client;
     });
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $repositoryName = $this->parseRepositoryName($input->getOption('repository'));
     $client = new Client($input->getOption('api-url') . '{?access_token}', array('access_token' => $input->getOption('access-token'), 'request.options' => array('Content-Type' => 'application/json')));
     $client->addSubscriber(BackoffPlugin::getExponentialBackoff());
     $postData = $this->generatePostData($input);
     if (!isset($postData['coverage'])) {
         $output->write(sprintf('Notifying that no code coverage data is available for repository "%s" and revision "%s"... ', $repositoryName, $postData['revision']));
     } else {
         $output->write(sprintf('Uploading code coverage for repository "%s" and revision "%s"... ', $repositoryName, $postData['revision']));
     }
     try {
         $client->post('repositories/' . $repositoryName . '/data/code-coverage{?access_token}', null, json_encode($postData))->send();
         $output->writeln('Done');
         return 0;
     } catch (BadResponseException $ex) {
         $output->writeln("<error>Failed</error>");
         if ($ex instanceof ClientErrorResponseException) {
             $output->writeln('<error>' . $ex->getResponse()->getBody(true) . '</error>');
             return 1;
         }
         throw $ex;
     }
 }
Example #10
0
 /**
  * Test array of models in response defined with wrapper model containing an array property
  * @group mock
  * @depends testClientConstruct
  */
 public function testModelListObjectResponse(Client $client)
 {
     // fake a response with multiple valid objects
     $plugin = new MockPlugin();
     $plugin->addResponse(new Response(200, array(), '{"list":[{"foo":4},{"foo":5},{"foo":6}]}'));
     $client->addSubscriber($plugin);
     $response = $client->testwrapobj();
     // test response is a model
     $this->assertInstanceof('\\Guzzle\\Service\\Resource\\Model', $response);
     // test response has 3 items
     $list = $response->get('list');
     $this->assertCount(3, $list);
 }
Example #11
0
 /**
  * Authorize the user on the remote server
  * @param  Input $input
  * @return GuzzleClient
  */
 public function authorizeRemote($input)
 {
     $client = new GuzzleClient($input['remote_url']);
     $client->setSslVerification(FALSE);
     $cookieJar = new ArrayCookieJar();
     // Create a new cookie plugin
     $cookiePlugin = new CookiePlugin($cookieJar);
     // Add the cookie plugin to the client
     $client->addSubscriber($cookiePlugin);
     $post_data = array('username' => $input['username'], 'password' => $input['password'], 'remember' => 'checked', 'api' => true);
     $response = $client->post('login/backend', array(), $post_data)->send();
     $response_json = $response->json();
     if (isset($response_json['error'])) {
         throw new Exception($response_json['error']);
     }
     return $client;
 }
Example #12
0
 /**
  * Adds a LogPlugin to a Guzzle service client object
  *
  * This will cause any requests created by the client to log all
  * requests sent and received.
  *
  * @param Guzzle\Service\Client $client The Guzzle service client
  */
 protected function logClient(Client $client)
 {
     $client->addSubscriber(LogPlugin::getDebugPlugin());
 }
 /**
  * Add a guzzle plugin to the client.
  * This is mainly for testing, but also useful for logging, validation, etc.
  *
  * @param mixed $plugin A guzzle plugin
  */
 public function addSubscriber($plugin)
 {
     $this->client->addSubscriber($plugin);
 }
 /**
  * @param Client $client
  * @param $config
  */
 public static function toggleRateLimitingPlugin(Client $client, $config)
 {
     if (array_key_exists('disable_rate_limiting', $config) && $config['disable_rate_limiting']) {
         return;
     }
     $rateFactor = array_key_exists('rate_limit_factor', $config) ? $config['rate_limit_factor'] : null;
     $client->addSubscriber(new RateLimitPlugin($rateFactor));
 }
Example #15
0
require '../vendor/autoload.php';
use Guzzle\Service\Client;
use Guzzle\Service\Description\ServiceDescription;
use Guzzle\Log\MessageFormatter;
use Guzzle\Log\MonologLogAdapter;
use Guzzle\Plugin\Log\LogPlugin;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Faker\Factory;
$faker = Factory::create();
$client = new Client();
$client->setDescription(ServiceDescription::factory('../config/config.json'));
$logger = new Logger('debug');
$logger->pushHandler(new StreamHandler('../logs/debug.log'));
$logPlugin = new LogPlugin(new MonologLogAdapter($logger), MessageFormatter::DEBUG_FORMAT);
$client->addSubscriber($logPlugin);
$contact = $client->showContact(['contactId' => 1, 'accept' => 'application/json']);
echo "\nShow contact information {$contact['name']} {$contact['last_name']}\n";
$newContact = ['name' => $faker->firstName, 'lastName' => $faker->lastName];
$contact = $client->newContact(array_merge($newContact, ['accept' => 'application/xml']));
echo <<<MESSAGE

New contact information {$contact['name']} {$contact['last_name']} with ID {$contact['contact_id']}

MESSAGE;
$newName = $faker->firstName;
$contact = $client->editContact(['contactId' => 2, 'name' => $newName, 'accept' => 'application/json']);
echo <<<MESSAGE

Edit name {$newName}. Updated contact information: {$contact['name']} {$contact['last_name']} with ID {$contact['contact_id']}
Example #16
0
 /**
  * @param $config
  */
 public function setAccessToken($config)
 {
     $oauth2Plugin = new \Gitlab\Auth\Oauth2\Plugin($this, $config);
     $this->client->addSubscriber($oauth2Plugin);
 }
 /**
  * @param EventSubscriberInterface $plugin
  */
 public function addGuzzlePlugin(EventSubscriberInterface $plugin)
 {
     $this->client->addSubscriber($plugin);
 }