Programster's Blog

Tutorials focusing on Linux, programming, and open-source

PHP - Install YAML Support

If you need to generate YAML files from PHP (perhaps for Docker Compose), then it is probably easiest to install the YAML Data Serialization PECL package. This tutorial will show you how to do that on Ubuntu.

Alternatively, if you do not wish to have to install any php extensions, one can simply install the Symfony YAML package through composer and use its methods instead.

Related Posts

Table Of Contents

Installation

Debian 12 With PHP 8.2

sudo apt update \
  && sudo install -y php8.2-yaml

Ubuntu 20.04 With PHP 8.0 {}

sudo apt update \
  && sudo  install php8.0-yaml

Ubuntu 16.04 With PHP 7.0

sudo apt-get install php7.0-dev php-pear libyaml-dev -y
sudo pecl install yaml-2.0.0 -y

Update the php.ini file for the cli

SEARCH=";   extension=msql.so"
REPLACE=";   extension=msql.so\nextension=yaml.so"
FILEPATH="/etc/php/7.0/cli/php.ini"
sudo sed -i "s?$SEARCH?$REPLACE?" $FILEPATH

This will configured your PHP CLI to work with YAML. If you need to add apache support then also execute the following:

# Update the php.ini file for apache
SEARCH=";   extension=msql.so"
REPLACE=";   extension=msql.so\nextension=yaml.so"
FILEPATH="/etc/php/7.0/apache2/php.ini"
sudo sed -i "s?$SEARCH?$REPLACE?" $FILEPATH

Ubuntu 14.04 With PHP 5

Run the following bash script:

sudo apt-get install php5-dev php-pear libyaml-dev -y
sudo pecl install yaml

# Update the php.ini file for the cli
SEARCH=";   extension=msql.so"
REPLACE=";   extension=msql.so\nextension=yaml.so"
FILEPATH="/etc/php5/cli/php.ini"
sudo sed -i "s?$SEARCH?$REPLACE?" $FILEPATH

This will configured your PHP CLI to work with YAML. If you need to add apache support then also execute the following:

# Update the php.ini file for apache
SEARCH=";   extension=msql.so"
REPLACE=";   extension=msql.so\nextension=yaml.so"
FILEPATH="/etc/php5/apache2/php.ini"
sudo sed -i "s?$SEARCH?$REPLACE?" $FILEPATH

Test

Now you should be able to execute the following PHP script from the CLI:

<?php
$customer = array(
    "id" => 123,
    "first_name" => "Programster",
    "last_name" => "Page"
);

$obj = array(
    "id"=> 34843,
    "date"=> "2001-01-23",
    "customer"=> $customer,
    "comments"=> "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338."
);

// generate a YAML representation of the invoice
$yaml = yaml_emit($obj);

print "yaml version is: " . PHP_EOL . $yaml . PHP_EOL;

The output of which should be:

yaml version is: 
---
id: 34843
date: "2001-01-23"
customer:
  id: 123
  first_name: Programster
  last_name: Page
comments: Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.
...

You should also be able to convert a YAML string back into a PHP data type with:

$parsed = yaml_parse($yaml);

Symfony YAML Package

Symfony have provided a package for generating and parsing YAML (Symfony documentation), that implements a selected subset of features defined in the YAML 1.2 version specification.

This package gives you a greater level of control, and resolves a word-wrap issue that I and others have experienced with the "native" PHP YAML functions.

composer require symfony/yaml
<?php

use Symfony\Component\Yaml\Yaml;

require_once(__DIR__ . '/vendor/autoload.php');

$customer = array(
    "id" => 123,
    "first_name" => "Programster",
    "last_name" => "Page"
);

$obj = array(
    "id"=> 34843,
    "date"=> "2001-01-23",
    "customer"=> $customer,
    "comments"=> "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338."
);

// generate a YAML representation of the invoice
$yaml = Yaml::dump(
    input: $obj,
    inline: 100, // use expanded arrays as much as possible.
    indent: 2,
    flags:Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE
);

print "yaml version is: " . PHP_EOL . $yaml . PHP_EOL;

That will output the following:

yaml version is: 
id: 34843
date: '2001-01-23'
customer:
  id: 123
  first_name: Programster
  last_name: Page
comments: 'Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.'

As you can see, it does not start the file with --- or end with .... We were also able to easily set the number of spaces used for indentation.

References

Last updated: 1st June 2024
First published: 16th August 2018

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch