The normal curl
way, works well.
curl
-F "[email protected]"
-H "Content-Type: multipart/form-data"
https://sm.ms/api/v2/upload
But in my PHP version curl, it returns bool(false)
and string(0) ""
:
<?php
$url = "https://sm.ms/api/v2/upload";
$headers = array();
array_push($headers, "Content-Type: multipart/form-data");
array_push($headers, "User-Agent: ".$_SERVER['HTTP_USER_AGENT']);
// $fields = array('smfile' => curl_file_create('test.png', 'image/png', 'test.png'));
$fields = array('smfile' => new CURLFile('test.png', 'image/png', 'tset.png'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
var_dump(curl_exec($ch));
var_dump(curl_error($ch));
What’s wrong with my code? ヽ(*。>Д<)o゜
2
Answers
you can use this simple code to upload files
Try this one: