zoukankan      html  css  js  c++  java
  • php用fsockopen实现post提交数据并获得返回数据

    /**
     * 函数介绍:    用于post方式提交数据
     * 输入参数:    完整url, 数据
     * 返回值  :    接口返回值
     */
    function post_it($url, $data = '', $timeout = '6') {
        $urls = parse_url($url);
        if (!$urls) {
            return "-500";
        }
        $port = isset($urls['port']) ? $urls['port'] : null; //isset()判断
        if (!$port) {
            $port = "80";
        }
        $host = $urls['host'];
        //----------------------------------------------//
        $httpheader = "POST " . $url . " HTTP/1.0" . "
    " . "Accept:*/*" . "
    " . "Accept-Language:zh-cn" . "
    " . "Referer:" . $url . "
    " . "Content-Type:application/x-www-form-urlencoded" . "
    " . "User-Agent:Mozilla/4.0(compatible;MSIE 7.0;Windows NT 5.1)" . "
    " . "Host:" . $host . "
    " . "Content-Length:" . strlen($data) . "
    " . "
    " . $data;
        $fd = fsockopen($host, $port);
        if (!is_resource($fd)) {
            return "fsockopen failed";
        }
        fwrite($fd, $httpheader);
        stream_set_blocking($fd, TRUE);
        stream_set_timeout($fd, $timeout);
        $info = stream_get_meta_data($fd);
        $gets = "";
        while ((!feof($fd)) && (!$info['timed_out'])) {
            $data .= fgets($fd, 8192);
            $info = stream_get_meta_data($fd);
            @ob_flush();
            flush();
        }
        if ($info['timed_out']) {
            return "timeout";
        } else {
            //echo $data;
            $contentInfo = explode("
    
    ", str_replace("
    ", "", $data));
            
            if (!strstr($contentInfo[0], "HTTP/1.1 200 OK")) {
                return -10;
            }
            return trim($contentInfo[1]);
        }
    }

    /**
     * 函数介绍:    用于post方式提交数据
     * 输入参数:    完整url, 数据
     * 返回值  :    接口返回值
     */
    function post_it($url, $data = '', $timeout = '6') {
        $urls = parse_url($url);
        if (!$urls) {
            return "-500";
        }
        $port = isset($urls['port']) ? $urls['port'] : null; //isset()判断
        if (!$port) {
            $port = "80";
        }
        $host = $urls['host'];
        //----------------------------------------------//
        $httpheader = "POST " . $url . " HTTP/1.0" . " " . "Accept:*/*" . " " . "Accept-Language:zh-cn" . " " . "Referer:" . $url . " " . "Content-Type:application/x-www-form-urlencoded" . " " . "User-Agent:Mozilla/4.0(compatible;MSIE 7.0;Windows NT 5.1)" . " " . "Host:" . $host . " " . "Content-Length:" . strlen($data) . " " . " " . $data;
        $fd = fsockopen($host, $port);
        if (!is_resource($fd)) {
            return "fsockopen failed";
        }
        fwrite($fd, $httpheader);
        stream_set_blocking($fd, TRUE);
        stream_set_timeout($fd, $timeout);
        $info = stream_get_meta_data($fd);
        $gets = "";
        while ((!feof($fd)) && (!$info['timed_out'])) {
            $data .= fgets($fd, 8192);
            $info = stream_get_meta_data($fd);
            @ob_flush();
            flush();
        }
        if ($info['timed_out']) {
            return "timeout";
        } else {
            //echo $data;
            $contentInfo = explode(" ", str_replace(" ", "", $data));
            
            if (!strstr($contentInfo[0], "HTTP/1.1 200 OK")) {
                return -10;
            }
            return trim($contentInfo[1]);
        }
    }

  • 相关阅读:
    工商银行:应用多k8s集群管理及容灾实践
    鸿蒙轻内核源码分析:掌握信号量使用差异
    云图说|ASM灰度发布,让服务发布变得更敏捷、更安全
    解读多云跨云下的容器治理与实践
    互斥锁Mutex:鸿蒙轻内核中处理临界资源独占的“法官”
    带你深入理解Java的IO到底是个啥
    毕业季offer怎么拿?收下这份非典型求职面试指南
    CKEditor 5 摸爬滚打(一)—— 从零构建定制化工程项目
    说起来你可能不信,一个正则就能让页面卡死
    sklearn中的pipeline的创建与访问
  • 原文地址:https://www.cnblogs.com/sooj/p/3217898.html
Copyright © 2011-2022 走看看