[英]Convert curl command to Guzzle psr7 http
我需要將此cURL
命令轉換為 php:
curl -X POST https://google.com \
-H 'custom_id: 1234' \
--form 'file=@"/Desktop/image.jpg"' \
--form 'options_json="{\"rm_spaces\": true}"'
我嘗試過這樣的事情:
<?php
use Psr\Http\Client\ClientInterface;
use GuzzleHttp\Psr7\Utils;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
final class CurlCommand
{
private RequestFactoryInterface $requestFactory;
private ClientInterface $httpClient;
private StreamFactoryInterface $streamFactory;
private UriFactoryInterface $uriFactory;
public function curl(): void
{
$createUri = $this->uriFactory->createUri('https://google.com');
$jsonData = [
"multipart" => [
[
'name' => 'image.jpg',
'contents' => Utils::tryFopen('/Desktop/image.jpg', 'r')
],
]
];
$request = $this->requestFactory->createRequest('POST', $createUri)
->withHeader('custom_id', '1234')
->withBody($this->streamFactory->createStream(json_encode($jsonData)));
$response = $this->httpClient->sendRequest($request);
}
}
但是該文件沒有作為form-data
附加我正在為 psr7 使用 guzzle。
在此先感謝您的幫助,我在 guzzle 文檔中找不到任何信息。 因為如您所見,我正在研究接口。
用這個
Laravel 8 You May Easy Call Multipart Api for image uploading directly using use GuzzleHttp\Client;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Utils;
use File;
$filename = $req->file('file1')->getClientOriginalName();
$getfilePath = $req->file('file1')->getRealPath();
$client = new Client();
$response = $client->request('POST', 'http://127.0.0.1:8045/api/uploadImages', [
'multipart' => [
[
'name' => 'image',
'contents' => fopen($getfilePath, 'r')
],
// 'headers' => [
// 'Content-Type' => '<Content-type header>'
// ]
]
]);
echo $response->getStatusCode();
$bodyresponcs = $response->getBody();
$result = json_decode($bodyresponcs);
print_r($result->status);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.