Permalink
Browse files

Manually perform CURLOPT_FOLLOWLOCATION (bug 272)

  • Loading branch information...
1 parent 40ba947 commit dc13a1d3425f9f084e3feb5ea3ee3521d360dc49 @haraldpdl haraldpdl committed Apr 15, 2011
Showing with 39 additions and 14 deletions.
  1. +39 −14 osCommerce/OM/Core/HttpRequest/Curl.php
@@ -8,38 +8,63 @@
namespace osCommerce\OM\Core\HttpRequest;
+ use osCommerce\OM\Core\HttpRequest;
+
class Curl {
public static function execute($parameters) {
$curl = curl_init($parameters['server']['scheme'] . '://' . $parameters['server']['host'] . $parameters['server']['path'] . (isset($parameters['server']['query']) ? '?' . $parameters['server']['query'] : ''));
- curl_setopt($curl, CURLOPT_PORT, $parameters['server']['port']);
+ $curl_options = array(CURLOPT_PORT => $parameters['server']['port'],
+ CURLOPT_HEADER => true,
+ CURLOPT_SSL_VERIFYPEER => false,
+ CURLOPT_RETURNTRANSFER => true,
+ CURLOPT_FORBID_REUSE => true,
+ CURLOPT_FRESH_CONNECT => true,
+ CURLOPT_FOLLOWLOCATION => false);
if ( !empty($parameters['header']) ) {
- curl_setopt($curl, CURLOPT_HTTPHEADER, $parameters['header']);
+ $curl_options[CURLOPT_HTTPHEADER] = $parameters['header'];
}
if ( !empty($parameters['certificate']) ) {
- curl_setopt($curl, CURLOPT_SSLCERT, $parameters['certificate']);
+ $curl_options[CURLOPT_SSLCERT] = $parameters['certificate'];
}
- curl_setopt($curl, CURLOPT_HEADER, 0);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
- curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($curl, CURLOPT_MAXREDIRS, 5);
-
if ( $parameters['method'] == 'post' ) {
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters['parameters']);
+ $curl_options[CURLOPT_POST] = true;
+ $curl_options[CURLOPT_POSTFIELDS] = $parameters['parameters'];
}
+ curl_setopt_array($curl, $curl_options);
$result = curl_exec($curl);
+ $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
+
curl_close($curl);
- return $result;
+ list($headers, $body) = explode("\r\n\r\n", $result, 2);
+
+ if ( ($http_code == 301) || ($http_code == 302) ) {
+ if ( !isset($parameters['redir_counter']) || ($parameters['redir_counter'] < 6) ) {
+ if ( !isset($parameters['redir_counter']) ) {
+ $parameters['redir_counter'] = 0;
+ }
+
+ $matches = array();
+ preg_match('/(Location:|URI:)(.*?)\n/i', $headers, $matches);
+
+ $redir_url = trim(array_pop($matches));
+
+ $parameters['redir_counter']++;
+
+ $redir_params = array('url' => $redir_url,
+ 'redir_counter', $parameters['redir_counter']);
+
+ $body = HttpRequest::getResponse($redir_params, 'Curl');
+ }
+ }
+
+ return $body;
}
public static function canUse() {

0 comments on commit dc13a1d

Please sign in to comment.