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

bionary's avatar

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

0 likes
9 replies
bionary's avatar

@amitsolanki24_ Sure thing, but just for context I tried solving this same exact problem approximately 3 years ago. In that timespan wordpress has not changed or improved their API docs regarding Media Uploads. It's amazing that such a garbage pos CMS has gotten so far with widespread adoption. I own many wordpress sites out of necessity, but having worked on the dev side of things I cannot believe what a steaming pile of crap wordpress is for development. At this point wordpress functions as about efficiently and transparently as your typical and always pathetic government/bureaucratic run entity.

jbloomstrom's avatar

You can use a debugging proxy tool like Fiddler on your dev box to inspect the raw request + headers.

bionary's avatar

@jbloomstrom I almost fell out of my chair when I saw the pricing for "Fiddler". I thought maybe I was on an airline site by accident, and was looking at round trip flights.

gych's avatar

Can you try this?

$response = Http::withHeaders([
    'Content-Disposition' => 'attachment; filename=".$filename." ',
    'Authorization' => 'Basic ' . base64_encode($this->username . ':' . $this->password),
	'Content-Type': 'image/jpeg'
])->post($this->baseUrl . "/wp/v2/media", $file);
bionary's avatar

@gych I've tried that...and I tried it again just to be sure. Doesn't work.

bionary's avatar

@gych Yes. That code you provided was one of the combinations I have tried. It leads to an encoding error. Then once I provide ->asForm() things are encoded properly . Then the next set of errors shows up starting with the 2nd param of ->post() which needs to be an array. So of course I've tried using things like ['attachment'=>$file] and tons more combinations. At best the post() does post, but no image is uploaded. When browsing the media files in WordPress, the images are blank.

Unless you have explicitly worked with wordpress doing this exact thing: Creating Media via their API, best guesses won't help. I have spent days trying combinations. The only thing that has worked is that Curl script I posted originally.

Please or to participate in this conversation.