简体   繁体   English

将cURL转换为Guzzle POST

[英]Convert cURL to Guzzle POST

I'm adapting an example for upload an image to imgur. 我正在调整一个例子来上传一个图像到imgur。 The example uses curl and I'm using guzzle ^6.1. 该示例使用curl,我使用guzzle ^ 6.1。 The example with curl is: 卷曲的例子是:

<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);

I'm try to convert to Guzzle using the next code: 我尝试使用下一个代码转换为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]);   

But I receive a 400 bad request; 但我收到了400个不好的请求; What is wrong in my code? 我的代码有什么问题?

On first glance, I see two issues: 乍一看,我看到两个问题:

  1. The Authorization header. Authorization标头。 In your Guzzle version, you use Authorization: Client-ID as header name and $client_id as header value. 在Guzzle版本中,您使用Authorization: Client-ID作为标题名称$client_id作为标题值。 This will generate a (wrong) HTTP header that looks like this: 这将生成一个(错误的)HTTP标头,如下所示:

     Authorization: Client-ID: myclientid 

    Solution: Pass your headers like this: 解决方案:像这样传递你的标题:

     "headers" => [ "authorization" => "Client-ID " . $clientId ] 
  2. The request body. 请求正文。 Your original, cURL-based version contained a URL query-encoded body with an image parameter. 您原始的基于cURL的版本包含带有image参数的URL查询编码正文。 That parameter contained the base64 encoded raw contents of the image file. 该参数包含图像文件的base64编码原始内容。 In your Guzzle version, you actually do not send a body at all , as you're using the non-existent image option (have a look at the Guzzle documentation for a list of all supported options). 在Guzzle版本中,您实际上根本不发送正文 ,因为您正在使用不存在的image选项(请查看Guzzle文档以获取所有支持选项的列表)。 Also, your original example did not contain the data:image/png;base64, prefix (which is usually just a hint for the browser). 此外,您的原始示例不包含data:image/png;base64, prefix(通常只是浏览器的提示)。

    Try passing the parameters as follows: 尝试传递参数如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM