如何利用http来发送post请求

大兔子大兔子 提交于 2019-11-28 13:00:32

服务器接收:

<?php

    error_reporting(0);
    function get_contents_input()
    {
        $data = file_get_contents('php://input');     //读取文档流中的数据
        @ file_put_contents('./data/a.txt', $data);   //写入文件中
    }
    get_contents_input();
?>

 

客服端请求: 

<?php
    
    $data = file_get_contents('./data/b.txt');
     $http_entity_body = $data; 
     $http_entity_type = 'application/x-www-form-urlencoded'; 
     $http_entity_length = strlen($http_entity_body); 
     $host = '127.0.0.1'; 
     $port = 8080; 
     $path = '/20160825/test13.php'; 
    
    
    $fp = fsockopen($host, $port, $error_no, $error_desc, 30);   //创建连接
    if($fp)
    {

        /**

        * 发送头信息

        */    
        fputs($fp, "POST {$path} HTTP/1.1\r\n"); 
        fputs($fp, "Host: {$host}\r\n"); 
        fputs($fp, "Content-Type: {$http_entity_type}\r\n"); 
        fputs($fp, "Content-Length: {$http_entity_length}\r\n"); 
        fputs($fp, "Connection: close\r\n\r\n"); 
        fputs($fp, $http_entity_body . "\r\n\r\n"); 
         

        //读取返回信息
        while (!feof($fp)) { 
         $d .= fgets($fp, 4096); 
        } 
        fclose($fp); 
        echo $d; 
        
    }

?>

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!