PHP code example of jolicode / structured-data

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

    

jolicode / structured-data example snippets


use JoliCode\StructuredData\Validator;

$validator = new Validator();
$audit = $validator->audit($html); // the document itself, never a URL or a file path

$audit->isValid();      // no errors
$audit->isFullyValid(); // no errors, no warnings

foreach ($audit->getDiagnostic() as $message) {
    echo $message;
    // [Google warning] DataFeed.dataFeedElement.workExample: Missing recommended property: "sameAs" for the type "Book"
}

use JoliCode\StructuredData\Vocabularies\Validators\Google\GoogleValidator;

$validator->setValidator(GoogleValidator::class);               // run a single validator
$validator->audit($html, mergeNodesSharingAnIdentifier: false); // audit blocks sharing an @id separately
$validator->audit($html, reportPendingVocabularyUsage: true);   // warn on pending.schema.org terms

use JoliCode\StructuredData\JsonLd\Algorithms\Expand\Expander;

$expanded = (new Expander())->expand('{"@context": "https://schema.org", "@type": "Person", "name": "John Doe"}');

$policy = RemoteContextPolicy::allowHosts('www.w3.org', 'json-ld.org');
$validator = new Validator(documentLoader: new HttpDocumentLoader($policy, $httpClient));