PHP code example of juanchosl / httpdata

1. Go to this page and download the library: Download juanchosl/httpdata 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/ */

    

juanchosl / httpdata example snippets


use JuanchoSL\HttpData\Bodies\Creators\UrlencodedCreator;

echo (new UrlencodedCreator)->appendData([
    'form' => [
        'name' => "nombre",
        'surname' => "apellidos"
    ]
]);

#> form%5Bname%5D=nombre&form%5Bsurname%5D=apellidos

use JuanchoSL\HttpData\Bodies\Creators\MultipartCreator;

echo (new MultipartCreator(md5(uniqid())))->appendData([
    'form' => [
        'name' => "nombre",
        'surname' => "apellidos"
    ]
]);

/*
--47608c536770a84a371e08a0ffd92e95
Content-Disposition: form-data; name="form[name]"

nombre
--47608c536770a84a371e08a0ffd92e95
Content-Disposition: form-data; name="form[surname]"

apellidos
--47608c536770a84a371e08a0ffd92e95--
*/

use JuanchoSL\HttpData\Bodies\Creators\MultipartCreator;

$file = realpath('../../file.txt');
$data = [
    'form' => [
        'name' => "nombre",
        'surname' => "apellidos"
    ],
    'file' => [
        new CURLStringFile(file_get_contents($file), basename($file), 'text/plain'),
        new CURLFile($file, 'text/plain', basename($file)),
        '@' . $file
    ]
];
echo (new MultipartCreator(md5(uniqid())))->appendData($data);
/*
--b660ef4adf655b4043ab99cd21c6fa92
Content-Disposition: form-data; name="form[name]"

nombre
--b660ef4adf655b4043ab99cd21c6fa92
Content-Disposition: form-data; name="form[surname]"

apellidos
--b660ef4adf655b4043ab99cd21c6fa92
Content-Disposition: form-data; name="file[]"; filename="file.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary
Content-Length: 95

esto es un texto de ejemplo desde un fichero
--b660ef4adf655b4043ab99cd21c6fa92
Content-Disposition: form-data; name="file[]"; filename="file.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary
Content-Length: 95

esto es un texto de ejemplo desde un fichero
--b660ef4adf655b4043ab99cd21c6fa92
Content-Disposition: form-data; name="file[]"; filename="file.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary
Content-Length: 95

esto es un texto de ejemplo desde un fichero
--b660ef4adf655b4043ab99cd21c6fa92--
*/

use JuanchoSL\HttpData\Bodies\Parsers\MultipartReader;

$str = <<< 'EOH'
--47608c536770a84a371e08a0ffd92e95
Content-Disposition: form-data; name="form[name]"

nombre
--47608c536770a84a371e08a0ffd92e95
Content-Disposition: form-data; name="form[surname]"

apellidos
--47608c536770a84a371e08a0ffd92e95--
EOH;

//$body = (new StreamFactory)->createStream($str);
$body = (string) $server_request->getBody();
$body_parsed = (new MultipartReader($body))->getBodyParams();
echo "<pre>" . print_r($body_parsed, true);

<pre>Array
(
    [form] => Array
        (
            [name] => nombre
            [surname] => apellidos
        )

)

use JuanchoSL\HttpData\Bodies\Parsers\MultipartReader;

$str = <<< 'EOH'
--b660ef4adf655b4043ab99cd21c6fa92
Content-Disposition: form-data; name="form[name]"

nombre
--b660ef4adf655b4043ab99cd21c6fa92
Content-Disposition: form-data; name="form[surname]"

apellidos
--b660ef4adf655b4043ab99cd21c6fa92
Content-Disposition: form-data; name="file[]"; filename="file.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary
Content-Length: 95

esto es un texto de ejemplo desde un fichero
--b660ef4adf655b4043ab99cd21c6fa92
Content-Disposition: form-data; name="file[]"; filename="file.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary
Content-Length: 95

esto es un texto de ejemplo desde un fichero
--b660ef4adf655b4043ab99cd21c6fa92
Content-Disposition: form-data; name="file[]"; filename="file.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary
Content-Length: 95

esto es un texto de ejemplo desde un fichero
--b660ef4adf655b4043ab99cd21c6fa92--
EOH;

//$body = (new StreamFactory)->createStream($str);
$body = (string) $server_request->getBody();
$body_parsed = (new MultipartReader($body))->getBodyFiles();
echo "<pre>" . print_r($body_parsed, true);
<pre>Array
(
    [file] => Array
        (
            [tmp_name] => Array
                (
                    [0] => C:\Users\juan\AppData\Local\Temp\php474B.tmp
                    [1] => C:\Users\juan\AppData\Local\Temp\php474C.tmp
                    [2] => C:\Users\juan\AppData\Local\Temp\php474D.tmp
                )

            [size] => Array
                (
                    [0] => 46
                    [1] => 46
                    [2] => 46
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                )

            [name] => Array
                (
                    [0] => file.txt
                    [1] => file.txt
                    [2] => file.txt
                )

            [type] => Array
                (
                    [0] => text/plain
                    [1] => text/plain
                    [2] => text/plain
                )

        )
)

[$_POST, $_FILES] = (new MultipartReader($body))->getBodyParts();

(new MultipartReader($body))->toGlobals();