Try to get a file from a server by PHP

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

    Try to get a file from a server by PHP

    I am trying to make afunction to get information from one file and
    print it or put it in an array, the problem in headers I can't figure
    that it is giving me this:
    ********
    HTTP/1.1 400 Bad Request Date: Fri, 21 Oct 2005 14:48:06 GMT Server:
    Apache Connection: close Content-Type: text/html; charset=iso-8859-1
    Bad Request
    Your browser sent a request that this server could not understand.

    Request header field is missing colon separator.

    Host=www.imtec. ba
    ********
    My code is :
    *********
    <?
    function getFile($host,$ file)
    {
    $request="GET $file HTTP/1.0\r\n"; // This is the path part of the
    link
    $request.="Host =$host\r\n\r\n" ; // This is the server part of the
    link

    // Now let's try connecting:
    $fp=fsockopen($ host, 80, $errno, $errstr, 30);

    // Let's see if we made it
    if (!$fp)
    {
    return(false);
    }
    else
    {
    // If we made it, let's send the request:
    fputs($fp, $request);

    while (!feof($fp))
    {
    $response.=fget s($fp, 128);
    }
    fclose($fp);
    return($respons e);
    }
    }
    echo getFile("www.im tec.ba","index. php");
    ?>
    *********
    Any help, what is wrong..
    Thank you

  • Jamie  Davison

    #2
    Re: Try to get a file from a server by PHP

    On 10/21/05 11:00 AM, in article
    1129906853.3638 29.103040@g47g2 00...legr oups.com, "NurAzije"
    <nurazije@gmail .com> wrote:
    [color=blue]
    > I am trying to make afunction to get information from one file and
    > print it or put it in an array, the problem in headers I can't figure
    > that it is giving me this:
    > ********
    > HTTP/1.1 400 Bad Request Date: Fri, 21 Oct 2005 14:48:06 GMT Server:
    > Apache Connection: close Content-Type: text/html; charset=iso-8859-1
    > Bad Request
    > Your browser sent a request that this server could not understand.
    >
    > Request header field is missing colon separator.
    >
    > Host=www.imtec. ba
    > ********
    > My code is :
    > *********
    > <?
    > function getFile($host,$ file)
    > {
    > $request="GET $file HTTP/1.0\r\n"; // This is the path part of the
    > link
    > $request.="Host =$host\r\n\r\n" ; // This is the server part of the
    > link
    >
    > // Now let's try connecting:
    > $fp=fsockopen($ host, 80, $errno, $errstr, 30);
    >
    > // Let's see if we made it
    > if (!$fp)
    > {
    > return(false);
    > }
    > else
    > {
    > // If we made it, let's send the request:
    > fputs($fp, $request);
    >
    > while (!feof($fp))
    > {
    > $response.=fget s($fp, 128);
    > }
    > fclose($fp);
    > return($respons e);
    > }
    > }
    > echo getFile("www.im tec.ba","index. php");
    > ?>
    > *********
    > Any help, what is wrong..
    > Thank you
    >[/color]


    I would suggest using CURL whenever possible . .

    If you have CURL compiled into your PHP distribution . . .

    The folowing pulled from http://us3.php.net/curl

    <?php

    $ch = curl_init("http ://www.imtec.ba/");
    $fp = fopen("imtect.t xt", "w");

    curl_setopt($ch , CURLOPT_FILE, $fp);
    curl_setopt($ch , CURLOPT_HEADER, 0);

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


    If not . . .

    <?php

    $host = http://www.imtec.ba;
    $curl_cmd = '/your/path/to/curl '. $host;
    $imtec_file = exec($curl_cmd) ;


    . . . Save it
    ?>






    Comment

    • Philip Ronan

      #3
      Re: Try to get a file from a server by PHP

      "Jamie Davison" wrote:
      [color=blue]
      > I would suggest using CURL whenever possible . .[/color]

      .... or just file_get_conten ts() <http://www.php.net/file_get_conten ts>

      NurAzije: If it helps at all, the error you're getting is caused by this
      line:
      [color=blue][color=green]
      >> $request.="Host =$host\r\n\r\n"[/color][/color]

      Headers should be followed by a colon and a space, not an equals sign. So
      maybe $request.="Host : $host\r\n\r\n" would have been a bit more successful.
      But your code is still going to fall down flat if you receive a chunked
      response, for example.

      --
      phil [dot] ronan @ virgin [dot] net



      Comment

      Working...