SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast Roj2002's Avatar
    Join Date
    Jan 2002
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sending a "header" with PHP

    I wonder, is it possible to sent a header with PHP that looks something like this:

    GET /support/admin/database.txt HTTP/1.1
    Host: www.xyz.com
    Connection: close
    Accept: */*
    Authorization: Basic c2F3czpzYXdzMjAwMA==

    I can do it with VB, but I have to do it with PHP...

    Michael

  2. #2
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's my get_URL function...
    It can be modified to do what you need.

    S

    PHP Code:
    <?php
    /**
    [n] &get_URL($url, $agent="Sean's Spider")
    [d] requests a URL, including full HTTP headers
    [p] $url   = URL to request
    [p] $agent = (default="IgnitionWeb Spider") masquerade HTTP_USER_AGENT as this
    [r] associative array of $url. $url['content'] contains the body. Headers are also returned.
    */
    function &get_URL($url$agent="IgnitionWeb Spider")
    {

        if (
    strtolower(substr($url07)) == "http://") {
            
    $url substr($url,7);
        }
        
        list(
    $host$path) = split("/"$url2);
        
        
    $fp fsockopen ($host80$errno$errstr30);
        
        if (!
    $fp) {
            die(
    "ERROR: $errstr ($errno)<br>\n");
        } else {
            
    $request "GET /$path HTTP/1.0\r\n"
                      
    ."Host: $host\r\n"
                      
    ."User-Agent: $agent\r\n"
                      
    ."\r\n";
        
            
    fputs ($fp$request);
        
            
    $result "";
        
            while (!
    feof($fp)) {
                
    $result .= fgets ($fp,128);
            }
        
            
    fclose ($fp);
        }
     
        list(
    $ret['headers'], $ret['content']) = split ("\r\n\r\n"$result2);

        
    $headers split("\n"$ret['headers']);
        list(
    $ret['protocol'], $ret['result_code'], $ret['result_msg']) = split(" "$headers[0]);
     
        return 
    $ret;
    }
    ?>

  3. #3
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    scoates, can you tell me what the & before the function name is for? i've seen it before, but don't know if it has to do with references, like other things with &, or what.

    thanks.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  4. #4
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I inherited it from one of my former co-workers.

    My understanding is that it passes the return value as a reference instead of making a copy.. which, now that I think about it doesn't really make sense since the original would likely get garbage-collected at the end of the function (on return) anyway.

    If you get rid of the &, it'll still work. Enough with the making me think. (-:

    Seriously, though, I'd like to know too.

    S

  5. #5
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, me too. i'll have to see if it's mentioned in the manual for functions or references. i've quickly looked for information about it there before, but found nothing. i'll look closer next time and let you know if i find out.

  6. #6
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh.
    I'm dumb, and I'll get rid of the & in the morning. (-:

    It makes sense.. I saw it done on our DB object (which is where it's useful), but I thought it was because of the array (I wrote that code a long time ago)..

    here: http://www.php.net/manual/en/languag...ces.return.php

    Makes sense now.

    S

  7. #7
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the link, but i don't really understand anything on that page. LOL it looks to me, though, that i don't want to use the & in most cases.

  8. #8
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found an answer to what references are that makes sense here

    Making arguments be passed by reference
    By default, function arguments are passed by value (so that if you change the value of the argument within the function, it does not get changed outside of the function). If you wish to allow a function to modify its arguments, you must pass them by reference.

    If you want an argument to a function to always be passed by reference, you can prepend an ampersand (&) to the argument name in the function definition:


    function add_some_extra(&$string) {
    $string .= 'and something extra.';
    }
    $str = 'This is a string, ';
    add_some_extra($str);
    echo $str; // outputs 'This is a string, and something extra.'



    If you wish to pass a variable by reference to a function which does not do this by default, you may prepend an ampersand to the argument name in the function call:


    function foo ($bar) {
    $bar .= ' and something extra.';
    }
    $str = 'This is a string, ';
    foo ($str);
    echo $str; // outputs 'This is a string, '
    foo (&$str);
    echo $str; // outputs 'This is a string, and something extra.'

  9. #9
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HarryF there is a subtle difference between passing a reference and returning a reference. You example is about passing a reference to a function. scotes function returns a reference. However, your example is a more common use of references (passing them to functions).

    If people go back and read that manual page and contemplate on it and the example given and the simple hueristic that in PHP variables are passed by copy (passing to and returning from functions) and that you can override this by expicity passing a reference (to or from a function) then the penny will drop

    For example:
    PHP Code:
    function foo($bar) {
       
    $bar $bar 2;
       return 
    $bar;
    }

    $zoot 5;
    $zog foo($zoot);
    echo 
    $zog // value: 10
    echo $zoot // value: 5

    function foo2(&$bar) {
       
    $bar $bar 2;
       return 
    $bar;
    }

    $zoot 5;
    $zog foo2($zoot);
    echo 
    $zog // value: 10
    echo $zoot // value: 10

    $nee 3;
    foo2($nee);
    echo 
    $nee // value: 6

    class MyClass {
       var 
    $myValue;
       
       function 
    MyClass($val) {
          
    $this->$myValue $val;
       }
    }

    // this function will return a reference
    // to a new myClass object
    // not a copy (why would we need a copy?)!
    function &myClassFactory($bar) {
       
    $temp = new MyClass($bar);
       return 
    $temp;

    Last edited by freakysid; Apr 10, 2002 at 06:14.

  10. #10
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome explaination!

    [Reaches for the button to rate this thread then discovers that's not possible in this forum and sighs]

    Many thanks FreakySid!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •