Beispiel #1
0
 protected function setRequestOptions($url, $method, $vars, $files = null)
 {
     $purl = parse_url($url);
     if ($purl['scheme'] == 'https') {
         curl_setopt($this->request, CURLOPT_PORT, empty($purl['port']) ? 443 : $purl['port']);
         if ($this->validate_ssl) {
             curl_setopt($this->request, CURLOPT_SSL_VERIFYPEER, true);
         } elseif ($this->validate_ssl_host) {
             curl_setopt($this->request, CURLOPT_SSL_VERIFYPEER, false);
             curl_setopt($this->request, CURLOPT_SSL_VERIFYHOST, 0);
         } else {
             curl_setopt($this->request, CURLOPT_SSL_VERIFYPEER, false);
             curl_setopt($this->request, CURLOPT_SSL_VERIFYHOST, 2);
         }
     }
     $method = strtoupper($method);
     switch ($method) {
         case 'HEAD':
             curl_setopt($this->request, CURLOPT_NOBODY, true);
             break;
         case 'GET':
             curl_setopt($this->request, CURLOPT_HTTPGET, true);
             break;
         case 'PUT':
             curl_setopt($this->request, CURLOPT_CUSTOMREQUEST, "PUT");
             $toPost = true;
             break;
         case 'POST':
             curl_setopt($this->request, CURLOPT_POST, true);
             $toPost = true;
             break;
         default:
             curl_setopt($this->request, CURLOPT_CUSTOMREQUEST, $method);
     }
     curl_setopt($this->request, CURLOPT_URL, $url);
     if ($files || !empty($vars)) {
         if (!$toPost) {
             throw new \InvalidArgumentException('POST-vars may only be set for a POST or PUT Request.');
         }
         if (!is_array($vars)) {
             curl_setopt($this->request, CURLOPT_POSTFIELDS, $vars);
         } elseif ($files) {
             foreach ($files as &$file) {
                 if ($file[0] != '@') {
                     $file = '@' . $file;
                 }
             }
             unset($file);
             curl_setopt($this->request, CURLOPT_POSTFIELDS, Arrays::merge($files, $vars));
         } else {
             curl_setopt($this->request, CURLOPT_POSTFIELDS, Http::buildQuery($vars));
         }
     } elseif ($toPost) {
         throw new \InvalidArgumentException('POST-vars must be set for a POST-Request.');
     }
     # Set some default CURL options
     curl_setopt($this->request, CURLOPT_HEADER, true);
     curl_setopt($this->request, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($this->request, CURLOPT_USERAGENT, $this->user_agent);
     curl_setopt($this->request, CURLOPT_TIMEOUT, $this->timeout);
     if ($this->cookie_file) {
         curl_setopt($this->request, CURLOPT_COOKIEFILE, $this->cookie_file);
         curl_setopt($this->request, CURLOPT_COOKIEJAR, $this->cookie_file);
     }
     /* relative paths fix, see requestFix
     		if ($this->follow_redirects){
     			curl_setopt($this->request, CURLOPT_FOLLOWLOCATION, true);
     		}
     		*/
     if ($this->referrer) {
         curl_setopt($this->request, CURLOPT_REFERER, $this->referrer);
     }
     # Set any custom CURL options
     foreach ($this->options as $option => $value) {
         curl_setopt($this->request, $option, $value);
     }
 }