public function testCanCreateStreamsUsingCustomFactory() { $stream = $this->getMockBuilder('Guzzle\\Stream\\StreamRequestFactoryInterface')->setMethods(array('fromRequest'))->getMockForAbstractClass(); $resource = new Stream(fopen('php://temp', 'r+')); $stream->expects($this->once())->method('fromRequest')->will($this->returnValue($resource)); $this->getServer()->enqueue(array("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ntest")); $result = StaticClient::get($this->getServer()->getUrl(), array('stream' => $stream)); $this->assertSame($resource, $result); }
public function getUserInformation() { $url = $this->config['api_url'] . '/profile'; $response = Guzzle::get($url, array('headers' => array('Accept' => 'application/json', 'Authorization' => 'Bearer ' . $this->access_token), 'exceptions' => false)); if ($response->isSuccessful()) { $userInfo = $response->json(); if (is_array($userInfo)) { return $userInfo; } else { Yii::app()->user->setFlash('danger', "There was a problem with the response from OAuth server, maybe try again in a moment? (" . __LINE__ . ")"); return false; } } else { Yii::app()->user->setFlash('danger', "There was a problem with the response from OAuth server, maybe try again in a moment? (" . __LINE__ . "). More Info: " . $response->getBody()); return false; } }
/** * Download the file to a local path. * * @param string $pathToFile, * @param string $name * @param headers $array * @return void */ public function download($pathToFile, $name = null, array $headers = array()) { $response = StaticClient::get($this->offsetGet('_downloadURL'), array('headers' => $headers, 'timeout' => $this->timeout, 'save_to' => $pathToFile)); file_put_contents($pathToFile, $response->getBody()->getStream()); }
public function apiInvalidToken() { if (!Yii::app()->user->isGuest && isset(Yii::app()->user->access_token)) { $url = Yii::app()->params['oauth']['api_url'] . '/profile'; $response = Guzzle::get($url, array('headers' => array('Accept' => 'application/json', 'Authorization' => 'Bearer invalidtoken'), 'exceptions' => false)); if ($response->isSuccessful()) { return $response->json(); } else { return json_decode($response->getBody(true), true); } } }
/** * Adds support for Twitter user photo resource paths. * * @param string $path * @return string|null */ public function getResourcePath($path) { // Are they requesting a Twitter user image? if (strncmp($path, 'twitteruserimages/', 18) === 0) { $parts = array_merge(array_filter(explode('/', $path))); if (count($parts) != 3) { return; } $userId = $parts[1]; $size = $parts[2]; $imageSizes = array('mini' => 24, 'normal' => 48, 'bigger' => 73); if (is_numeric($size) && ($sizeKey = array_search($size, $imageSizes)) !== false) { $size = $sizeKey; } $baseUserImagePath = craft()->path->getRuntimePath() . 'twitter/userimages/' . $userId . '/'; $sizedFolderPath = $baseUserImagePath . $size . '/'; // Have we already downloaded this user's image at this size? $contents = IOHelper::getFolderContents($sizedFolderPath, false); if ($contents) { return $contents[0]; } else { // Do we have the original image? if (!is_numeric($size)) { if ($size == 'original' || array_key_exists($size, $imageSizes)) { $sizeName = $size; } else { return; } } else { $sizeName = 'original'; foreach ($imageSizes as $sizeKey => $sizeSize) { if ($size <= $sizeSize) { $sizeName = $sizeKey; break; } } } $originalFolderPath = $baseUserImagePath . $sizeName . '/'; $contents = IOHelper::getFolderContents($originalFolderPath, false); if ($contents) { $originalPath = $contents[0]; } else { // OK, let's fetch it then $user = craft()->twitter->getUserById($userId); if (!$user || empty($user['profile_image_url'])) { return; } $url = $user['profile_image_url']; if ($sizeName != 'normal') { if ($sizeName == 'original') { $url = str_replace('_normal', '', $url); } else { $url = str_replace('_normal', '_' . $sizeName, $url); } } IOHelper::ensureFolderExists($originalFolderPath); $fileName = pathinfo($url, PATHINFO_BASENAME); $originalPath = $originalFolderPath . $fileName; $response = \Guzzle\Http\StaticClient::get($url, array('save_to' => $originalPath)); if (!$response->isSuccessful()) { return; } } // If they were actually requesting "mini", "normal", "bigger", or "original", we're done if (!is_numeric($size)) { return $originalPath; } // Resize it to the requested size $fileName = pathinfo($originalPath, PATHINFO_BASENAME); $sizedPath = $sizedFolderPath . $fileName; IOHelper::ensureFolderExists($sizedFolderPath); craft()->images->loadImage($originalPath)->scaleAndCrop($size, $size)->saveAs($sizedPath); return $sizedPath; } } }