【问题标题】:Convert cURL to Guzzle POST将 cURL 转换为 Guzzle POST
【发布时间】:2016-03-21 14:44:28
【问题描述】:

我正在调整一个将图像上传到 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 错误请求;我的代码有什么问题?

【问题讨论】:

    标签: php curl guzzle


    【解决方案1】:

    乍一看,我发现了两个问题:

    1. Authorization 标头。在您的 Guzzle 版本中,您使用 Authorization: Client-ID 作为 标题名称 和 $client_id 作为标题值。这将生成一个(错误的)HTTP 标头,如下所示:

      Authorization: Client-ID: myclientid
      

      解决方案:像这样传递你的标题:

      "headers" => [
          "authorization" => "Client-ID " . $clientId
      ]
      
    2. 请求正文。您原来的基于 cURL 的版本包含一个带有 image 参数的 URL 查询编码正文。该参数包含图像文件的 base64 编码原始内容。在您的 Guzzle 版本中,您实际上根本不发送正文,因为您使用的是不存在的 image 选项(请查看 Guzzle documentation 以获取所有支持的列表选项)。此外,您的原始示例不包含 data:image/png;base64, 前缀(通常只是浏览器的提示)。

      尝试如下传递参数:

      "form_params" => [
          "image" => base64_encode(/* image content here */)
      ]
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-08
      • 2013-10-30
      • 2019-06-30
      • 1970-01-01
      • 2015-11-02
      • 2021-09-08
      • 2021-02-19
      • 2017-01-13
      相关资源
      最近更新 更多