zoukankan      html  css  js  c++  java
  • php如何发post请求

     /**
         *  post提交数据
         * @param  string $url 请求Url
         * @param  array $datas 提交的数据
         * @return url响应返回的html
         */
        function sendPost($url, $datas) {
            $temps = array();
            foreach ($datas as $key => $value) {
                $temps[] = sprintf('%s=%s', $key, $value);
            }
            $post_data = implode('&', $temps);
            $url_info = parse_url($url);
            if(empty($url_info['port']))
            {
                $url_info['port']=80;
            }
            $httpheader = "POST " . $url_info['path'] . " HTTP/1.0
    ";         //get请求,这里就改为get
            $httpheader.= "Host:" . $url_info['host'] . "
    ";
            $httpheader.= "Content-Type:application/x-www-form-urlencoded
    ";
            $httpheader.= "Content-Length:" . strlen($post_data) . "
    ";       //描述HTTP消息实体的传输长度
            $httpheader.= "Connection:close
    
    ";
            $httpheader.= $post_data;
            $fd = fsockopen($url_info['host'], $url_info['port']);          //post请求
            fwrite($fd, $httpheader);
            $gets = "";
            $headerFlag = true;
            while (!feof($fd)) {                          //检测是否到达了文件末尾,如果服务器没有关闭fsockopen,feof会等到超时   默认的超时限制是 60 秒,可以使用 stream_set_timeout() 来改变这个值。
                if (($header = @fgets($fd)) && ($header == "
    " || $header == "
    ")) {
                    break;
                }
            }
            while (!feof($fd)) {
                $gets.= fread($fd, 128);     //如果只是想将一个文件的内容读入到一个字符串中,请使用 file_get_contents(),它的性能比 fread() 好得多
            }
            fclose($fd);
    
            return $gets;
        }

    亲测好用

  • 相关阅读:
    Boost.Asio c++ 网络编程翻译(10)
    建站手册:网站品质
    建站手册-template
    CDN:分类
    CDN:BootCDN 项目列表-摘录-20180405
    CDN:BootCDN
    CDN:目录
    CDN-template
    JavaScript-Tool:md5.js
    Regexp-Utils:基本
  • 原文地址:https://www.cnblogs.com/hanshuai0921/p/7928975.html
Copyright © 2011-2022 走看看