• 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]);
        }
    }

  • 相关阅读:
    linux高级编程day14 笔记
    [转]ubuntu下gcc的安装与使用
    MySQL主从复制(Replication for Backup)
    Zabbix(2,添加主机,监控项)
    jumpserver for C7一步一步安装
    MySQL:创建、修改和删除表
    NOT EXISTS替代NOT IN 和 EXISTS替换DISTINCT 识别‘低效执行’的SQL语句
    ie6、ie7下JSON.parse JSON未定义的解决方法
    CentOS6.4编译源码安装mysql5.0.41
    获取客户端IP并判断内外网
  • 原文地址:https://www.cnblogs.com/sooj/p/3217898.html
Copyright © 2020-2023  润新知