Home > database >  How do I convert the CURL command to Guzzle HTTP Laravel
How do I convert the CURL command to Guzzle HTTP Laravel

Time:10-30

Do you have any idea how to convert this curl code that I want to convert to guzzle http?

curl -X POST "https://api.cloudflare.com/client/v4/accounts/023e105f4ecef8ad9ca31a8372d0c353/stream" \
     -H "X-Auth-Email: [email protected]" \
     -H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
     --form 'file=@/Users/kyle/Desktop/skiing.mp4'

My guzzle code

       $token = config('services.cloudflare.token');
        $accountId = config('services.cloudflare.acountId');
        $client = new Client();

        $response =$client->request('POST', 'https://api.cloudflare.com/client/v4/accounts/' . $accountId . '/stream', [
            'headers' => [
                'Authorization' => 'Bearer ' . $token,
                "Content-Type" => "application/json",

            ],
            'multipart' => [
                [
                    'name' => 'upload',
                    'filename' => 'fl',
                    'contents' => fopen($file,'r '),
                    'name' => 'new-video.' . $file->getClientOriginalExtension(),
                ],

            ]
        ]);
        echo $response->getBody();

CodePudding user response:

Laravel and Cloudflare, two of my least favorite things.
Just use straight PHP

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/accounts/023e105f4ecef8ad9ca31a8372d0c353/stream');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'X-Auth-Email' => '[email protected]',
    'X-Auth-Key' => 'c2547eb745079dac9320b638f5e225cf483cc5cfdda41',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
    'file' => new CURLFile('/Users/kyle/Desktop/skiing.mp4'),
]);

$response = curl_exec($ch);
  • Related