Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

samalapsy's avatar

PHP Quivalent of this JS Code

<script>
            function parse(){
                document.getElementById("text").innerText = 'processing audio';
                let file =  document.getElementById("clip").files[0];
                let reader = new FileReader();
               
                reader.onload = (evt) => {
                    let contents = evt.target.result;

                    let request = new XMLHttpRequest();
                    request.open("POST", "http://example.com/treat");

                    request.setRequestHeader('Content-Type', file.type === 'video/webm' ? 'audio/webm': file.type);
                    
                    request.onload = (event) => {
                        if(event.currentTarget.status == 200 && event.currentTarget.readyState == 4){
                            document.getElementById("text").innerText = `The Parsed Audio is: ${JSON.parse(request.response).text}`
                        }else{
                            document.getElementById("text").innerText = "Processing failed"
                        }
                    }

                    request.send(contents);
                }

                reader.readAsArrayBuffer(file);
            }
        </script>

Please how can I create a replica of this in PHP..... This code accepts and audio file and streams it to the supplied URL. The Url now send a transcription of that Audio file.

Thank You

0 likes
7 replies
burlresearch's avatar

If you install guzzle,

composer require guzzlehttp/guzzle

Then like:

        $content_type = $file_type === 'video/webm' ? 'audio/webm': $file_type;

        $client = new \GuzzleHttp\Client([
            'base_uri' => 'http://example.com/',
        ]);

        $response = $client->request('POST', 'treat', [
            'headers' => ['Content-Type' => $content_type],
        ]);

        $code = $response->getStatusCode(); // 200
        // [...]
samalapsy's avatar

@martinbean, Very true I've been trying t write this same script in PHP but I don't seems to get it right, that's why I'm calling for help on this platform..

Please or to participate in this conversation.