Как отправить файл через pecl_http? - PullRequest
0 голосов
/ 12 декабря 2018

Я пишу API Client, но не могу отправить файлы через pecl_http.Я написал все на http \ Client.Большинство вещей копируются почтальоном, но при отправке я получаю нулевые файлы.Как я должен отправить это?как мне поместить в этот скрипт мою переменную $ _FILES с данными?

<?php

$client = new http\Client;
$request = new http\Client\Request;

$body = new http\Message\Body;
$body->addForm(NULL, array(
  array(
    'name' => 'photo',
    'type' => null,
    'file' => 'user_path/2018-11-09 o 15.00.48.png',
    'data' => null
  )
));

$request->setRequestUrl('url');
$request->setRequestMethod('POST');
$request->setBody($body);

$request->setHeaders(array(
  'Postman-Token' => 'f6154fff-46f4-47d0-a7c3-98d7de8d0f24',
  'Cache-Control' => 'no-cache',
  'Content-Type' => 'application/x-www-form-urlencoded'
));

$client->enqueue($request)->send();
$response = $client->getResponse();

echo $response->getBody();

1 Ответ

0 голосов
/ 12 декабря 2018

Вот часть кода, который я нашел в: Учебник Pecl_http

<?php
$r = new HttpRequest('http://dev.iworks.at/.print_request.php', HTTP_METH_POST);

// if redirects is set to true, a single redirect is allowed;
// one can set any reasonable count of allowed redirects
$r->setOptions(
    array(  'cookies'   => array('MyCookie' => 'has a value'),
            'redirect'  => true,
    )
);

// common form data
$r->setPostFields(
    array(  'name'  => 'Mike',
            'mail'  => 'mike@php.net',
    )
);
// add the file to post (form name, file name, file type)
touch('profile.jpg');
$r->addPostFile('image', 'profile.jpg', 'image/jpeg');

try {
    print $r->send()->getBody();
} catch (HttpException $e) {
    print $e;
}
?>
...