2012-04-05 2 views
1

Я работаю над скриптом, который вытягивает изображения из твитов с определенным хэштегом. Обычно этот скрипт работает отлично, поскольку он только реализован на выделенных серверах, где open_basedir и safe_mode не были проблемой для закрытия.Альтернатива для CURLOPT_FOLLOWLOCATION на общем сервере с open_basedir ON

Однако, сейчас я пытаюсь заставить этот скрипт работать для клиента, который размещает свой сайт на общем сервере, где open_basedir включен. Результат. Предоставление мне ошибки при попытке получить перенаправленный URL-адрес из URL-адреса изображения Twitter (несколько клиентов, таких как Twitpic, Yfrog).

Это ошибка, я получаю:

Предупреждение: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION не может быть активирована, когда в safe_mode или open_basedir установлен в .....

В основном сценарий генерирует твиты в базе данных, где также сохраняется URL-адрес. Но при попытке создать изображение на сервере он дает мне результат: «скрытый», как указано в приведенном ниже скрипте, поскольку он не может найти тип MIME из прямого URL-адреса. Таким образом, URL-адрес, хранящийся в базе данных, должен следовать по пути к изображению, чтобы вытащить изображение и создать его.

Мой вопрос: есть ли способ переписать часть FOLLOWLOCATION? Я искал в течение примерно 3 часов для решения, но не могу показаться, чтобы реализовать это право ...

Надеюсь, кто-нибудь может мне помочь

Сценарий:

<?php 
# Database gegevens inladen 
include('config.php'); 

$query = mysql_query("select * from tweets where loaded=0 and hidden=0 order by id asc limit ".$maximgload); 
if(mysql_num_rows($query) > 0){ 
while($db = mysql_fetch_object($query)){ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $db->url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    $buffer = curl_exec($ch); 
    curl_close($ch); 
    if(!empty($buffer)){ 
     # Afbeelding opslaan 
     $fp = fopen($imgdir.'/'.$db->id.'.temp', 'x'); 
     fwrite($fp, $buffer); 
     fclose($fp); 


     if(!function_exists('mime_content_type')) { 

      function mime_content_type($filename) { 

        $mime_types = array(

         // images 
         'png' => 'image/png', 
         'jpe' => 'image/jpeg', 
         'jpeg' => 'image/jpeg', 
         'jpg' => 'image/jpeg', 
        ); 

       $ext = strtolower(array_pop(explode('.',$filename))); 
       if (array_key_exists($ext, $mime_types)) { 
        return $mime_types[$ext]; 
       } 
       elseif (function_exists('finfo_open')) { 
        $finfo = finfo_open(FILEINFO_MIME); 
        $mimetype = finfo_file($finfo, $filename); 
        finfo_close($finfo); 
        return $mimetype; 
       } 
       else { 
        return 'application/octet-stream'; 
       } 
      } 
     } 

     # Bestand omzetten naar juiste formaat 
     $mimetype = mime_content_type($imgdir.'/'.$db->id.'.temp'); 
     # Jpg 
     if($mimetype == 'image/jpeg'){ 
      rename($imgdir.'/'.$db->id.'.temp',$imgdir.'/'.$db->id.'.jpg'); 
     } 
     # Png 
     elseif($mimetype == 'image/png'){ 
      rename($imgdir.'/'.$db->id.'.temp',$imgdir.'/'.$db->id.'.png'); 
     } 
     # Ander (onbekend) formaat? weg er mee! 
     else{ 
      @unlink($imgdir.'/'.$db->id.'.temp'); 
      @mysql_query("update tweets set hidden='1' where id='".$db->id."'"); 
      $result = 'file '.$db->id.' onbekend'; 
      break; 
     } 

     # Thumbnail maken 
     $source_image = imagecreatefromstring($buffer); 
     $source_width = imagesx($source_image); 
     $source_height = imagesy($source_image); 
     $source_ratio = $source_width/$source_height; 
     $destination_ratio = $thumbwidth/$thumbheight; 
     // landscape 
     if($source_ratio > $destination_ratio){ 
      $temp_width = (int)($source_height * $destination_ratio); 
      $temp_height = $source_height; 
      $source_x = (int)(($source_width - $temp_width)/2); 
      $source_y = 0; 
     } 
     // portrait 
     else { 
      $temp_width = $source_width; 
      $temp_height = (int)($source_width * $destination_ratio); 
      $source_x = 0; 
      $source_y = (int)(($source_height - $temp_height)/2); 
     } 
     $destination_x = 0; 
     $destination_y = 0; 
     $source_width = $temp_width; 
     $source_height = $temp_height; 
     $new_destination_width = $thumbwidth; 
     $new_destination_height = $thumbheight; 

     $thumb = imagecreatetruecolor($thumbwidth,$thumbheight); 
     imagecopyresampled($thumb,$source_image,$destination_x,$destination_y,$source_x,$source_y,$new_destination_width,$new_destination_height,$source_width,$source_height); 
     # Thumbnail opslaan 
     if($mimetype == 'image/jpeg'){ 
      imagejpeg($thumb,$imgdir.'/'.$db->id.'_thumb.jpg'); 
     } 
     else{ 
      imagepng($thumb,$imgdir.'/'.$db->id.'_thumb.png'); 
     } 

     # Bijwerken in database 
     mysql_query("update tweets set loaded='1', mime='".$mimetype."' where id='".$db->id."'"); 
     $result = 'afb '.$db->id.' gemaakt'; 
    } 
    # Kan url niet openen? Dan uit database gooien 
    else{ 
     mysql_query("update tweets set hidden='1' where id='".$db->id."'"); 
     $result = 'afb '.$db->id.' hidden'; 
    } 
} 
} 
else{ 
$result = 'done'; 
} 
echo '<html> 
<head> 
<meta http-equiv="refresh" content="2"> 
<title>'.$result.'</title> 
</head> 
</html>'; 
?> 

ответ

1

Проверить это один функция, его используют для имитации повторения followlocation

/** 
* @param cURL $ch      - uchwyt do cURL 
* @param int $redirects    - przekierowania 
* @param bool $curlopt_returntransfer - CURLOPT_RETURNTRANSFER 
* @param int $curlopt_maxredirs  - CURLOPT_MAXREDIRS 
* @param bool $curlopt_header   - CURLOPT_HEADER 
* @return mixed 
*/ 
function curl_redirect_exec($ch, &$redirects, $curlopt_returntransfer = false, $curlopt_maxredirs = 10, $curlopt_header = false) { 
    curl_setopt($ch, CURLOPT_HEADER, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $data = curl_exec($ch); 
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    $exceeded_max_redirects = $curlopt_maxredirs > $redirects; 
    $exist_more_redirects = false; 
    if ($http_code == 301 || $http_code == 302) { 
     if ($exceeded_max_redirects) { 
      list($header) = explode("\r\n\r\n", $data, 2); 
      $matches = array(); 
      preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches); 
      $url = trim(array_pop($matches)); 
      $url_parsed = parse_url($url); 
      if (isset($url_parsed)) { 
       curl_setopt($ch, CURLOPT_URL, $url); 
       $redirects++; 
       return curl_redirect_exec($ch, $redirects, $curlopt_returntransfer, $curlopt_maxredirs, $curlopt_header); 
      } 
     } 
     else { 
      $exist_more_redirects = true; 
     } 
    } 
    if ($data !== false) { 
     if (!$curlopt_header) 
      list(,$data) = explode("\r\n\r\n", $data, 2); 
     if ($exist_more_redirects) return false; 
     if ($curlopt_returntransfer) { 
      return $data; 
     } 
     else { 
      echo $data; 
      if (curl_errno($ch) === 0) return true; 
      else return false; 
     } 
    } 
    else { 
     return false; 
    } 
} 

Пример использования:

$ch = curl_init("http://some-website.com"); 
$redirects = 0; 
curl_redirect_exec($ch, $redirects); 
$info = curl_getinfo($ch); 
curl_close($ch); 

Код принятой: http://antczak.org/2009/12/curl-rozwiazanie-problemu-z-curlopt_followlocation/