1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need Curl - PHP to pull a single image from another site.

Discussion in 'PHP' started by Twas, Jun 19, 2007.

  1. #1
    I'm just asking a big favor from some php guru out there in DP World.

    My website "A" needs a little piece of php/curl code to goto website "B", get an image and display it on the page it was requested from on website "A".


    This single image does not allow hotlinking from website "B"
    'http://www.theirdomain.com/test.jpg

    If I put in a full url like the one above directly into my browser, I can see the image, but if I use a SRC html statement to put it on my page it's blocked.

    Curl has the ability to provide browser and referer information which would allow me to get the image.

    Any example code would be greatly appreciated.

    Thanks
     
    Twas, Jun 19, 2007 IP
  2. SeLfkiLL

    SeLfkiLL Active Member

    Messages:
    85
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    50
    #2
    Try this:

    
    $link = curl_init();
    
    curl_setopt($link, CURLOPT_URL, "http://www.theirdomain.com/test.jpg");
    curl_setopt($link, CURLOPT_REFERER, "http://www.theirdomain.com/");
    curl_setopt($link, CURLOPT_HEADER, false);
    
    curl_exec($link);
    
    curl_close($link);
    
    PHP:
     
    SeLfkiLL, Jun 19, 2007 IP
  3. loibe

    loibe Peon

    Messages:
    6
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    and this:

    w/o the hotlink thingy =]

    
    <?php
    function savePhoto($remoteImage, $isbn) {
    	$ch = curl_init();
    	curl_setopt ($ch, CURLOPT_URL, $remoteImage);
    	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
    	$fileContents = curl_exec($ch);
    	curl_close($ch);
    	$newImg = imagecreatefromstring($fileContents);
    	return imagejpeg($newImg, "./photos/{$isbn}.jpg",100);
    }
    #be sure to chmod your destination dir - in my case 'photos' =]
    savePhoto('http://g-ec2.images-amazon.com/images/I/21VM0C7V6AL._SS160_.jpg', 1904811892);
    ?>
    
    PHP:
     
    loibe, Jun 19, 2007 IP
  4. nabiha

    nabiha Peon

    Messages:
    111
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    @Twas:

    Did it work for you?
     
    nabiha, Jun 20, 2007 IP
  5. Twas

    Twas Guest

    Messages:
    137
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You two ( SelFkill and loibe) really helped me out. SelFkill you made me realize how to do it, though my coding skills almost don't exist, and loibe you provided it all down to the last byte.

    All I had to do was put in my url, change imagejpg to imagegif, change the directory to where the file is stored, and the gif file name.

    I really appreciate your time in this because I spent a few hours and didn't get anywhere.

    Thanks :)
     
    Twas, Jun 20, 2007 IP
  6. abhishek198

    abhishek198 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Try this ..
    
    Try this ..
    [PHP]
    	$img = "http://yourdomain.com/test.jpg";
            $fullpath = basename($img);
    
            $ch = curl_init ($img);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
            $rawdata=curl_exec($ch);
            if(strpos($rawdata,"Not Found") === false) {
                if(file_exists($fullpath)) {
                    unlink($fullpath);
                }
                $fp = fopen($fullpath,'x');
                fwrite($fp, $rawdata);
                fclose($fp);
                $this->log("success");
    
            }else {
                $this->log("fail");
            }
            curl_close ($ch);
    
    PHP:
    [/PHP]
     
    abhishek198, Jun 1, 2010 IP