F
F
Forever Extreme2021-06-09 17:13:32
PHP
Forever Extreme, 2021-06-09 17:13:32

How to use CURLFile in Guzzle?

Hello! I have a question. I am using vk api. I need to send a photo to the server. When writing via curl, I was able to do this, however when I tried to use Guzzle, nothing was sent.

$upload_url = 'www.example.com'; //  это сервер на который нужно постом отправить фото

$image_path = dirname(__FILE__).'/temp.jpg';
$cfile = curl_file_create($pic,'image/jpeg','temp.jpg');
$ch = curl_init($upload_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => $cfile));
$result = json_decode(curl_exec($ch), true);
curl_close($ch);

in this case everything works. But if I use Guzzle, it gives an error that the extension is not suitable, but it loads it through curl. Tried like this:
$pic = 'path/to/image/';
$arr = [
    'contents' => Psr7\Utils::tryFopen($pic, 'r'),
    'name' => 'temp.jpg', // название файла
];
$arr1['file'] = $arr;
$upload_url = $response['response']['upload_url']; // адрес сервера на который нужно отправить
$data1 = $client->request('POST', $upload_url, $arr)->getBody();
$response = json_decode($data1->getContents(), true);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question