Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Redirect 在打开open_basedir的共享服务器上进行CURLOPT_FOLLOWLOCATION的替代方法_Redirect_Twitter_Curl - Fatal编程技术网

Redirect 在打开open_basedir的共享服务器上进行CURLOPT_FOLLOWLOCATION的替代方法

Redirect 在打开open_basedir的共享服务器上进行CURLOPT_FOLLOWLOCATION的替代方法,redirect,twitter,curl,Redirect,Twitter,Curl,我正在编写一个脚本,用一个特定的标签从推特中提取图像。正常情况下,这个脚本工作得很好,因为它只在专用服务器上实现,在这些服务器上,open_basedir和safe_模式关闭没有问题 然而,现在,我正试图让这个脚本为一个客户端工作,该客户端将他的网站托管在一个共享服务器上,open_basedir就在这个服务器上。结果:在尝试从Twitter图片URL(几个客户端,如Twitpic、Yfrog)获取重定向的URL时出错 这是我得到的错误: 警告:curl_setopt()[function.cu

我正在编写一个脚本,用一个特定的标签从推特中提取图像。正常情况下,这个脚本工作得很好,因为它只在专用服务器上实现,在这些服务器上,open_basedir和safe_模式关闭没有问题

然而,现在,我正试图让这个脚本为一个客户端工作,该客户端将他的网站托管在一个共享服务器上,open_basedir就在这个服务器上。结果:在尝试从Twitter图片URL(几个客户端,如Twitpic、Yfrog)获取重定向的URL时出错

这是我得到的错误:

警告:curl_setopt()[function.curl setopt]:在安全模式下或在..中设置了open_basedir时,无法激活CURLOPT_FOLLOWLOCATION

基本上,脚本在数据库中生成tweet,URL也存储在数据库中。但是当尝试在服务器上创建映像时,它会给出如下脚本中所述的结果:“隐藏”,因为它无法从直接URL中找到MIME类型。因此,存储在数据库中的URL需要跟随到图像路径以提取图像并创建它

我的问题是,有没有办法重写下面的部分?我已经搜索了大约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>';
?>

检查这一个函数,它使用递归来模拟跟踪定位

/**
 * @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);

代码取自:

检查这一函数,它使用递归来模拟跟踪定位

/**
 * @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);
代码取自: