elFinder::curlExec PHP Method

curlExec() public static method

Call curl_exec() with supported redirect on safe_mode or open_basedir
Author: Naoki Sawada
public static curlExec ( resource $curl, array $options = [], array $headers = [] ) : mixed
$curl resource
$options array
$headers array
return mixed
    public static function curlExec($curl, $options = array(), $headers = array())
    {
        if ($followLocation = !ini_get('safe_mode') && !ini_get('open_basedir')) {
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        }
        if ($options) {
            curl_setopt_array($curl, $options);
        }
        if ($headers) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }
        $result = curl_exec($curl);
        if (!$followLocation && ($redirect = curl_getinfo($curl, CURLINFO_REDIRECT_URL))) {
            if ($stream = self::getStreamByUrl(array('target' => $redirect, 'headers' => $headers))) {
                $result = stream_get_contents($stream);
            }
        }
        if ($result === false) {
            if (curl_errno($curl)) {
                throw new \Exception('curl_exec() failed: ' . curl_error($curl));
            } else {
                throw new \Exception('curl_exec(): empty response');
            }
        }
        curl_close($curl);
        return $result;
    }

Usage Example

 /**
  * Call curl_exec().
  *
  * @param resource    $curl
  * @param bool|string $decodeOrParent
  * @param array       $headers
  *
  * @throws \Exception
  *
  * @return mixed
  */
 protected function _bd_curlExec($curl, $decodeOrParent = true, $headers = array())
 {
     $headers = array_merge(array('Authorization: Bearer ' . $this->token->data->access_token), $headers);
     $result = elFinder::curlExec($curl, array(), $headers);
     if (!$decodeOrParent) {
         return $result;
     }
     $decoded = json_decode($result);
     if (!empty($decoded->error_code)) {
         $errmsg = $decoded->error_code;
         if (!empty($decoded->message)) {
             $errmsg .= ': ' . $decoded->message;
         }
         throw new \Exception($errmsg);
     }
     // make catch
     if ($decodeOrParent && $decodeOrParent !== true) {
         $raws = null;
         list(, $parentId) = $this->_bd_splitPath($decodeOrParent);
         if (isset($decoded->entries)) {
             $raws = $decoded->entries;
         } elseif (isset($decoded->id)) {
             $raws = array($decoded);
         }
         if ($raws) {
             foreach ($raws as $raw) {
                 if (isset($raw->id)) {
                     $stat = $this->_bd_parseRaw($raw);
                     $itemPath = $this->_joinPath($decodeOrParent, $raw->id);
                     $this->updateCache($itemPath, $stat);
                 }
             }
         }
     }
     return $decoded;
 }