PHP code example of p3k / multipart

1. Go to this page and download the library: Download p3k/multipart library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

p3k / multipart example snippets


// Using composer...
directly
ject
$multipart = new p3k\Multipart();

// Example POST data
$params = [
  'category' => [
    'one',
    'two'
  ],
  'name' => 'test',
  'nested' => [
    'foo' => [
      'bar1',
      'bar2'
    ],
    'bar' => 'foo'
  ]
];

// Add the params to the request
$multipart->addArray($params);

// You can add files too!
$multipart->addFile('photo', '/tmp/example.jpg', 'image/jpeg');

// Set up curl
$ch = curl_init('http://localhost:8000/server.php');
curl_setopt($ch, CURLOPT_POST, true);

// Set the POSTFIELDS to the result of this object
curl_setopt($ch, CURLOPT_POSTFIELDS, $multipart->data());

// You'll also need to set the Content-Type header
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Content-type: ' . $multipart->contentType()
));

curl_exec($ch);

$params = array(
  'category' => [
    'one',
    'two'
  ]
);

$ch = curl_init('http://localhost:8000/server.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_exec($ch);

cd servers
php -S localhost:8000