How Download Images Using Curl

Hi guys,

I'm a newbie in Yii and PHP i just graduated in college last 2 months. 

Im currently trying to learn cUrl on how to download images in a given url. However the problem is

when i try to output the images it shows only a json code eq. {"image": "src":} etcetera

I dont know what to do next or is there a problem w/ the code.

Can someone help me with this, I will really appreciate it.

Thanks guys!

Here is my code:




class ImageGetter

{

    private $_url;

    private $_document;

    private $_base;

    private $_urlParts;

    

    public function __construct($url) {

        $this->_url = $url;

        $this->checkUrl();

        

    }

    

    public function load(){   

        if($this->_document)

            return;

        $this->_document = self::getData($this->_url);

        

        $this->_base = self::getBase($this->_document);

        if(!$this->_base){

            $this->_base = $this->_url;

        }

    }

    

    public function getImages(){

        $this->load();

        $images = array();

        

        foreach($this->_document->getElementsByTagName('img') as $img){

            $image = array('src' => self::make_absolute($img->getAttribute('src'), $this->_base));

            

             if(!$image['src'])

                continue;

             // add to the array

             $images[$image['src']] = $image;

        }

       return array_values($images);

    }

    

    public static function getData($url){

        $request = curl_init();

        curl_setopt_array($request, array(

            CURLOPT_URL => $url,

            CURLOPT_HEADER => FALSE,

            CURLOPT_RETURNTRANSFER => TRUE,                     

            CURLOPT_MAXREDIRS => 10,

            CURLOPT_FOLLOWLOCATION => TRUE,


        ));

        $response = curl_exec($request);

        curl_close($request);

        

        $doc = new DOMDocument();

        if($response){

            libxml_use_internal_errors(true);

            $doc->loadHTML($response);

            libxml_clear_errors();


        }

        return $doc;

    }

    

    protected function checkUrl(){

        $flags = FILTER_FLAG_SCHEME_REQUIRED || FILTER_FLAG_HOST_REQUIRED;

        $urlOk = filter_var($this->_url, FILTER_VALIDATE_URL, $flags);

        $this->_urlParts = parse_url($this->_url);

        if(!$urlOk || $this->_urlParts['scheme'] != 'http'){

            throw new Exception($this->_url . ' is not a valid url.');

        }

    }


    private static function getBase(DOMDocument $doc){

        $tags = $doc->getElementsByTagName('base');


       foreach($tags as $tag)

            return $tag->getAttribute('href');


            return NULL;

    }


    // I just got this function from the web <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/smile.gif' class='bbc_emoticon' alt=':)' />

    private static function make_absolute($url, $base) 

        {

                // Return base if no url

                if( ! $url) return $base;


                // Already absolute URL

                if(parse_url($url, PHP_URL_SCHEME) != '') return $url;

                

                // Only containing query or anchor

                if($url[0] == '#' || $url[0] == '?') return $base.$url;

                

                // Parse base URL and convert to local variables: $scheme, $host, $path

                extract(parse_url($base));


                // If no path, use /

                if( ! isset($path)) $path = '/';

         

                // Remove non-directory element from path

                $path = preg_replace('#/[^/]*$#', '', $path);

         

                // Destroy path if relative url points to root

                if($url[0] == '/') $path = '';

                

                // Dirty absolute URL

                $abs = "$host$path/$url";

         

                // Replace '//' or '/./' or '/foo/../' with '/'

                $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');

                for($n = 1; $n > 0; $abs = preg_replace($re, '/', $abs, -1, $n)) {}

                

                // Absolute URL is ready!

                return $scheme.'://'.$abs;

        }

}



and here is the other file:




header('Content-Type: application/json; charset=utf-8');


$url = array_key_exists('url', $_POST) ? $_POST['url'] : null;


require('ImageGetter.php');

$imageGetter = new ImageGetter($url);


$images = $imageGetter->getImages();


// output images

$result = array('images' => $images);

ob_start('ob_gzhandler');

echo json_encode($result);



that’s a wrong code !!!!

@jh@mei25

Please wrap your code with ‘code’ tags for easier reading.

@sumanstm21

Please provide the solution or at least point out the errors instead of this useless comment.

Done.

There doesn’t seem to be anything Yii related in this thread, so I’m moving it into General PHP Topics.

Note to the original author:

Given that this isn’t Yii related, you may get an answer more quickly on Stack Overflow.