Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.3 kB
1
Indexable
Never
 public static function post_multiple_files_ekyc($url, $files, $headers = [], $configs = [])
    {

        foreach ($files as $ind => $file) {
            foreach ($configs as $config) {
                if ($config['key'] == $file['name']) {
                    $extensions = explode('.', $config['value']);
                    if ($extensions && count($extensions) > 0) {
                        $files[$ind]['ext'] = $extensions[count($extensions) - 1];
                    } else {
                        $files[$ind]['ext'] = '';
                    }
                }
            }
        }

        $eol = "\r\n"; //default line-break for mime type
        $BOUNDARY = uniqid(); //random boundaryid, is a separator for each param on my post curl function
        $delimiter = '-------------' . $BOUNDARY;
        $BODY = ""; //init my curl body

        foreach ($files as $ind => $file) {
            if (isset($file['data'])) {
                $BODY .= "--" . $delimiter . $eol
                    . 'Content-Disposition: form-data; name="' . $file['name'] . '"; filename="' . StringUtils::random_string(32)  . time() . '.' . $files[$ind]['ext']  . '"' . $eol
                    . 'Content-Type: image/' . $files[$ind]['ext'] . $eol
                    . 'Content-Transfer-Encoding: binary' . $eol;

                $BODY .= $eol;
                $BODY .= $file['data'] . $eol;
            }
        }
        $BODY .= "--" . $delimiter . "--" . $eol;
        $ch = curl_init(); //init curl
        $arrHeader = [];
        foreach ($headers as $key => $header) {
            $arrHeader[] = "$key: $header";
        } 
        $arrHeader[] = "Content-Type:multipart/form-data; boundary=$delimiter"; 

        curl_setopt($ch, CURLOPT_HTTPHEADER, $arrHeader);
        curl_setopt($ch, CURLOPT_URL, $url); //setting our api post url
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // call return content
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 3);
        curl_setopt($ch, CURLOPT_POST, true); //set as post
        curl_setopt($ch, CURLOPT_POSTFIELDS, $BODY); // set our $BODY

        $response = curl_exec($ch); // start curl navigation
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        return $response;
    }