Example #1
0
 /**
  * @param array $data
  * Data that will be sent to the Graph
  *
  * @param string|null $path
  * The path of the request. Some operations like Batch don't need it.
  * If the $path is complete, with all query parameters, $data will be ignored
  *
  * @return \stdClass[]
  */
 public function curl($data = array(), $path = '', $method = 'post')
 {
     $fails = 0;
     $this->guzzle_client->setBaseUrl('https://graph.facebook.com');
     if (substr($path, 0, 26) != 'https://graph.facebook.com') {
         $data['access_token'] = $this->getAccessToken();
     }
     do {
         if ($fails > 0) {
             usleep($fails * 20000 + rand(0, 1000000));
         }
         $result = null;
         switch ($method) {
             case 'get':
                 $path = $this->buildQuery($data, $path);
                 $request = $this->guzzle_client->get($path);
                 break;
             case 'post':
                 foreach ($data as &$data_param) {
                     if (is_array($data_param)) {
                         $data_param = json_encode($data_param);
                     }
                 }
                 $request = $this->guzzle_client->post($path, array(), $data);
                 break;
             default:
                 throw new \Exception('Available methods: post or get');
                 break;
         }
         try {
             $response = $request->send();
         } catch (ClientErrorResponseException $e) {
             throw new OAuthException('Error', 0, $e);
         }
         if ($fails > self::MAX_REQUEST_ATTEMPTS) {
             throw new \Exception('Failed to connect to server ' . self::MAX_REQUEST_ATTEMPTS . ' times');
         }
         $fails++;
         $result = json_decode($response->getBody(true), false, 512, JSON_BIGINT_AS_STRING);
     } while ($this->isNotValidResponse($result));
     if (isset($result->paging) && isset($result->paging->next) && $result->paging->next != null) {
         //$next_result = $this->curl(array(), $result->paging->next, 'get');
         usleep(1000);
         $next_result = $this->curl([], $result->paging->next, 'get');
         /** @noinspection PhpUndefinedFieldInspection */
         $result->data = array_merge($result->data, $next_result->data);
     }
     return $result;
 }
 /**
  * Get all the users email addresses
  *
  * @param  string $token
  * @return mixed
  */
 protected function getVerifiedEmails($token)
 {
     $url = 'https://api.github.com/user/emails?access_token=' . $token;
     try {
         $client = new GuzzleClient();
         $client->setBaseUrl($url);
         $request = $client->get()->send();
         $response = $request->getBody();
     } catch (BadResponseException $e) {
         // @codeCoverageIgnoreStart
         $raw_response = explode("\n", $e->getResponse());
         d($raw_response);
         die;
     }
     return json_decode($response);
 }
Example #3
0
 /**
  * @param string
  */
 public function setBaseUrl($baseUrl)
 {
     $this->client->setBaseUrl($baseUrl);
 }