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

DDSameera's avatar

How to convert this CURL code into Guzzle Code

Guzzle - https://docs.guzzlephp.org/en/stable/


         'https://api.dyntube.com/v1/videos',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'GET',
        CURLOPT_HTTPHEADER => array(
        'Authorization: Bearer {{token}}'
        ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        echo $response;
0 likes
4 replies
DDSameera's avatar

First issue fixed. then i want to upload file to 3rd party server using Guzzle.

Orginal Curl code



        <?php

        if (isset($_POST['btnUpload']))
        {
        $message='';

        $url = "https://upload.dyntube.com/v1/videos";
        $filename = $_FILES['file']['name'];
        $filedata = $_FILES['file']['tmp_name'];
        $filesize = $_FILES['file']['size'];
        $filetype = $_FILES['file']['type'];

        if ($filedata != '')
        {
        $headers = array(
        'Content-type: multipart/form-data',
        'Authorization: Bearer {{token}}',
        );


        $postfields = array("projectId" => "{{projectId}}",'file' => new CURLFile(realpath($filedata),$filetype,$filename));

        $ch = curl_init();
        $options = array(
        CURLOPT_URL => $url,
        CURLOPT_HEADER => true,
        CURLOPT_POST => 1,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_POSTFIELDS => $postfields,
        CURLOPT_INFILESIZE => $filesize,
        CURLOPT_RETURNTRANSFER => true
        ); // cURL options
        curl_setopt_array($ch, $options);
        $message= curl_exec($ch);
        if(!curl_errno($ch))
        {
        $info = curl_getinfo($ch);
        if ($info['http_code'] == 200)
        $message = "File uploaded successfully";
        }
        else
        {
        $message = curl_error($ch);
        }
        curl_close($ch);
        }
        else
        {
        $message = "Please select the file";
        }

        echo $message;
        }

        ?>

   


Error Message

{message: "A 'contents' key is required", exception: "InvalidArgumentException",…}
exception: "InvalidArgumentException"
file: "C:\wamp64\www\lms\vendor\guzzlehttp\psr7\src\MultipartStream.php"
line: 86
message: "A 'contents' key is required"
trace: [{file: "C:\wamp64\www\lms\vendor\guzzlehttp\psr7\src\MultipartStream.php", line: 73,…},…]

Guzzle Code




        $token = "eyJhbGciOiJIUz"

        $filename = $_FILES['file']['name'];
        $filedata = $_FILES['file']['tmp_name'];
        $filesize = $_FILES['file']['size'];
        $filetype = $_FILES['file']['type'];

        if ($filedata != '') {
            $headers = array(
                'Content-type: multipart/form-data',
                'Authorization: Bearer ' . $token,
            );

            $postfields = array(
                "projectId" => "TSYRQJGuLrECoVwNErgjlQ",
                'file' => new \CURLFile(realpath($filedata), $filetype, $filename)
            );



        } else {
            $message = "Please select the file";
        }


        $file = "https://picsum.photos/id/237/200/300";

        $client = new Client();
        $client->request('POST', '/post', [
            'multipart' => [

                [
                    'name' => 'upload',
                    'contents' =>fopen($file,'r'),
                    'filename' => 'fl'
                ],
                [
                    'name' => 'Authorization',
                    'content' => 'Bearer ' . $token,
                ],
                [
                    'name' => 'projectId',
                    'contents' => "TYRQJGuLrECoVwNErgjlQ"
                ],
            ]
        ]);



    }

apex1's avatar

You have a typo : 'content' should be 'contents'

Please or to participate in this conversation.