コード例 #1
0
ファイル: Session.php プロジェクト: callcolor/PHRETS
 /**
  * @param $capability
  * @param array $options
  * @throws Exceptions\CapabilityUnavailable
  * @throws Exceptions\RETSException
  * @return \GuzzleHttp\Psr7\Response
  */
 protected function request($capability, $options = [])
 {
     $url = $this->capabilities->get($capability);
     if (!$url) {
         throw new CapabilityUnavailable("'{$capability}' tried but no valid endpoint was found.  Did you forget to Login()?");
     }
     if (!array_key_exists('headers', $options)) {
         $options['headers'] = [];
     }
     if (!array_key_exists('auth', $options)) {
         $options['auth'] = [];
     }
     if (!array_key_exists('allow_redirects', $options) && @$this->default_options['allow_redirects']) {
         $options['allow_redirects'] = $this->default_options['allow_redirects'];
     }
     // Guzzle 5 changed the order that headers are added to the request, so we'll do this manually
     $options['headers'] = array_merge($this->default_options['headers'], $options['headers']);
     $options['auth'] = array_merge($this->default_options['auth'], $options['auth']);
     // user-agent authentication
     if ($this->configuration->getUserAgentPassword()) {
         $ua_digest = $this->configuration->userAgentDigestHash($this);
         $options['headers'] = array_merge($options['headers'], ['RETS-UA-Authorization' => 'Digest ' . $ua_digest]);
     }
     $options = array_merge($options, ['cookies' => $this->cookie_jar]);
     $this->debug("Sending HTTP Request for {$url} ({$capability})", $options);
     if (array_key_exists('query', $options)) {
         $this->last_request_url = $url . '?' . \http_build_query($options['query']);
     } else {
         $this->last_request_url = $url;
     }
     /** @var \GuzzleHttp\Psr7\Response $response */
     if ($this->configuration->readOption('use_post_method')) {
         $this->debug('Using POST method per use_post_method option');
         $query = array_key_exists('query', $options) ? $options['query'] : null;
         $response = $this->client->post($url, array_merge($options, ['body' => $query]));
     } else {
         $response = $this->client->get($url, $options);
     }
     $this->last_response = $response;
     $cookies = $response->getHeader('Set-Cookie');
     foreach ($cookies as $cookie) {
         if (preg_match('/RETS-Session-ID\\=(.*?)(\\;|\\s+|$)/', $cookie, $matches)) {
             $this->rets_session_id = $matches[1];
         }
     }
     if ($response->getHeader('Content-Type') == 'text/xml' and $capability != 'GetObject') {
         $xml = simplexml_load_string($response->getBody());
         if ($xml and isset($xml['ReplyCode'])) {
             $rc = (string) $xml['ReplyCode'];
             // 20201 - No records found - not exception worthy in my mind
             if ($rc != "0" and $rc != "20201") {
                 throw new RETSException($xml['ReplyText'], (int) $xml['ReplyCode']);
             }
         }
     }
     $this->debug('Response: HTTP ' . $response->getStatusCode());
     return $response;
 }
コード例 #2
0
 /** @test **/
 public function it_generates_user_agent_auth_hashes_correctly()
 {
     $c = new Configuration();
     $c->setLoginUrl('http://www.reso.org/login')->setUserAgent('PHRETS/2.0')->setUserAgentPassword('12345')->setRetsVersion('1.7.2');
     $s = new \PHRETS\Session($c);
     $this->assertSame('123c96e02e514da469db6bc61ab998dc', $c->userAgentDigestHash($s));
 }