problem fetching URL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andres Baravalle

    #1

    problem fetching URL

    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.co m/comics_show.cfm ?comicname=mrh" ;
    $url = "http://www.creators.co m/1130/mrh/mrh1202g.gif";

    $url = str_replace("&a mp;","&",$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:i mage/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1\r\nAcce pt-Language:
    en-us,en;q=0.5\r\n Accept-Charset:
    ISO-8859-1,utf-8;q=0.7,*;q=0.7 \r\nKeep-Alive: 300\r\nConnecti on:
    keep-alive\r\nRefere r: ". $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.co m/comics_show.cfm ?comicname=mrh" ;
    $url = "http://www.creators.co m/1130/mrh/mrh1202g.gif";

    $ch = curl_init();
    curl_setopt($ch , CURLOPT_URL,$ur l);
    curl_setopt($ch , CURLOPT_REFERER , $referer);
    curl_setopt($ch , CURLOPT_USERAGE NT, "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
Working...