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;





    public function load(){   



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


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


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




    public function getImages(){


        $images = array();


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

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




             // 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,



        $response = curl_exec($request);



        $doc = new DOMDocument();






        return $doc;



    protected function checkUrl(){


        $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='' 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


                // 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;


$imageGetter = new ImageGetter($url);

$images = $imageGetter->getImages();

// output images

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


echo json_encode($result);

that’s a wrong code !!!!


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


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


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.