3
我正在修改一个将图像上传到imgur的示例。该示例使用卷曲,我正在使用guzzle^6.1。与卷曲的例子是:将cURL转换为Guzzle POST
<html>
<h3>Form</h3>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="50000" />
Image (< 50kb): <input type="file" name="upload" /><br/>
ClientID: <input type="text" name="clientid" /><br/>
<input type="submit" value="Upload to Imgur" />
</form>
</html>
<?php
if (empty($_POST['clientid']) || @$_FILES['upload']['error'] !== 0 || @$_FILES['upload']['size'] > 50000) {
exit;
}
$client_id = $_POST['clientid'];
$filetype = explode('/',mime_content_type($_FILES['upload']['tmp_name']));
if ($filetype[0] !== 'image') {
die('Invalid image type');
}
$image = file_get_contents($_FILES['upload']['tmp_name']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => base64_encode($image)));
$reply = curl_exec($ch);
curl_close($ch);
$reply = json_decode($reply);
echo "<h3>Image</h3>";
printf('<img height="180" src="%s" >', @$reply->data->link);
echo "<h3>API Debug</h3><pre>";
var_dump($reply);
我尝试转换使用下面的代码狂饮:
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request as gRequest;
//....Clases and functions ...
$url = "https://api.imgur.com/3/image.json";
$client_id = "miclientid";
$client = new Client([
// Base URI is used with relative requests
'base_uri' => $url,
// You can set any number of default request options.
'timeout' => 15.0,
]);
$gRequest = new gRequest('POST', 'https://api.imgur.com/3/image.json', [
'headers' => [
'Authorization: Client-ID' => $client_id
],
'image' => "data:image/png;base64,iVBORw0K..."
]);
$gResponse = $client->send($gRequest, ['timeout' => 2]);
但是我收到一个400错误的请求;我的代码有什么问题?