addHeader() public method

public addHeader ( $name, $value, $overWrite = true )
Ejemplo n.º 1
0
 /**
  * Handle
  *
  * @param \PayPal\Core\PPHttpConfig $httpConfig
  * @param \PayPal\Core\PPRequest    $request
  * @param array                     $options
  *
  * @throws \PayPal\Exception\PPInvalidCredentialException
  * @throws \PayPal\Exception\PPMissingCredentialException
  */
 public function handle($httpConfig, $request, $options)
 {
     $credential = $this->apiContext->getCredential();
     $config = $this->apiContext->getConfig();
     if ($credential == null) {
         // Try picking credentials from the config file
         $credMgr = PPCredentialManager::getInstance($config);
         $credValues = $credMgr->getCredentialObject();
         if (!is_array($credValues)) {
             throw new PPMissingCredentialException("Empty or invalid credentials passed");
         }
         $credential = new OAuthTokenCredential($credValues['clientId'], $credValues['clientSecret']);
     }
     if ($credential == null || !$credential instanceof OAuthTokenCredential) {
         throw new PPInvalidCredentialException("Invalid credentials passed");
     }
     $httpConfig->setUrl(rtrim(trim($this->_getEndpoint($config)), '/') . (isset($options['path']) ? $options['path'] : ''));
     if (!array_key_exists("User-Agent", $httpConfig->getHeaders())) {
         $httpConfig->addHeader("User-Agent", PPUserAgent::getValue(self::$sdkName, self::$sdkVersion));
     }
     if (!is_null($credential) && $credential instanceof OAuthTokenCredential) {
         $httpConfig->addHeader('Authorization', "Bearer " . $credential->getAccessToken($config));
     }
     if ($httpConfig->getMethod() == 'POST' || $httpConfig->getMethod() == 'PUT') {
         $httpConfig->addHeader('PayPal-Request-Id', $this->apiContext->getRequestId());
     }
 }
Ejemplo n.º 2
0
 /**
  * @test
  */
 public function testHeaderFunctions()
 {
     $o = new PPHttpConfig();
     $o->addHeader('key1', 'value1');
     $o->addHeader('key2', 'value');
     $o->addHeader('key2', 'overwritten');
     $this->assertEquals(2, count($o->getHeaders()));
     $this->assertEquals('overwritten', $o->getHeader('key2'));
     $this->assertNull($o->getHeader('key3'));
     $o = new PPHttpConfig();
     $o->addHeader('key1', 'value1');
     $o->addHeader('key2', 'value');
     $o->addHeader('key2', 'and more', false);
     $this->assertEquals(2, count($o->getHeaders()));
     $this->assertEquals('value;and more', $o->getHeader('key2'));
     $o->removeHeader('key2');
     $this->assertEquals(1, count($o->getHeaders()));
     $this->assertNull($o->getHeader('key2'));
 }