PHP Post data with Fsockopen

前端 未结 10 1561
梦如初夏
梦如初夏 2020-12-01 15:20

I am attempting to post data using fsockopen, and then returning the result. Here is my current code:



        
10条回答
  •  被撕碎了的回忆
    2020-12-01 15:29

    There are many small errors in your code. Here's a snippet which is tested and works.

     'world'
    );
    $content = http_build_query($vars);
    
    fwrite($fp, "POST /reposter.php HTTP/1.1\r\n");
    fwrite($fp, "Host: example.com\r\n");
    fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
    fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
    fwrite($fp, "Connection: close\r\n");
    fwrite($fp, "\r\n");
    
    fwrite($fp, $content);
    
    header('Content-type: text/plain');
    while (!feof($fp)) {
        echo fgets($fp, 1024);
    }
    

    And then at example.com/reposter.php put this

    When run you should get output something like

    HTTP/1.1 200 OK
    Date: Wed, 05 Jan 2011 21:24:07 GMT
    Server: Apache
    X-Powered-By: PHP/5.2.9
    Vary: Host
    Content-Type: text/html
    Connection: close
    
    1f
    Array
    (
        [hello] => world
    )
    0
    

提交回复
热议问题