NYCPHP Meetup

NYPHP.org

[nycphp-talk] images with cURL

Joseph Crawford codebowl at gmail.com
Wed Mar 15 20:14:07 EST 2006


Hello guys,

i am having an issue here, i am trying to display images on a site with it
masking the actual image location, we are grabbing circuit city's images
(with permission)
to be shown on our site, we have all the url's in the db so i created an
image.php file that we will use in our image links, here it is

<?php
include_once('config/config.php');
include_once(LIBRARY.'ConnectionPool.php');

$imgType = (isset($_GET['type'])) ? ucfirst($_GET['type']) : 'Item';
$rowID = (isset($_GET['img'])) ? $_GET['img'] : null;

if(!is_null($rowID)) {
    $dblink = new ConnectionPool(true);
    $sql = "SELECT ".$imgType."_Image as image FROM MasterItems WHERE
Record_ID=".$rowID;
    $res = $dblink->db_query($sql);
    $row = $dblink->db_fetch_object($res);

    $cookieFile = BASE_PATH.'cache/circuitcities.cookie.txt';

    $ch = curl_init($row->image);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, 'http://circuitcity.com/');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT
5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1");
    curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9
,text/plain;q=0.8,image/png,*/*;q=0.5",
    ));
    $source = curl_exec($ch);
    curl_close($ch);
    header("content-type: image/jpeg");
    echo $source;
}
?>

In FireFox when you hit the page you see a broken image (txt url in mine)
however if you view source you see the binary from the image.

This works fine if i hard code the url in curl_init, and i have tried
urlencode() and trim() on the url before feeding it to curl.

you can see the file in use here

http://b2cdev.licketyship.com/image.php?img=50903

Any help would be appreciated.

Thanks,

--
Joseph Crawford Jr.
Zend Certified Engineer
Codebowl Solutions, Inc.
http://www.codebowl.com/
1-802-671-2021
codebowl at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20060315/bd3add8b/attachment.html>


More information about the talk mailing list