set_curlopts() 공개 메소드

Set additional CURLOPT settings. These will merge with the default settings, and override if there is a duplicate.
public set_curlopts ( array $curlopts )
$curlopts array (Optional) A set of key-value pairs that set `CURLOPT` options. These will merge with the existing CURLOPTs, and ones passed here will override the defaults. Keys should be the `CURLOPT_*` constants, not strings.
예제 #1
0
 /**
  * Fetches and caches EC2 instance profile credentials. This is meant to be used by the constructor, and is not to
  * be manually invoked.
  *
  * @param CacheCore $cache (Required) The a reference to the cache object that is being used to handle the caching.
  * @param array $options (Required) The options that were passed into the constructor.
  * @return mixed The data to be cached, or NULL.
  */
 public function cache_instance_profile_credentials($cache, $options)
 {
     $instance_profile_url = 'http://169.254.169.254/latest/meta-data/iam/security-credentials/';
     $connect_timeout = isset($options['instance_profile_timeout']) ? $options['instance_profile_timeout'] : 2;
     try {
         // Make a call to the EC2 Metadata Service to find the available instance profile
         $request = new RequestCore($instance_profile_url);
         $request->set_curlopts(array(CURLOPT_CONNECTTIMEOUT => $connect_timeout));
         $response = $request->send_request(true);
         if ($response->isOK()) {
             // Get the instance profile name
             $profile = (string) $response->body;
             // Make a call to the EC2 Metadata Service to get the instance profile credentials
             $request = new RequestCore($instance_profile_url . $profile);
             $request->set_curlopts(array(CURLOPT_CONNECTTIMEOUT => $connect_timeout));
             $response = $request->send_request(true);
             if ($response->isOK()) {
                 // Get the credentials
                 $credentials = json_decode($response->body, true);
                 if ($credentials['Code'] === 'Success') {
                     // Determine the expiration time
                     $expiration_time = strtotime((string) $credentials['Expiration']);
                     $expiration_duration = round(($expiration_time - time()) * 0.85);
                     $cache->expire_in($expiration_duration);
                     // Return the credential information
                     return array('key' => $credentials['AccessKeyId'], 'secret' => $credentials['SecretAccessKey'], 'token' => $credentials['Token'], 'expires' => $credentials['Expiration']);
                 }
             }
         }
     } catch (cURL_Exception $e) {
         // The EC2 Metadata Service does not exist or had timed out.
         // An exception will be thrown on the next line.
     }
     // @codeCoverageIgnoreStart
     throw new CFCredentials_Exception('No credentials were provided. The SDK attempted to retrieve Instance ' . 'Profile credentials from the EC2 Instance Metadata Service, but failed to do so. Instance profile ' . 'credentials are only accessible on EC2 instances configured with a specific IAM role.');
     // @codeCoverageIgnoreEnd
 }
예제 #2
0
 /**
  * _baseControl
  *   
  * 用户关注:否
  * 
  * 网络交互方法
  * 
  * @access protected
  * @param array $opt 参数数组
  * @throws ChannelException 如果出错,则抛出异常,错误号为self::CHANNEL_SDK_SYS
  * 
  * @version 1.0.0.0
  */
 protected function _baseControl($opt)
 {
     $content = '';
     $resource = 'channel';
     if (isset($opt[self::CHANNEL_ID]) && !is_null($opt[self::CHANNEL_ID]) && !in_array($opt[self::METHOD], $this->_method_channel_in_body)) {
         $resource = $opt[self::CHANNEL_ID];
         unset($opt[self::CHANNEL_ID]);
     }
     $host = $opt[self::HOST];
     unset($opt[self::HOST]);
     $url = $host . '/rest/2.0/' . self::PRODUCT . '/';
     $url .= $resource;
     $http_method = 'POST';
     $opt[self::SIGN] = $this->_genSign($http_method, $url, $opt);
     foreach ($opt as $k => $v) {
         $k = urlencode($k);
         $v = urlencode($v);
         $content .= $k . '=' . $v . '&';
     }
     $content = substr($content, 0, strlen($content) - 1);
     $request = new RequestCore($url);
     $headers['Content-Type'] = 'application/x-www-form-urlencoded';
     $headers['User-Agent'] = 'Baidu Channel Service Phpsdk Client';
     foreach ($headers as $headerKey => $headerValue) {
         $headerValue = str_replace(array("\r", "\n"), '', $headerValue);
         if ($headerValue !== '') {
             $request->add_header($headerKey, $headerValue);
         }
     }
     $request->set_method($http_method);
     $request->set_body($content);
     if (is_array($this->_curlOpts)) {
         $request->set_curlopts($this->_curlOpts);
     }
     $request->send_request();
     return new ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code());
 }
 /**
  * Fetches the activation code for a gateway using its public URL.
  *
  * @param string $gateway_url (Required) The public URL to a gateway.
  * @return string|boolean The activation key for the gateway, or false if it could not be determined.
  */
 public function acquire_activation_code($gateway_url)
 {
     // Send a request to the gateway's URL
     $request = new RequestCore($gateway_url);
     $request->ssl_verification = false;
     $request->set_curlopts(array(CURLOPT_FOLLOWLOCATION => false));
     $response = $request->send_request(true);
     // Parse the query string from the URL in the location header to get the activation key
     if (isset($response->header['location'])) {
         $url = $response->header['location'];
         $query = parse_url($url, PHP_URL_QUERY);
         parse_str($query, $params);
         if (isset($params['activationKey'])) {
             return $params['activationKey'];
         }
     }
     return false;
 }