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
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:
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:
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
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]