By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

problem fetching URL

P: n/a
Hi,
I have a problem using a URL fetching algorithm (HTTP 1.0).

To download an image, I used to use this algorithm:

<?php

Header("Content-type: image/gif");

$referer = "http://www.creators.com/comics_show.cfm?comicname=mrh";
$url = "http://www.creators.com/1130/mrh/mrh1202g.gif";

$url = str_replace("&amp;","&",$url);
$tmp = parse_url($url);

$host = $tmp["host"];
$page = $tmp["path"];
if(isset($tmp["query"]))
$page .= "?". $tmp["query"];
if(isset($tmp["fragment"]))
$page .= "#". $tmp["fragment"];
$fp = fsockopen ($host, 80, $errno, $errstr, 15);

if($fp)
{
$response = "";
$header = "";

$request = "GET $page HTTP/1.0\r\nHost: $host\r\nUser-Agent:
Mozilla/4.0\r\nAccept:image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1\r\nAccept-Language:
en-us,en;q=0.5\r\nAccept-Charset:
ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\nKeep-Alive: 300\r\nConnection:
keep-alive\r\nReferer: ". $referer. "\r\n\r\n";

fputs ($fp, $request);

do
{
$header.=fgets($fp, 1024);

if(feof($fp))
break;
}
while (!preg_match("/\\r\\n\\r\\n$/",$header));

$tmp_response = fread ($fp, 2048);

while (!feof($fp))
{
$tmp_response .= fread ($fp, 4096);
}

echo $tmp_response;
@fclose ($fp);
}

?>

while it works in *most* web sites, it does not work in creators.com
and in a few more web sites.

If I use curl:

<?php
Header("Content-type: image/gif");
$referer = "http://www.creators.com/comics_show.cfm?comicname=mrh";
$url = "http://www.creators.com/1130/mrh/mrh1202g.gif";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0");

curl_exec ($ch);
curl_close ($ch);
?>

it works.

The problem is that I have no curl support in my web site. Any one can
detect the error in my code?

Thanks in advance,
Andres
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.