SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sending POST data with fopen

    I have been stuck on this for hours and hours, hoping someone here can help.

    I am trying to access another web page using fopen, but that web page requires POST data (I tried www.domain.com/page.php?param=1) and it won't work, so I am forced to do a POST.

    I can't use CURL because my PHP Version is 4.3.x.

    Can someone advise please?

    Thanks.

  2. #2
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    by the way, the script I am trying to pass data to is not mine and it's on another server.

  3. #3
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check out Snoopy, HTTP client library written entirely in PHP.

  4. #4
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found 2 different scripts online and they both sort of worked but not completely.

    PHP Code:
    <?php
    # $host includes host and path and filename
        # ex: "myserver.com/this/is/path/to/file.php"
    # $query is the POST query data
        # ex: "a=thisstring&number=46&string=thatstring
    # $others is any extra headers you want to send
        # ex: "Accept-Encoding: compress, gzip\r\n"
    function post($host,$query,$others=''){
        
    $path=explode('/',$host);
        
    $host=$path[0];
        unset(
    $path[0]);
        
    $path='/'.(implode('/',$path));
        
    $post="POST $path HTTP/1.1\r\nHost: $host\r\nContent-type: application/x-www-form-urlencoded\r\n${others}User-Agent: Mozilla 4.0\r\nContent-length: ".strlen($query)."\r\nConnection: close\r\n\r\n$query";
        
    $h=fsockopen($host,80);
        
    fwrite($h,$post);
        for(
    $a=0,$r='';!$a;){
            
    $b=fread($h,8192);
            
    $r.=$b;
            
    $a=(($b=='')?1:0);
        }
        
    fclose($h);
        return 
    $r;
    }


    echo 
    post("www.domain.org/page.php","param=1");

    ?>
    For some reason, both of the scripts return results with funky numbers and letters in them.


    Here is a piece of the result with the weird letters/numbers in it:


    15
    <option value='blah'
    f
    >blah</option>
    14
    <option value='blah2'
    e
    >blah2</option>
    13
    <option value='blah3'
    d
    >blah3</option>
    15

    <option value='blah4'
    f
    >blah4</option>
    18


    The things in bold aren't supposed to be there and I don't see them when I try to access the page through browsers, only when I do it with the script.




    so confused....

  5. #5
    Forum Buyer
    Join Date
    Jun 2004
    Location
    United States
    Posts
    811
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not have a form directly post to the other file on the other server like normal?
    Founder/Admin of a pretty decent chat forum
    Download free winterboard themes for your iPhone
    I run sites powered by vbulletin and one about the HTC Jetstream.

  6. #6
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need the data that the destination site returns as I need to do additional work to that data.

  7. #7
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does website return any non-Latin letters?

  8. #8
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by earl-grey View Post
    Does website return any non-Latin letters?
    no, only regular letters and numbers, no non-latin letters.


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
  •