微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP Tcp请求第三方接口

function socketApi()
    {
        error_reporting(E_ALL);
        set_time_limit(0);

        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if ($socket === false) {
            $error_code = socket_last_error();
            $error_msg = socket_strerror($error_code);
            die("Couldn't create socket: [$error_code] $error_msg");
        }

        $result = socket_connect($socket, $this->ip, $this->port);
        if (!$result) {
            $this->errorStr = "socket_connect() Failed.\nReason: ($result) " . socket_strerror($result) . "\n";
            return $this->errorStr;
        }

        $in = $this->bodyTit . $this->bodyXml;
        if (!socket_write($socket, $in, strlen($in))) {
            $this->errorStr = "socket_write() Failed: reason: " . socket_strerror($socket) . "\n";
            return $this->errorStr;
        }

        while ($out = @socket_read($socket, 8192)) {
            $this->outTradingType = substr($out, 87, 6);
            $this->outSubject = trim(substr($out, 93, 100));
            $this->outBodyTit = substr($out, 0, 222);
            if ($this->outTradingType == '000000') {
                $this->outBodyXml = substr($out, 222);
                $this->outXmlArr = self::xmlToArray($this->outBodyXml);

            } else {
                $this->errCode = $this->outTradingType;
                $this->errStr = $this->outSubject;
                return $this->errStr;
            }
        }

        socket_close($socket);
        return $this->errStr;
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐