- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在调整一个将图像上传到 imgur 的示例。该示例使用 curl,我使用的是 guzzle ^6.1。 curl 的例子是:
<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);
我尝试使用下一个代码转换为 Guzzle:
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 错误的请求;我的代码有什么问题?
最佳答案
乍一看,我发现了两个问题:
Authorization
header 。在您的 Guzzle 版本中,您使用 Authorization: Client-ID
作为 header name 并使用 $client_id
作为 header 值。这将生成一个(错误的)HTTP header ,如下所示:
Authorization: Client-ID: myclientid
解决方案:像这样传递 header :
"headers" => [
"authorization" => "Client-ID " . $clientId
]
请求正文。您最初的基于 cURL 的版本包含一个带有 image
参数的 URL 查询编码正文。该参数包含图像文件的 base64 编码原始内容。在您的 Guzzle 版本中,您实际上根本不发送正文,因为您使用的是不存在的 image
选项(查看 Guzzle documentation所有支持选项的列表)。此外,您的原始示例不包含 data:image/png;base64,
前缀(这通常只是浏览器的提示)。
尝试按如下方式传递参数:
"form_params" => [
"image" => base64_encode(/* image content here */)
]
关于php - 将 cURL 转换为 Guzzle POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34295502/
当我运行以下代码(使用最新的 Guzzle v6)时,请求的 URL 是 http://example.com/foobar?foo=bar删除 boo=far从请求。 $guzzle_http_cl
我正在使用 Guzzle 并发请求工具: http://docs.guzzlephp.org/en/latest/quickstart.html#concurrent-requests 我的代码类似于
我需要将 header 类型添加到下面的 guzzle 请求中,但无法弄清楚如何在不出错的情况下添加它这是我要添加的内容: $command->set('command.headers', array
我正在使用 lumen 尝试通过 guzzle 设置简单的 api 请求。 问题是 base_uri 参数似乎没有在初始 new Client() 上正确传递. 简化示例: use GuzzleHtt
我正在使用 Guzzle 6。 我正在尝试模拟客户端并像这样使用它: 'application/json'], 'foo'), ]); $history = []; $history_middlew
我正在尝试使用 Guzzle 而不是直接使用 cURL 来实现和 HTTP 请求。我如何使用 Guzzle 发出相同类型的请求?或者我应该坚持使用cURL? $ch = curl_init(); //
Guzzle 的 API 文档在哪里? 有这两个链接(看起来是同一件事): http://guzzle.readthedocs.org/en/latest/quickstart.html http:/
我有一个页面使用 Guzzle 6 执行一些 REST 查询。它工作正常,但有时速度太慢,因为它总是进行查询。我发现有 guzzle-cache-middleware应该缓存来自远程 API 的响应。
在 Symfony 4 项目中,我使用 eightpoints/guzzle 包来调用远程 API。安装 bundle (并在单独的 yaml 文件中配置客户端)后,我关注了 this quide让我
您可以使用 Guzzle 的 Pool:batch() 方法并行执行 http 请求。它允许您使用第三个参数中的 options 键为请求设置默认选项。 但是如果我需要为池中的不同请求提供不同的选项怎
我正在尝试使用 Guzzle 5 连接到 WooCommerce API(Guzzle 6 似乎没有 oAuth 选项 o.O)。 Woocommerce requires the oAuth aut
我在 laravel 项目中遇到问题。 它显示了这个错误 Fatal Error : Class 'Guzzle\Http\Client' not found 我已安装 Guzzle 并将其添加到配置
Guzzle/Http的新手。 我有一个API rest url登录名,如果未授权,则用401代码回答;如果缺少值,则用400回答。 我将获取http状态代码以检查是否存在某些问题,但不能仅包含代码(
A. 总结 顾名思义,Guzzle 允许一次发送多个请求以节省时间,如 documentation . $responses = $client->send(array( $requestOb
我正在使用 PayPal API 模拟负面响应,以便在客户通过 onApprove 方法批准付款时获得响应并正确处理付款的关键部分。 我正在使用 GuzzleHttp + Laravel 来捕获 客户
我一直在尝试从工作中获取 GET 请求,但不断收到 500 错误。 我不知道发生了什么。 如果我执行以下操作,它可以正常工作: 'http://www.google.com', ]); $respo
我如何通过 guzzle 服务客户端获取发布数据 getCommand功能? 我的 json 如下所示: "createMessage": { "httpMethod": "P
之间有什么区别? GuzzleHttp ReactPHP Amphp 它们之间有何不同?与典型的用例一起使用是什么? 最佳答案 它们之间的主要区别是Guzzle是HTTP客户端,而Amp和ReactP
我不确定为什么会收到此错误: php composer.phar require guzzle/guzzle:~3.7 错误 : ./composer.json has been update
我想使用Guzzle和Silex将请求发送到https页面。 使用http url我有一个回应: app->get('/',function() use ($app, $client){ $r
我是一名优秀的程序员,十分优秀!