Example #1
0
 /**
  * Curl HTTP POST Request
  *
  * @param string $url
  * @param array $params
  * @return array of result, error code and error
  */
 private function doCurl($url, $params = array(), $files = array())
 {
     $this->responseHeaders = array();
     $requestHeaders = $this->config->getRequestHeaders();
     $params = $this->jsonEncode($params);
     $this->log("curl: {$url}");
     $this->log("post: {$params}");
     if ($this->config->getFormat() == self::KALTURA_SERVICE_FORMAT_JSON) {
         $requestHeaders[] = 'Accept: application/json';
     }
     $cookies = array();
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_POST, 1);
     if (count($files) > 0) {
         $params = array('json' => $params);
         foreach ($files as $key => $file) {
             // The usage of the @filename API for file uploading is
             // deprecated since PHP 5.5. CURLFile must be used instead.
             if (PHP_VERSION_ID >= 50500) {
                 $params[$key] = new \CURLFile($file);
             } else {
                 $params[$key] = "@" . $file;
                 // let curl know its a file
             }
         }
         curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
     } else {
         $requestHeaders[] = 'Content-Type: application/json';
         curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
     }
     curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_USERAGENT, $this->config->getUserAgent());
     if (count($files) > 0) {
         curl_setopt($ch, CURLOPT_TIMEOUT, 0);
     } else {
         curl_setopt($ch, CURLOPT_TIMEOUT, $this->config->getCurlTimeout());
     }
     if ($this->config->getStartZendDebuggerSession() === true) {
         $zendDebuggerParams = $this->getZendDebuggerParams($url);
         $cookies = array_merge($cookies, $zendDebuggerParams);
     }
     if (count($cookies) > 0) {
         $cookiesStr = http_build_query($cookies, null, '; ');
         curl_setopt($ch, CURLOPT_COOKIE, $cookiesStr);
     }
     if ($this->config->getProxyHost()) {
         curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
         curl_setopt($ch, CURLOPT_PROXY, $this->config->getProxyHost());
         if ($this->config->getProxyPort()) {
             curl_setopt($ch, CURLOPT_PROXYPORT, $this->config->getProxyPort());
         }
         if ($this->config->getProxyUser()) {
             curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->config->getProxyUser() . ':' . $this->config->getProxyPassword());
         }
         if ($this->config->getProxyType() === 'SOCKS5') {
             curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
         }
     }
     // Set SSL verification
     if (!$this->config->getVerifySSL()) {
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
     }
     // Set custom headers
     curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders);
     // Save response headers
     curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'readHeader'));
     $result = curl_exec($ch);
     $curlErrorCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     $curlError = curl_error($ch);
     curl_close($ch);
     return array($result, $curlErrorCode, $curlError);
 }
 /**
  * Curl HTTP POST Request
  *
  * @param string $url
  * @param array $params
  * @return array of result, error code and error
  */
 private function doCurl($url, $params = array(), $files = array())
 {
     $this->responseHeaders = array();
     $cookies = array();
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_POST, 1);
     if (count($files) > 0) {
         foreach ($files as &$file) {
             $file = "@" . $file;
         }
         // let curl know its a file
         curl_setopt($ch, CURLOPT_POSTFIELDS, array_merge($params, $files));
     } else {
         $opt = http_build_query($params, null, "&");
         $this->log("curl: {$url}&{$opt}");
         curl_setopt($ch, CURLOPT_POSTFIELDS, $opt);
     }
     curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_USERAGENT, $this->config->getUserAgent());
     if (count($files) > 0) {
         curl_setopt($ch, CURLOPT_TIMEOUT, 0);
     } else {
         curl_setopt($ch, CURLOPT_TIMEOUT, $this->config->getCurlTimeout());
     }
     if ($this->config->getStartZendDebuggerSession() === true) {
         $zendDebuggerParams = $this->getZendDebuggerParams($url);
         $cookies = array_merge($cookies, $zendDebuggerParams);
     }
     if (count($cookies) > 0) {
         $cookiesStr = http_build_query($cookies, null, '; ');
         curl_setopt($ch, CURLOPT_COOKIE, $cookiesStr);
     }
     if ($this->config->getProxyHost()) {
         curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
         curl_setopt($ch, CURLOPT_PROXY, $this->config->getProxyHost());
         if ($this->config->getProxyPort()) {
             curl_setopt($ch, CURLOPT_PROXYPORT, $this->config->getProxyPort());
         }
         if ($this->config->getProxyUser()) {
             curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->config->getProxyUser() . ':' . $this->config->getProxyPassword());
         }
         if ($this->config->getProxyType() === 'SOCKS5') {
             curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
         }
     }
     // Set SSL verification
     if (!$this->config->getVerifySSL()) {
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
     }
     // Set custom headers
     curl_setopt($ch, CURLOPT_HTTPHEADER, $this->config->getRequestHeaders());
     // Save response headers
     curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'readHeader'));
     $result = curl_exec($ch);
     $curlErrorCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     $curlError = curl_error($ch);
     curl_close($ch);
     return array($result, $curlErrorCode, $curlError);
 }