public function download($url) { $cookies = $this->getSession()->getCookie($this->sessionName); $cookie = SetCookie::fromString($cookies); $jar = new CookieJar(); $jar->setCookie($cookie); $client = new Client(['base_url' => $this->getSession()->getCurrentUrl()]); $this->response = $client->get($url, ['cookies' => $jar]); }
public function __construct(\GuzzleHttp\Client $guzzle, SerializerInterface $serializer, CookieJar $cookieJar) { $this->guzzle = $guzzle; // @TODO temporary solution, as authentication does not work ATM. $cookieJar->setCookie(SetCookie::fromString('SID="AuthKey 23ec5d03-86db-4d80-a378-6059139a7ead"; expires=Thu, 24 Nov 2016 13:52:20 GMT; path=/; domain=.sketchup.com')); $this->serializer = $serializer; }
/** * Create a new CookieAuth plugin. * * @param string $uri Login url * @param array|\Traversable $fields Array containing all fields that should * be send upon login, where the key is the field name and the value is the * field value * @param string $method Resquest method POST or GET * @param string|array|\Traversable|CookieJar $cookies */ public function __construct($uri, $fields, $method = 'POST', $cookies = null) { if ($cookies instanceof CookieJar) { $this->coockieJar = $cookies; } else { $this->coockieJar = new CookieJar(); if (!is_null($cookies)) { if (is_string($cookies)) { $this->coockieJar->setCookie(SetCookie::fromString($cookies)); $this->loginMade = true; } else { if (is_array($cookies) || $cookies instanceof \Traversable) { foreach ($cookies as $cookie) { if (is_string($cookie)) { $this->coockieJar->setCookie(SetCookie::fromString($cookie)); } else { if (is_array($cookie)) { $this->coockieJar->setCookie(new SetCookie($cookie)); } } } $this->loginMade = true; } } } } $this->config = ['uri' => $uri, 'method' => $method, 'fields' => $fields]; }
/** * Returns only the final results. To enable interim results, * use session-based requests or Websockets API. Endianness * of the incoming audio is autodetected. Audio files larger * than 4 MB are required to be sent in streaming mode * (chunked transfer-encoding). * * Streaming audio size limit is 100 MB. In streaming mode, * the connection is closed by the server if no data chunk * is received for more than 30 seconds and the last chunk * has not been sent yet (if all data has been sent, it can * take more than 30 seconds to generate the response and the * request will not time out). * * Use getUrlWithSession() to prevent the session from expiring. * The connection is also closed by the server if no speech is * detected for "inactivity_timeout" seconds of audio (not * processing time). This time can be set by "inactivity_timeout" * parameter; the default is 30 seconds. * * @param string $pathToFile * @param string $contentType * @param bool $uri * * @return $this */ public function recognize($pathToFile, $contentType = self::CONTENT_TYPE, $uri = false) { $uri = $uri ?: $this->getUrlWithSession('/recognize'); foreach ($this->getSetCookieHeader() as $cookie) { $cookie = SetCookie::fromString($cookie); $cookie->setDomain('stream.watsonplatform.net'); $this->cookieJar->setCookie($cookie); } $data[] = ['name' => 'audio_file', 'contents' => fopen($pathToFile, 'r')]; $this->recognize = $this->request($uri, 'POST', ['multipart' => $data, 'query' => ['continuous' => 'true', 'inactivity_timeout' => -1], 'cookies' => $this->cookieJar, 'headers' => ['content-type' => $contentType]]); return $this; }
protected function preConstructHealthChecker() { $cookieJar = new CookieJar(); foreach ($this->getCookies() as $cookieData) { $cookieJar->setCookie(new SetCookie($cookieData)); } $this->getHttpClient()->getEmitter()->attach(new HttpCookieSubscriber($cookieJar)); }
/** * Save cookies to the client's session. * * @param \GuzzleHttp\Cookie\SetCookie $cookie * * @return bool */ public function setCookie(SetCookie $cookie) { $successful = parent::setCookie($cookie); if ($successful) { $this->cookie->make($cookie->getName(), $cookie); return true; } return false; }
public function authenticate() { // go to google.com to get some cookies $request = $this->guzzleClient->createRequest('GET', 'http://www.google.com/ncr', ['cookies' => $this->cookieJar]); $response = $this->guzzleClient->send($request); // get google auth page html and fetch GALX $request = $this->guzzleClient->createRequest('GET', self::AUTH_URL, ['cookies' => $this->cookieJar]); $response = $this->guzzleClient->send($request); $content = $response->getBody()->getContents(); $document = new \DOMDocument(); @$document->loadHTML($content); $inputElements = $document->getElementsByTagName("input"); $params = []; foreach ($inputElements as $input) { $params[$input->getAttribute("name")] = $input->getAttribute("value"); } $params['Email'] = $this->email; $params['Passwd'] = $this->password; $params['pstMsg'] = '1'; $params['continue'] = 'http://www.google.com/trends'; // authenticate $request = $this->guzzleClient->createRequest('POST', self::AUTH_URL, ['cookies' => $this->cookieJar]); $query = $request->getQuery(); foreach ($params as $key => $param) { $query->set($key, $param); } $response = $this->guzzleClient->send($request); // verify sign in if needed if (strpos($response->getEffectiveUrl(), 'LoginVerification') !== false) { $content = $response->getBody()->getContents(); $document = new \DOMDocument(); @$document->loadHTML($content); $inputElements = $document->getElementsByTagName("input"); $params = []; foreach ($inputElements as $input) { $params[$input->getAttribute("name")] = $input->getAttribute("value"); } $params['challengetype'] = 'RecoveryEmailChallenge'; $params['emailAnswer'] = $this->recoveryEmail; $url = substr($response->getEffectiveUrl(), 0, strpos($response->getEffectiveUrl(), '?')); $request = $this->guzzleClient->createRequest('POST', $url, ['cookies' => $this->cookieJar]); $query = $request->getQuery(); foreach ($params as $key => $param) { $query->set($key, $param); } $response = $this->guzzleClient->send($request); } // update cookies $response = $this->guzzleClient->get('https://www.google.com/accounts/CheckCookie?chtml=LoginDoneHtml', ['cookies' => $this->cookieJar], ['headers' => ['Referrer' => 'https://www.google.com/accounts/ServiceLoginBoxAuth']]); // set language for trends $I4SUserLocale = new setCookie(['Name' => 'I4SUserLocale', 'Value' => $this->language, 'Domain' => 'www.google.com', 'Path' => '/trends', 'Secure' => true, 'HttpOnly' => true]); $this->cookieJar->setCookie($I4SUserLocale); return $this; }
/** * 将自定义的cookie追加进请求 * @param Api $api * @return CookieJar */ protected function getCookies(Api $api) { if ($cookies = $api->getCookies()) { $this->cookieContainer->setCookies($cookies); } //将cookie自身的容器交还给guzzle foreach ($this->cookieContainer->getCookies() as $cookie) { $this->cookies->setCookie(new SetCookie(['Name' => $cookie->getName(), 'Value' => $cookie->getValue(), 'Domain' => $cookie->getDomain() ?: $api->getUrl()->getHost(), 'Path' => $cookie->getPath(), 'Expires' => $cookie->getExpires()])); } return $this->cookies; }
public function setUp() { $this->loadHttpClientFixtures(array('HTTP/1.1 200 Ok', 'HTTP/1.1 200 Ok', 'HTTP/1.1 200 Ok', 'HTTP/1.1 200 Ok')); $webPage = new WebPage(); $webPage->setHttpResponse($this->getHttpFixtureFromHtmlDocument('example16', 'http://example.com')); $cookieJar = new CookieJar(); foreach ($this->getCookies() as $cookieData) { $cookieJar->setCookie(new SetCookie($cookieData)); } $this->getHttpClient()->getEmitter()->attach(new HttpCookieSubscriber($cookieJar)); $checker = $this->getDefaultChecker(); $checker->setWebPage($webPage); $checker->getAll(); }
public function setUp() { $this->setTestFixturePath(get_class($this)); $this->setHttpFixtures($this->getHttpFixtures($this->getFixturesDataPath() . '/HttpResponses')); $cookieJar = new CookieJar(); foreach ($this->getCookies() as $cookieData) { $cookieJar->setCookie(new SetCookie($cookieData)); } $this->getHttpClient()->getEmitter()->attach(new HttpCookieSubscriber($cookieJar)); $configuration = new Configuration(); $configuration->setUrlToValidate('http://example.com/'); $configuration->setHttpClient($this->getHttpClient()); $this->wrapper = $this->getNewCssValidatorWrapper(); $this->wrapper->setConfiguration($configuration); $this->wrapper->enableDeferToParentIfNoRawOutput(); $this->wrapper->setCssValidatorRawOutput($this->getFixture('no-messages.txt')); $this->wrapper->validate(); }
/** * @param array $config * * @return array */ protected function addCookiesToForwardDebugSession(array $config) { if (!Config::get(ZedRequestConstants::TRANSFER_DEBUG_SESSION_FORWARD_ENABLED)) { return $config; } if (!isset($_COOKIE[Config::get(ZedRequestConstants::TRANSFER_DEBUG_SESSION_NAME)])) { return $config; } $cookie = new SetCookie(); $cookie->setName(trim(Config::get(ZedRequestConstants::TRANSFER_DEBUG_SESSION_NAME))); $cookie->setValue($_COOKIE[Config::get(ZedRequestConstants::TRANSFER_DEBUG_SESSION_NAME)]); $cookie->setDomain(Config::get(ZedRequestConstants::HOST_ZED_API)); $cookieJar = new CookieJar(); $cookieJar->setCookie($cookie); $config['cookies'] = $cookieJar; return $config; }
public function testCanConvertToAndLoadFromArray() { $jar = new CookieJar(true); foreach ($this->getTestCookies() as $cookie) { $jar->setCookie($cookie); } $this->assertCount(3, $jar); $arr = $jar->toArray(); $this->assertCount(3, $arr); $newCookieJar = new CookieJar(false, $arr); $this->assertCount(3, $newCookieJar); $this->assertSame($jar->toArray(), $newCookieJar->toArray()); }
public function testDeletesCookiesByName() { $cookies = $this->getTestCookies(); $cookies[] = new SetCookie(['Name' => 'other', 'Value' => '123', 'Domain' => 'bar.com', 'Path' => '/boo', 'Expires' => time() + 1000]); $jar = new CookieJar(); foreach ($cookies as $cookie) { $jar->setCookie($cookie); } $this->assertCount(4, $jar); $jar->clear('bar.com', '/boo', 'other'); $this->assertCount(3, $jar); $names = array_map(function (SetCookie $c) { return $c->getName(); }, $jar->getIterator()->getArrayCopy()); $this->assertEquals(['foo', 'test', 'you'], $names); }