HttpRequestService::do_post_request PHP Method

do_post_request() public method

POST wrapper,不基于curl函数,环境可以不支持curl函数
public do_post_request ( $url, $postdata, $files ) : mixed
return mixed
    public function do_post_request($url, $postdata, $files)
    {
        $data = '';
        $boundary = '---------------------' . substr(md5(rand(0, 32000)), 0, 10);
        //Collect Postdata
        foreach ($postdata as $key => $val) {
            $data .= "--{$boundary}\r\n";
            $data .= 'Content-Disposition: form-data; name="' . $key . "\"\r\n\r\n" . $val . "\r\n";
        }
        $data .= "--{$boundary}\r\n";
        //Collect Filedata
        foreach ($files as $key => $file) {
            $fileContents = file_get_contents($file['tmp_name']);
            $data .= "Content-Disposition: form-data; name=\"{$key}\"; filename=\"{$file['name']}\"\r\n";
            $data .= 'Content-Type: ' . $file['type'] . "\r\n";
            $data .= "Content-Transfer-Encoding: binary\r\n\r\n";
            $data .= $fileContents . "\r\n";
            $data .= "--{$boundary}--\r\n";
        }
        $params = array('http' => array('method' => 'POST', 'header' => 'Content-Type: multipart/form-data; boundary=' . $boundary, 'content' => $data));
        $ctx = stream_context_create($params);
        $fp = fopen($url, 'rb', false, $ctx);
        if (!$fp) {
            die('can not open server!');
        }
        $response = @stream_get_contents($fp);
        if ($response === false) {
            die('can not get message form server!');
            //throw new Exception("Problem reading data from {$url}, {$php_errormsg}");
        }
        return $response;
    }