public function testCanCreateCustomStreamClass() { $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi"); $request = $this->client->get('/'); $stream = $this->factory->fromRequest($request, array(), array('stream_class' => 'Guzzle\\Http\\EntityBody')); $this->assertInstanceOf('Guzzle\\Http\\EntityBody', $stream); }
public function testBasicAuthAddsUserAndPassToUrl() { $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi"); $request = $this->client->get('/'); $request->setAuth('Foo', 'Bar'); $this->factory = $this->getMockBuilder('Guzzle\\Stream\\PhpStreamRequestFactory')->setMethods(array('createStream'))->getMock(); $this->factory->expects($this->once())->method('createStream')->will($this->returnValue(new Stream(fopen('php://temp', 'r')))); $this->factory->fromRequest($request); $this->assertContains('Foo:Bar@', (string) $this->readAttribute($this->factory, 'url')); }
public static function request($method, $url, $options = array()) { if (!self::$client) { self::$client = new Client(); } $request = self::$client->createRequest($method, $url, null, null, $options); if (isset($options['stream'])) { if ($options['stream'] instanceof StreamRequestFactoryInterface) { return $options['stream']->fromRequest($request); } elseif ($options['stream'] == true) { $streamFactory = new PhpStreamRequestFactory(); return $streamFactory->fromRequest($request); } } return $request->send(); }
/** * Get the streaming contents of the specified resource. * * @param string $uri * Resource URI * * @return EntityBody Returns the stream resource on success or false on failure * @throws \RuntimeException If the stream cannot be opened or an error occurs */ public function getStream($uri) { $request = $this->createRequest('GET', $uri); $factory = new PhpStreamRequestFactory(); $stream = $factory->fromRequest($request, array(), array('stream_class' => 'Guzzle\\Http\\EntityBody')); // The implementation of streaming download proposed by Guzzle's EntityBody class does not care about // HTTP errors. As a workaround, let's rebuild the HTTP response from the response headers sent in the // $http_response_header variable (http://www.php.net/manual/en/reserved.variables.httpresponseheader.php) $response = HttpResponse::fromMessage(implode("\r\n", $factory->getLastResponseHeaders())); // Creates History $this->lastRequest = $request; $this->lastResponse = $response; if (!$response->isSuccessful()) { $stream = false; } if (!$stream && $this->throwExceptions) { switch ($response->getStatusCode()) { case 404: throw new NoSuchResourceException('No such file or directory'); default: throw new \RuntimeException($response->getReasonPhrase(), $response->getStatusCode()); } } return $stream; }
/** * * @param string $serviceFileName * @param string $outputFileName */ public function getFile($serviceFileName, $outputFileName) { set_time_limit(0); $params = array('user' => $this->user, 'fileName' => $serviceFileName); $filepath = $_SERVER['DOCUMENT_ROOT'] . "/../tmp/" . $outputFileName; $filehandle = fopen($filepath, 'w'); $request = $this->client->post('getFile', array('Content-Type' => 'application/json'), json_encode($params))->setResponseBody($filepath); $factory = new PhpStreamRequestFactory(); $stream = $factory->fromRequest($request); while (!$stream->feof()) { $line = $stream->readLine(); fwrite($filehandle, $line); } fclose($filehandle); return $filepath; }
/** * Creates or Updates a loan to the LOS * @param LoanApplication $application * @throws \Exception */ public function upsertLoanToLos(LoanApplication $application) { try { $loId = null; $loanOfficer = $application->getLoanOfficer(); if (isset($loanOfficer)) { $loId = $loanOfficer->getLosId(); } $params = array('vendor' => $this->losConnection->getUsername(), 'cust' => $this->losConnection->getLicenseKey(), 'LO' => $loId, 'FNM' => $this->loanToFannieMae($application)); $request = $this->client->post($this::BASE_SERVICE_URL . '/process1003.ashx', array('Content-Type' => 'text/plain'), $params); $factory = new PhpStreamRequestFactory(); $stream = $factory->fromRequest($request); $data = ''; // Read until the stream is closed while (!$stream->feof()) { // Read a line from the stream $data .= $stream->readLine(); } if (!is_numeric($data)) { $params = array('vendor' => $this->losConnection->getUsername(), 'cust' => $this->losConnection->getLicenseKey(), 'LO' => '', 'FNM' => $this->loanToFannieMae($application)); $request = $this->client->post($this::BASE_SERVICE_URL . '/process1003.ashx', array('Content-Type' => 'text/plain'), $params); $stream = $factory->fromRequest($request); $data = ''; // Read until the stream is closed while (!$stream->feof()) { // Read a line from the stream $data .= $stream->readLine(); } } if (is_numeric($data)) { $application->setLosId($data); $modifiedDate = new \DateTime(); $modifiedDate->getTimestamp(); $application->setLosModified($modifiedDate); $application->setLosLoanNumber($data); $application->setStatus(1); // lookup lo if (isset($loId)) { $losLoanOfficer = $this->em->getRepository('SudouxMortgageBundle:LoanOfficer')->findOneBySiteAndLosId($application->getSite(), $loId); $application->setLoanOfficer($losLoanOfficer); } else { $application->setLoanOfficer(null); } $this->em->persist($application); $this->em->flush(); } else { $e = new \Exception("Error adding loan - ID: " . $application->getId() . " Exception: " . $data); $this->logger->crit($e->getMessage()); throw $e; } } catch (\Exception $e) { $this->logger->crit($e->getMessage()); throw $e; } return $data; }
/** * Process request * * @return mixed */ public function sendData($data) { if (true == $this->getParameter('testMode')) { $data['username'] = '******'; $data['password'] = '******'; } else { $data['username'] = $this->getUsername(); $data['password'] = $this->getPassword(); } // don't throw exceptions for 4xx errors $this->httpClient->getEventDispatcher()->addListener('request.error', function ($event) { if ($event['response']->isClientError()) { $event->stopPropagation(); } }); $httpRequest = $this->httpClient->createRequest($this->getHttpMethod(), $this->getEndpoint(), null, $data); // Might be useful to have some debug code here. Perhaps hook to whatever // logging engine is being used. // echo "Data == " . json_encode($data) . "\n"; //$httpResponse = $httpRequest->send(); $factory = new PhpStreamRequestFactory(); $stream = $factory->fromRequest($httpRequest); // Read until the stream is closed while (!$stream->feof()) { // Read a line from the stream $line = $stream->readLine(); // JSON decode the line of data } parse_str($line, $output); return $this->response = new Response($this, $output, $factory->getLastResponseHeaders()); }
/** * @param $url * @param array $params * @return \Guzzle\Stream\StreamInterface */ protected function createStream($url, array $params) { $request = $this->client->post($url, null, $params); $factory = new PhpStreamRequestFactory(); return $factory->fromRequest($request); }