@bionary Hey i have stucked in same issue when you will get solution please tag me. Thanks
Mar 14, 2024
9
Level 6
Help Converting Curl to HTTP
I have a Curl script that uploads a file successfully to a wordpress site. I want to convert this Curl script to Laravel's convenient HTTP (guzzle) format. Can't seem to get it to work. Here's the two scripts:
$filename = 'horizontal sample.jpg';
$file = Storage::disk('shared')->get('/samples/'.$filename);
//This Works
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $this->baseUrl . "/wp/v2/media" );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $file );
curl_setopt( $ch, CURLOPT_HTTPHEADER, [
'Content-Disposition: form-data; filename="'.$filename.'"',
'Authorization: Basic ' . base64_encode( $this->username . ':' . $this->password ),
] );
$result = curl_exec( $ch );
curl_close( $ch );
dd( json_decode( $result ) );
//This Does Not Work
$response = Http
::withBasicAuth($this->username, $this->password)
->asForm()
->asMultipart()
->withHeaders(['Content-Disposition' => 'form-data; filename="'.$filename.'"',])
->attach('my-file', $file, $filename)
->post($this->baseUrl . "/wp/v2/media");
dd(collect($response->json()));
I wish there was a way to easily print out the raw info + headers that are being sent...that way I can simply compare both methods and see what is going on, but there is not a straightforward way as far as I know.
Anybody have any advice? thanks
Please or to participate in this conversation.