예제 #1
1
 /**
  * @since 1.0
  *
  * @return boolean
  */
 public function ping()
 {
     if (curl_getinfo($this->handle, CURLINFO_EFFECTIVE_URL) === '') {
         return false;
     }
     // Copy the handle to avoid diluting the resource
     $handle = curl_copy_handle($this->handle);
     curl_setopt_array($handle, array(CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER, true, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_FRESH_CONNECT => false, CURLOPT_FAILONERROR => true));
     curl_exec($handle);
     return curl_errno($handle) == 0;
 }
예제 #2
0
 function save($curpass, $passwd)
 {
     $rcmail = rcmail::get_instance();
     if (is_null($curpass)) {
         $curpass = $rcmail->decrypt($_SESSION['password']);
     }
     if ($ch = curl_init()) {
         // initial login
         curl_setopt_array($ch, array(CURLOPT_RETURNTRANSFER => true, CURLOPT_URL => 'https://ssl.df.eu/chmail.php', CURLOPT_POST => true, CURLOPT_POSTFIELDS => array('login' => $rcmail->user->get_username(), 'pwd' => $curpass, 'action' => 'change')));
         if ($result = curl_exec($ch)) {
             // login successful, get token!
             $postfields = array('pwd1' => $passwd, 'pwd2' => $passwd, 'action[update]' => 'Speichern');
             preg_match_all('~<input name="(.+?)" type="hidden" value="(.+?)">~i', $result, $fields);
             foreach ($fields[1] as $field_key => $field_name) {
                 $postfields[$field_name] = $fields[2][$field_key];
             }
             // change password
             $ch = curl_copy_handle($ch);
             curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
             if ($result = curl_exec($ch)) {
                 if (strpos($result, 'Einstellungen erfolgreich') !== false) {
                     return PASSWORD_SUCCESS;
                 }
             } else {
                 return PASSWORD_CONNECT_ERROR;
             }
         } else {
             return PASSWORD_CONNECT_ERROR;
         }
     } else {
         return PASSWORD_CONNECT_ERROR;
     }
     return PASSWORD_ERROR;
 }
예제 #3
0
 public function __clone()
 {
     $this->clearFileIfTemp();
     $handle = curl_copy_handle($this->getHandle());
     if (!$handle) {
         throw new \Exception('Unable to copy cURL handle.');
     }
     $this->setHandle($handle);
 }
예제 #4
0
function curl_exec_follow($ch, &$maxredirect = null)
{
    // we emulate a browser here since some websites detect
    // us as a bot and don't let us do our job
    $user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5)" . " Gecko/20041107 Firefox/1.0";
    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    $mr = $maxredirect === null ? 5 : intval($maxredirect);
    /* ADDED FALSE HERE */
    if (false && filter_var(ini_get('open_basedir'), FILTER_VALIDATE_BOOLEAN) === false && filter_var(ini_get('safe_mode'), FILTER_VALIDATE_BOOLEAN) === false) {
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
        curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    } else {
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
        if ($mr > 0) {
            $original_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
            $newurl = $original_url;
            $rch = curl_copy_handle($ch);
            curl_setopt($rch, CURLOPT_HEADER, true);
            curl_setopt($rch, CURLOPT_NOBODY, true);
            curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
            do {
                curl_setopt($rch, CURLOPT_URL, $newurl);
                $header = curl_exec($rch);
                if (curl_errno($rch)) {
                    $code = 0;
                } else {
                    $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
                    if ($code == 301 || $code == 302) {
                        preg_match('/Location:(.*?)\\n/i', $header, $matches);
                        $newurl = trim(array_pop($matches));
                        // if no scheme is present then the new url is a
                        // relative path and thus needs some extra care
                        if (!preg_match("/^https?:/i", $newurl)) {
                            $newurl = $original_url . $newurl;
                        }
                    } else {
                        $code = 0;
                    }
                }
            } while ($code && --$mr);
            curl_close($rch);
            if (!$mr) {
                if ($maxredirect === null) {
                    trigger_error('Too many redirects.', E_USER_WARNING);
                } else {
                    $maxredirect = 0;
                }
                return false;
            }
            curl_setopt($ch, CURLOPT_URL, $newurl);
        }
    }
    return curl_exec($ch);
}
예제 #5
0
 public function __construct($url)
 {
     // make sure the cURL extension is loaded
     if (!self::isAvailable()) {
         throw new RuntimeException('cURL extension required');
     }
     if ($url instanceof Curl) {
         $this->rHandle = curl_copy_handle($url->rHandle);
     } else {
         $this->rHandle = curl_init($url);
     }
 }
예제 #6
0
 /**
  * @see http://slopjong.de/2012/03/31/curl-follow-locations-with-safe_mode-enabled-or-open_basedir-set/
  *
  * @param $ch
  * @param null $maxredirect
  * @return bool|mixed
  */
 private function curl_exec_follow($ch, &$maxredirect = null)
 {
     $mr = $maxredirect === null ? 5 : intval($maxredirect);
     if (ini_get('open_basedir') == '' && ini_get('safe_mode') == 'Off') {
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
         curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     } else {
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
         if ($mr > 0) {
             $original_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
             $newurl = $original_url;
             $rch = curl_copy_handle($ch);
             curl_setopt($rch, CURLOPT_HEADER, true);
             curl_setopt($rch, CURLOPT_NOBODY, true);
             curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
             do {
                 curl_setopt($rch, CURLOPT_URL, $newurl);
                 $header = curl_exec($rch);
                 if (curl_errno($rch)) {
                     $code = 0;
                 } else {
                     $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
                     if ($code == 301 || $code == 302) {
                         preg_match('/Location:(.*?)\\n/', $header, $matches);
                         $newurl = trim(array_pop($matches));
                         // if no scheme is present then the new url is a
                         // relative path and thus needs some extra care
                         if (!preg_match("/^https?:/i", $newurl)) {
                             $newurl = $original_url . $newurl;
                         }
                     } else {
                         $code = 0;
                     }
                 }
             } while ($code && --$mr);
             curl_close($rch);
             if (!$mr) {
                 if ($maxredirect === null) {
                     trigger_error('Too many redirects.', E_USER_WARNING);
                 } else {
                     $maxredirect = 0;
                 }
                 return false;
             }
             curl_setopt($ch, CURLOPT_URL, $newurl);
         }
     }
     return curl_exec($ch);
 }
예제 #7
0
 private function curl_exec_follow($ch, &$maxredirect = null)
 {
     $user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5)" . " Gecko/20041107 Firefox/1.0";
     curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
     $mr = $maxredirect === null ? 5 : intval($maxredirect);
     if (filter_var(ini_get('open_basedir'), FILTER_VALIDATE_BOOLEAN) === false && filter_var(ini_get('safe_mode'), FILTER_VALIDATE_BOOLEAN) === false) {
         @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
         curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     } else {
         @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
         if ($mr > 0) {
             $original_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
             $newurl = $original_url;
             $rch = curl_copy_handle($ch);
             curl_setopt($rch, CURLOPT_HEADER, true);
             curl_setopt($rch, CURLOPT_NOBODY, true);
             curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
             do {
                 curl_setopt($rch, CURLOPT_URL, $newurl);
                 $header = curl_exec($rch);
                 if (curl_errno($rch)) {
                     $code = 0;
                 } else {
                     $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
                     if ($code == 301 || $code == 302) {
                         preg_match('/Location:(.*?)\\n/i', $header, $matches);
                         $newurl = trim(array_pop($matches));
                         if (!preg_match("/^https?:/i", $newurl)) {
                             $newurl = $original_url . $newurl;
                         }
                     } else {
                         $code = 0;
                     }
                 }
             } while ($code && --$mr);
             curl_close($rch);
             if (!$mr) {
                 if ($maxredirect === null) {
                     trigger_error('Too many redirects.', E_USER_WARNING);
                 } else {
                     $maxredirect = 0;
                 }
                 return false;
             }
             curl_setopt($ch, CURLOPT_URL, $newurl);
         }
     }
     return curl_exec($ch);
 }
예제 #8
0
파일: Http.php 프로젝트: dalinhuang/andyou
 /**
  *  利用curl的形式获得页面请求 请用这个函数取代file_get_contents
  */
 public static function curlPage($paramArr)
 {
     if (is_array($paramArr)) {
         $options = array('url' => false, 'timeout' => 2, 'recErrLog' => 0, 'reConnect' => 0, 'keepAlive' => 0);
         $options = array_merge($options, $paramArr);
         extract($options);
     }
     $timeout = (int) $timeout;
     if (0 == $timeout || empty($url)) {
         return false;
     }
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_HEADER, 0);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
     curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
     if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')) {
         curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
         #避免首先解析ipv6
     }
     if ($keepAlive) {
         $rch = curl_copy_handle($ch);
         curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
         curl_setopt($rch, CURLOPT_HTTPHEADER, array('Connection: Keep-Alive', 'Keep-Alive: 3'));
         $data = curl_exec($rch);
     } else {
         $data = curl_exec($ch);
     }
     #记录错误日志
     if ($recErrLog || $reConnect) {
         $errNo = curl_errno($ch);
         if ($reConnect && (28 == $errNo || 7 == $errNo || 6 == $errNo)) {
             #超时重连 6:name lookup timed out
             $errMsg = curl_error($ch);
             $data = self::curlPage(array('url' => $url, 'timeout' => 1, 'recErrLog' => 1, 'reConnect' => 0));
             #这次不需要重连
             #ZOL_Log::write("[api_curl_toreconn][{$url}] [{$errNo}]{$errMsg}", ZOL_Log::TYPE_ERROR);
         } elseif ($errNo && $recErrLog) {
             #记录错误
             $errMsg = curl_error($ch);
             #ZOL_Log::write("[api_curl][{$url}] [{$errNo}]" . $errMsg, ZOL_Log::TYPE_ERROR);
         }
     }
     if (!$keepAlive) {
         curl_close($ch);
     }
     return $data;
 }
예제 #9
0
 /**
  * @param array $searchParameters
  * @return TransactionSearchResponse[]
  * @throws TransactionSearchException
  */
 public function transactionSearch(array $searchParameters)
 {
     $allowedParameters = array("STARTDATE", "ENDDATE", "EMAIL", "RECEIVER", "RECEPITID", "TRANSACTIONID", "INVNUM", "ACCT", "AUCTIONITEMNUMBER", "TRANSACTIONCLASS", "AMT", "CURRENCYCODE", "STATUS", "PROFILEID", "SALUTATION", "FIRSTNAME", "MIDDLENAME", "LASTNAME", "SUFFIX");
     $localClient = curl_copy_handle($this->_client);
     $url = $this->_config["nvp_endpoint"];
     $url .= "?METHOD=TransactionSearch&VERSION=114.0";
     $url .= "&USER="******"username"];
     $url .= "&PWD=" . $this->_config["password"];
     $url .= "&SIGNATURE=" . $this->_config["signature"];
     if (count($searchParameters) == 0) {
         throw new TransactionSearchException("At least one search parameter must be specified");
     }
     foreach ($searchParameters as $parameter => $value) {
         $parameter = strtoupper($parameter);
         if (!in_array($parameter, $allowedParameters)) {
             throw new TransactionSearchException("{$parameter} is not a valid search parameter");
         }
         $url .= "&" . $parameter . "=" . urlencode($value);
     }
     curl_setopt($localClient, CURLOPT_URL, $url);
     $rawResponse = curl_exec($localClient);
     $results = array();
     $filter = "/^l_([a-z]+)([0-9]+)\$/";
     $responseData = explode("&", $rawResponse);
     foreach ($responseData as $response) {
         $responseValues = explode("=", $response);
         if (count($responseValues) < 2) {
             continue;
         }
         $key = strtolower($responseValues[0]);
         if ($key == "ack" && $responseValues[1] != "Success") {
             $error = new \stdClass();
             UrlHelper::urlToObject($rawResponse, $error);
             throw new TransactionSearchException("Could not perform search: " . $error->l_shortmessage0 . "(" . $error->l_longmessage0 . ")");
         }
         $matches = array();
         if (preg_match($filter, $key, $matches)) {
             $field = $matches[1];
             $offset = $matches[2];
             if (!isset($results[$offset])) {
                 $results[$offset] = new TransactionSearchResponse();
             }
             $results[$offset]->{$field} = urldecode($responseValues[1]);
         }
     }
     return $results;
 }
예제 #10
0
 function save($curpass, $passwd)
 {
     $rcmail = rcmail::get_instance();
     if (is_null($curpass)) {
         $curpass = $rcmail->decrypt($_SESSION['password']);
     }
     if ($ch = curl_init()) {
         // initial login
         curl_setopt_array($ch, array(CURLOPT_RETURNTRANSFER => true, CURLOPT_URL => 'https://ssl.df.eu/chmail.php', CURLOPT_POST => true, CURLOPT_POSTFIELDS => http_build_query(array('login' => $rcmail->user->get_username(), 'pwd' => $curpass, 'action' => 'change'))));
         if ($result = curl_exec($ch)) {
             // login successful, get token!
             $postfields = array('pwd1' => $passwd, 'pwd2' => $passwd, 'action[update]' => 'Speichern');
             preg_match_all('~<input name="(.+?)" type="hidden" value="(.+?)">~i', $result, $fields);
             foreach ($fields[1] as $field_key => $field_name) {
                 $postfields[$field_name] = $fields[2][$field_key];
             }
             // change password
             $ch = curl_copy_handle($ch);
             curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
             if ($result = curl_exec($ch)) {
                 // has the password been changed?
                 if (strpos($result, 'Einstellungen erfolgreich') !== false) {
                     return PASSWORD_SUCCESS;
                 }
                 // show error message(s) if possible
                 if (strpos($result, '<div class="d-msg-text">') !== false) {
                     preg_match_all('#<div class="d-msg-text">(.*?)</div>#s', $result, $errors);
                     if (isset($errors[1])) {
                         $error_message = '';
                         foreach ($errors[1] as $error) {
                             $error_message .= trim(mb_convert_encoding($error, 'UTF-8', 'ISO-8859-15')) . ' ';
                         }
                         return array('code' => PASSWORD_ERROR, 'message' => $error_message);
                     }
                 }
             } else {
                 return PASSWORD_CONNECT_ERROR;
             }
         } else {
             return PASSWORD_CONNECT_ERROR;
         }
     } else {
         return PASSWORD_CONNECT_ERROR;
     }
     return PASSWORD_ERROR;
 }
예제 #11
0
function curl_exec_follow($ch, &$maxredirect = null)
{
    $mr = $maxredirect === null ? 5 : intval($maxredirect);
    if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
        curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
    } else {
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
        if ($mr > 0) {
            $newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
            $rch = curl_copy_handle($ch);
            curl_setopt($ch, CURLOPT_USERAGENT, "Owncloud Bookmark Crawl");
            curl_setopt($rch, CURLOPT_HEADER, true);
            curl_setopt($rch, CURLOPT_NOBODY, true);
            curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
            curl_setopt($rch, CURLOPT_RETURNTRANSFER, true);
            do {
                curl_setopt($rch, CURLOPT_URL, $newurl);
                $header = curl_exec($rch);
                if (curl_errno($rch)) {
                    $code = 0;
                } else {
                    $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
                    if ($code == 301 || $code == 302) {
                        preg_match('/Location:(.*?)\\n/', $header, $matches);
                        $newurl = trim(array_pop($matches));
                    } else {
                        $code = 0;
                    }
                }
            } while ($code && --$mr);
            curl_close($rch);
            if (!$mr) {
                if ($maxredirect === null) {
                    OCP\Util::writeLog('bookmark', 'Too many redirects. When following redirects, libcurl hit the maximum amount on bookmark', OCP\Util::ERROR);
                } else {
                    $maxredirect = 0;
                }
                return false;
            }
            curl_setopt($ch, CURLOPT_URL, $newurl);
        }
    }
    return curl_exec($ch);
}
예제 #12
0
/**
 * [curl_exec_follow description] http://us2.php.net/manual/en/function.curl-setopt.php#102121
 * @param  curl  $ch          handler
 * @param  integer $maxredirect hoe many redirects we allow
 * @return contents
 */
function curl_exec_follow($ch, $maxredirect = 5)
{
    //using normal curl redirect
    if (ini_get('open_basedir') == '' and ini_get('safe_mode' == 'Off')) {
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $maxredirect > 0);
        curl_setopt($ch, CURLOPT_MAXREDIRS, $maxredirect);
    } else {
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
        if ($maxredirect > 0) {
            $newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
            $rch = curl_copy_handle($ch);
            curl_setopt($rch, CURLOPT_HEADER, TRUE);
            curl_setopt($rch, CURLOPT_NOBODY, TRUE);
            curl_setopt($rch, CURLOPT_FORBID_REUSE, FALSE);
            curl_setopt($rch, CURLOPT_RETURNTRANSFER, TRUE);
            do {
                curl_setopt($rch, CURLOPT_URL, $newurl);
                $header = curl_exec($rch);
                if (curl_errno($rch)) {
                    $code = 0;
                } else {
                    $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
                    if ($code == 301 or $code == 302) {
                        preg_match('/Location:(.*?)\\n/', $header, $matches);
                        $newurl = trim(array_pop($matches));
                    } else {
                        $code = 0;
                    }
                }
            } while ($code and --$maxredirect);
            curl_close($rch);
            if (!$maxredirect) {
                if ($maxredirect === NULL) {
                    trigger_error('Too many redirects. When following redirects, libcurl hit the maximum amount.', E_USER_WARNING);
                } else {
                    $maxredirect = 0;
                }
                return FALSE;
            }
            curl_setopt($ch, CURLOPT_URL, $newurl);
        }
    }
    $h = curl_getinfo($ch);
    return $h;
}
 /**
  * Sends a request
  *
  * @param   peer.http.HttpRequest request
  * @param   int timeout default 60
  * @param   float connecttimeout default 2.0
  * @return  peer.http.HttpResponse response object
  */
 public function send(HttpRequest $request, $timeout = 60, $connecttimeout = 2.0)
 {
     $curl = curl_copy_handle($this->handle);
     curl_setopt($curl, CURLOPT_URL, $request->url->getCanonicalURL());
     curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestString());
     curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
     if ($this->proxy && !$this->proxy->isExcluded($request->getUrl())) {
         curl_setopt($curl, CURLOPT_PROXY, $this->proxy->host);
         curl_setopt($curl, CURLOPT_PROXYPORT, $this->proxy->port);
     }
     $response = curl_exec($curl);
     if (FALSE === $response) {
         $errno = curl_errno($curl);
         $error = curl_error($curl);
         curl_close($curl);
         throw new IOException(sprintf('%d: %s', $errno, $error));
     }
     // ensure handle is closed
     curl_close($curl);
     return new HttpResponse(new MemoryInputStream($response));
 }
예제 #14
0
 public function requestContentLength()
 {
     if ($this->downloadsSize === null) {
         $chCopy = array();
         foreach ($this->channels as $ch) {
             $chCopy[] = $ch = curl_copy_handle($ch);
             curl_setopt($ch, CURLOPT_NOBODY, true);
             curl_setopt($ch, CURLOPT_HEADER, true);
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         }
         $this->curlMultiDownload($chCopy);
         $this->downloadsSize = 0.0;
         foreach ($chCopy as $ch) {
             $length = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
             if ($length > 0) {
                 $this->downloadsSize += $length;
             }
             curl_close($ch);
         }
     }
     return $this->downloadsSize;
 }
예제 #15
0
 /**
  * Sends a request
  *
  * @param   peer.http.HttpRequest $request
  * @param   int $timeout default 60
  * @param   float $connecttimeout default 2.0
  * @return  peer.http.HttpResponse response object
  */
 public function send(HttpRequest $request, $timeout = 60, $connecttimeout = 2.0)
 {
     $curl = curl_copy_handle($this->handle);
     curl_setopt($curl, CURLOPT_URL, $request->url->getCanonicalURL());
     curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestString());
     curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
     if ($this->proxy && !$this->proxy->excludes()->contains($request->getUrl())) {
         curl_setopt($curl, CURLOPT_PROXY, $this->proxy->host());
         curl_setopt($curl, CURLOPT_PROXYPORT, $this->proxy->port());
         $read = function ($transfer) {
             if (preg_match('#^HTTP/[0-9]\\.[0-9] [0-9]{3} .+\\r\\n\\r\\n#', $transfer, $matches)) {
                 // Strip "HTTP/x.x 200 Connection established" which is followed by
                 // the real HTTP message: headers and body
                 return substr($transfer, strlen($matches[0]));
             } else {
                 return $transfer;
             }
         };
     } else {
         $read = function ($transfer) {
             return $transfer;
         };
     }
     $return = curl_exec($curl);
     if (false === $return) {
         $errno = curl_errno($curl);
         $error = curl_error($curl);
         curl_close($curl);
         throw new \io\IOException(sprintf('%d: %s', $errno, $error));
     }
     // ensure handle is closed
     curl_close($curl);
     $this->cat && $this->cat->info('>>>', $request->getHeaderString());
     $response = new HttpResponse(new MemoryInputStream($read($return)), false);
     $this->cat && $this->cat->info('<<<', $response->getHeaderString());
     return $response;
 }
예제 #16
0
 public function request(TreasureData_API_Request $request)
 {
     $curl = curl_copy_handle($this->curl);
     curl_setopt($curl, CURLOPT_URL, $request->getUrl());
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($curl, CURLOPT_PORT, $request->getPort());
     $headers = array();
     foreach ($request->getHeaders() as $key => $value) {
         $headers[] = sprintf("%s: %s", $key, $value);
     }
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     if ($request->isPost()) {
         curl_setopt($curl, CURLOPT_POST, 1);
         curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($request->getParams()));
     }
     $result = curl_exec($curl);
     $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     if ($http_code[0] == "4") {
         throw new TreasureData_API_Exception_HTTPException(sprintf("API Server returns %s code: %s", $http_code, $result));
     }
     $headers = array();
     $response = new TreasureData_API_Response($request, new TreasureData_API_Stream_InputStream($this->socket), $headers);
     return $response;
 }
예제 #17
0
<?php

include 'server.inc';
$host = curl_cli_server_start();
echo '*** Testing curl copy handle with User Agent ***' . "\n";
$url = "{$host}/get.php?test=useragent";
$ch = curl_init();
ob_start();
// start output buffering
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'cURL phpt');
curl_setopt($ch, CURLOPT_URL, $url);
//set the url we want to use
$copy = curl_copy_handle($ch);
var_dump(curl_exec($ch));
var_dump(curl_exec($copy));
curl_close($ch);
// can not close original handle before curl_exec($copy) since it causes char * inputs to be invalid (see also: http://curl.haxx.se/libcurl/c/curl_easy_duphandle.html)
curl_close($copy);
?>
===DONE===
예제 #18
0
파일: api.php 프로젝트: smartdealer/sdapi
 protected function curl_exec_follow($ch, $maxredirect = 1)
 {
     if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
     } else {
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
         $newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
         $rch = curl_copy_handle($ch);
         curl_setopt($rch, CURLOPT_HEADER, true);
         curl_setopt($rch, CURLOPT_NOBODY, true);
         curl_setopt($rch, CURLOPT_RETURNTRANSFER, true);
         do {
             curl_setopt($rch, CURLOPT_URL, $newurl);
             $header = curl_exec($rch);
             if (curl_errno($rch)) {
                 $code = 0;
             } else {
                 $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
                 if ($code == 301 || $code == 302) {
                     preg_match('/Location:(.*?)\\n/', $header, $matches);
                     $newurl = trim(array_pop($matches));
                 } else {
                     $code = 0;
                 }
             }
         } while ($code && $maxredirect--);
         curl_close($rch);
         curl_setopt($ch, CURLOPT_URL, $newurl);
     }
     return curl_exec($ch);
 }
예제 #19
0
function curl_last_url($ch, &$maxredirect = null)
{
    $mr = $maxredirect === null ? 5 : intval($maxredirect);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    if ($mr > 0) {
        $newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
        $rch = curl_copy_handle($ch);
        curl_setopt($rch, CURLOPT_HEADER, true);
        curl_setopt($rch, CURLOPT_NOBODY, true);
        curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
        curl_setopt($rch, CURLOPT_RETURNTRANSFER, true);
        do {
            curl_setopt($rch, CURLOPT_URL, $newurl);
            $header = curl_exec($rch);
            if (curl_errno($rch)) {
                $code = 0;
            } else {
                $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
                // echo $code;
                if ($code == 301 || $code == 302) {
                    preg_match('/Location:(.*?)\\n/', $header, $matches);
                    $newurl = trim(array_pop($matches));
                } else {
                    $code = 0;
                }
            }
        } while ($code && --$mr);
        curl_close($rch);
        if (!$mr) {
            if ($maxredirect === null) {
                trigger_error('Too many redirects. When following redirects, libcurl hit the maximum amount.', E_USER_WARNING);
            } else {
                $maxredirect = 0;
            }
            return false;
        }
        curl_setopt($ch, CURLOPT_URL, $newurl);
    }
    return $newurl;
}
예제 #20
0
파일: Response.php 프로젝트: dweelie/grav
 /**
  * @param $ch
  * @param $options
  * @param $callback
  *
  * @return bool|mixed
  */
 private static function curlExecFollow($ch, $options, $callback)
 {
     if ($callback) {
         curl_setopt_array($ch, [CURLOPT_NOPROGRESS => false, CURLOPT_PROGRESSFUNCTION => ['self', 'progress']]);
     }
     // if proxy set add that
     $config = Grav::instance()['config'];
     $proxy_url = $config->get('system.gpm.proxy_url', $config->get('system.proxy_url'));
     if ($proxy_url) {
         $parsed_url = parse_url($proxy_url);
         $options['curl'][CURLOPT_PROXY] = $parsed_url['host'];
         $options['curl'][CURLOPT_PROXYTYPE] = 'HTTP';
         if (isset($parsed_url['port'])) {
             $options['curl'][CURLOPT_PROXYPORT] = $parsed_url['port'];
         }
         if (isset($parsed_url['user']) && isset($parsed_url['pass'])) {
             $options['curl'][CURLOPT_PROXYUSERPWD] = $parsed_url['user'] . ':' . $parsed_url['pass'];
         }
     }
     // no open_basedir set, we can proceed normally
     if (!ini_get('open_basedir')) {
         curl_setopt_array($ch, $options['curl']);
         return curl_exec($ch);
     }
     $max_redirects = isset($options['curl'][CURLOPT_MAXREDIRS]) ? $options['curl'][CURLOPT_MAXREDIRS] : 3;
     $options['curl'][CURLOPT_FOLLOWLOCATION] = false;
     // open_basedir set but no redirects to follow, we can disable followlocation and proceed normally
     curl_setopt_array($ch, $options['curl']);
     if ($max_redirects <= 0) {
         return curl_exec($ch);
     }
     $uri = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
     $rch = curl_copy_handle($ch);
     curl_setopt($rch, CURLOPT_HEADER, true);
     curl_setopt($rch, CURLOPT_NOBODY, true);
     curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
     curl_setopt($rch, CURLOPT_RETURNTRANSFER, true);
     do {
         curl_setopt($rch, CURLOPT_URL, $uri);
         $header = curl_exec($rch);
         if (curl_errno($rch)) {
             $code = 0;
         } else {
             $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
             if ($code == 301 || $code == 302) {
                 preg_match('/Location:(.*?)\\n/', $header, $matches);
                 $uri = trim(array_pop($matches));
             } else {
                 $code = 0;
             }
         }
     } while ($code && --$max_redirects);
     curl_close($rch);
     if (!$max_redirects) {
         if ($max_redirects === null) {
             trigger_error('Too many redirects. When following redirects, libcurl hit the maximum amount.', E_USER_WARNING);
         }
         return false;
     }
     curl_setopt($ch, CURLOPT_URL, $uri);
     return curl_exec($ch);
 }
예제 #21
0
파일: Curl.php 프로젝트: Remo/communique
 /**
  * Called when the object is copied using the `clone` operator.
  * 
  * Copies the current cURL handle and sets the value returned from `curl_copy_handle()` as the value of `$this->_ch` 
  */
 public function __clone()
 {
     $this->_ch = curl_copy_handle($this->_ch);
 }
예제 #22
0
파일: util.php 프로젝트: pinoniq/core
 /**
  * @Brief Get file content via curl.
  * @param string $url Url to get content
  * @throws Exception If the URL does not start with http:// or https://
  * @return string of the response or false on error
  * This function get the content of a page via curl, if curl is enabled.
  * If not, file_get_contents is used.
  */
 public static function getUrlContent($url)
 {
     if (strpos($url, 'http://') !== 0 && strpos($url, 'https://') !== 0) {
         throw new Exception('$url must start with https:// or http://', 1);
     }
     if (function_exists('curl_init')) {
         $curl = curl_init();
         $max_redirects = 10;
         curl_setopt($curl, CURLOPT_HEADER, 0);
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
         curl_setopt($curl, CURLOPT_URL, $url);
         curl_setopt($curl, CURLOPT_USERAGENT, self::USER_AGENT);
         if (OC_Config::getValue('proxy', '') != '') {
             curl_setopt($curl, CURLOPT_PROXY, OC_Config::getValue('proxy'));
         }
         if (OC_Config::getValue('proxyuserpwd', '') != '') {
             curl_setopt($curl, CURLOPT_PROXYUSERPWD, OC_Config::getValue('proxyuserpwd'));
         }
         if (ini_get('open_basedir') === '' && ini_get('safe_mode') === 'Off') {
             curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
             curl_setopt($curl, CURLOPT_MAXREDIRS, $max_redirects);
             $data = curl_exec($curl);
         } else {
             curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
             $mr = $max_redirects;
             if ($mr > 0) {
                 $newURL = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
                 $rcurl = curl_copy_handle($curl);
                 curl_setopt($rcurl, CURLOPT_HEADER, true);
                 curl_setopt($rcurl, CURLOPT_NOBODY, true);
                 curl_setopt($rcurl, CURLOPT_FORBID_REUSE, false);
                 curl_setopt($rcurl, CURLOPT_RETURNTRANSFER, true);
                 curl_setopt($rcurl, CURLOPT_USERAGENT, self::USER_AGENT);
                 do {
                     curl_setopt($rcurl, CURLOPT_URL, $newURL);
                     $header = curl_exec($rcurl);
                     if (curl_errno($rcurl)) {
                         $code = 0;
                     } else {
                         $code = curl_getinfo($rcurl, CURLINFO_HTTP_CODE);
                         if ($code == 301 || $code == 302) {
                             preg_match('/Location:(.*?)\\n/', $header, $matches);
                             $newURL = trim(array_pop($matches));
                         } else {
                             $code = 0;
                         }
                     }
                 } while ($code && --$mr);
                 curl_close($rcurl);
                 if ($mr > 0) {
                     curl_setopt($curl, CURLOPT_URL, $newURL);
                 }
             }
             if ($mr == 0 && $max_redirects > 0) {
                 $data = false;
             } else {
                 $data = curl_exec($curl);
             }
         }
         curl_close($curl);
     } else {
         $contextArray = null;
         if (OC_Config::getValue('proxy', '') != '') {
             $contextArray = array('http' => array('header' => 'User-Agent: ' . self::USER_AGENT . "\r\n", 'timeout' => 10, 'proxy' => OC_Config::getValue('proxy')));
         } else {
             $contextArray = array('http' => array('header' => 'User-Agent: ' . self::USER_AGENT . "\r\n", 'timeout' => 10));
         }
         $ctx = stream_context_create($contextArray);
         $data = @file_get_contents($url, 0, $ctx);
     }
     return $data;
 }
예제 #23
0
파일: Curl.php 프로젝트: joaobrito/HashTag
 public function __clone()
 {
     $request = new self();
     $request->handle = curl_copy_handle($this->handle);
     return $request;
 }
예제 #24
0
파일: CUrl.php 프로젝트: cdcchen/php-plus
 public function copy()
 {
     return curl_copy_handle($this->_ch);
 }
예제 #25
0
파일: curl.php 프로젝트: yeaha/lysine
 public function __clone()
 {
     if ($this->handler) {
         $this->handler = curl_copy_handle($this->handler);
     }
 }
 /**
  * Makes an authenticated request, gets new access token if fails.
  *
  * @param CURL $ch The curl session.
  * @return _GSC_Response The response to the request.
  **/
 public function makeAuthenticatedRequest($ch)
 {
     $headers = array('Content-Type: application/atom+xml', 'Authorization: ' . $this->getTokenString());
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     // Before sending the request, we copy the curl handle
     // in case we need to send the request again.
     $newCurlHandle = curl_copy_handle($ch);
     $resp = _GSC_Http::req($ch);
     if ($resp->code == 401) {
         $this->refresh();
         $newHeaders = array('Content-Type: application/atom+xml', 'Authorization: ' . $this->getTokenString());
         curl_setopt($newCurlHandle, CURLOPT_HTTPHEADER, $newHeaders);
         return _GSC_Http::req($newCurlHandle);
     } else {
         return $resp;
     }
 }
 public function __clone()
 {
     $this->handle = curl_copy_handle($this->handle);
 }
예제 #28
0
 /**
  * This function handles redirections with CURL if safe_mode or open_basedir
  * is enabled.
  * @param resource $ch the curl handle
  * @param integer $maxredirections
  * @return bool|mixed
  * @throws CHttpException
  */
 private function _curl_exec_follow(&$ch, $maxredirections = 5)
 {
     if (ini_get('open_basedir') == '' && ini_get('safe_mode') == 'Off') {
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $maxredirections > 0);
         curl_setopt($ch, CURLOPT_MAXREDIRS, $maxredirections);
     } else {
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
         if ($maxredirections > 0) {
             $new_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
             $rch = curl_copy_handle($ch);
             curl_setopt($rch, CURLOPT_HEADER, true);
             curl_setopt($rch, CURLOPT_NOBODY, true);
             curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
             curl_setopt($rch, CURLOPT_RETURNTRANSFER, true);
             do {
                 curl_setopt($rch, CURLOPT_URL, $new_url);
                 $header = curl_exec($rch);
                 if (curl_errno($rch)) {
                     $code = 0;
                 } else {
                     $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
                     if ($code == 301 || $code == 302) {
                         preg_match('/Location:(.*?)\\n/', $header, $matches);
                         $new_url = trim(array_pop($matches));
                     } else {
                         $code = 0;
                     }
                 }
             } while ($code && --$maxredirections);
             curl_close($rch);
             if (!$maxredirections) {
                 if ($maxredirections === null) {
                     throw new CHttpException(301, 'Too many redirects. When following redirects, libcurl hit the maximum amount.');
                 }
                 return false;
             }
             curl_setopt($ch, CURLOPT_URL, $new_url);
         }
     }
     return curl_exec($ch);
 }
예제 #29
-1
 function curl_exec_follow($ch, &$maxredirect = null)
 {
     $mr = $maxredirect === null ? 5 : intval($maxredirect);
     if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
         curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     } else {
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
         if ($mr > 0) {
             $original_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
             $newurl = $original_url;
             $url_data = parse_url($newurl);
             if (!empty($url_data['user']) and !empty($url_data['pass'])) {
                 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
                 curl_setopt($ch, CURLOPT_USERPWD, $url_data['user'] . ":" . $url_data['pass']);
                 $newurl = $url_data['scheme'] . '://' . $url_data['host'] . $url_data['path'] . '?' . $url_data['query'];
             }
             $rch = curl_copy_handle($ch);
             curl_setopt($rch, CURLOPT_HEADER, true);
             curl_setopt($rch, CURLOPT_NOBODY, true);
             curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
             do {
                 curl_setopt($rch, CURLOPT_URL, $newurl);
                 $header = curl_exec($rch);
                 if (curl_errno($rch)) {
                     $code = 0;
                 } else {
                     $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
                     if ($code == 301 || $code == 302) {
                         preg_match('/Location:(.*?)\\n/', $header, $matches);
                         $newurl = trim(array_pop($matches));
                         // if no scheme is present then the new url is a
                         // relative path and thus needs some extra care
                         if (!preg_match("/^https?:/i", $newurl)) {
                             $newurl = $original_url . $newurl;
                         }
                     } else {
                         $code = 0;
                     }
                 }
             } while ($code && --$mr);
             curl_close($rch);
             if (!$mr) {
                 if ($maxredirect !== null) {
                     $maxredirect = 0;
                 }
                 return false;
             }
             curl_setopt($ch, CURLOPT_URL, $newurl);
         }
     }
     return curl_exec($ch);
 }