PHP code example of bmatovu / laravel-xml

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

    

bmatovu / laravel-xml example snippets


$request->xml();

$request->sentXml();

$request->wantsXml();

$isValid = Xml::is_valid($request->xml());

if (! $isValid) {
    return response()->xml(['message' => 'The given data was malformed.'], 400);
}

$errors = Xml::validate($request->xml(), 'path_to/sample.xsd');

if ($errors) {
    return response()->xml([
        'message' => 'The given data was invalid.',
        'errors' => $errors,
    ], 422);
}

Route::get('/users/{user}', function (Request $request, int $userId) {
    $user = User::findOrFail($userId);

    return response()->xml($user);
});

Route::get('/users/{user}', function (Request $request, int $userId) {
    $user = User::findOrFail($userId);

    return response()->xml(['user' => $user->toArray()]);
});

Route::get('/users/{user}', function (Request $request, int $userId) {
    $user = User::findOrFail($userId);

    return response()->xml($user, 200, [], ['root' => 'user']);
});

Route::get('/users', function () {
    $users = User::get();

    return response()->xml(['users' => $users->toArray()]);
});

protected $routeMiddleware = [
    // ...
    'xml' => \Bmatovu\LaravelXml\Http\Middleware\RequireXml::class,
];

Route::post('/users', function (Request, $request) {
    // do something...
})->middleware('xml');

Route::post('/users', function (Request, $request) {
    // do something...
})->middleware('xml:1');

Xml::encode(['key' => 'value']);

xml_encode(['key' => 'value']);

Xml::decode('<?xml version="1.0" encoding="UTF-8"

xml_decode('<?xml version="1.0" encoding="UTF-8"