特定のハッシュタグを付けてツイートから画像を取得するスクリプトに取り組んでいます。通常、このスクリプトは正常に機能しています。これは、open_basedirとsafe_modeをシャットダウンしても問題がない専用サーバーにのみ実装されているためです。
ただし、現在、open_basedirがオンになっている共有サーバーで彼のWebサイトをホストしているクライアントに対して、このスクリプトを機能させようとしています。結果:Twitterの画像URLからリダイレクトされたURLを取得しようとすると、エラーが発生します(Twitpic、Yfrogなどのいくつかのクライアント)。
これは私が得るエラーです:
警告:curl_setopt()[function.curl-setopt]:セーフモードまたはopen_basedirが設定されている場合、CURLOPT_FOLLOWLOCATIONをアクティブ化できません。
基本的に、スクリプトはデータベースにツイートを生成し、そこにURLも保存されます。しかし、サーバー上にイメージを作成しようとすると、結果が表示されます。直接URLからMIMEタイプを見つけることができないため、以下のスクリプトに示されているように「非表示」になります。したがって、データベースに保存されている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>';
?>