Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic values in inline JSON-LD script block

I have data stored in a DB that I select and echo on my html page like so:

 <article id="content_left_article_1">
      <h1 class="main-heading">Get in Touch</h1>
      <P><?php echo $rs_contactRows['ContactJobTitle']; ?>: <?php echo $rs_contactRows['ContactName']; ?></P>
      <P>Email: <?php echo $rs_contactRows['ContactEmail']; ?></P>
      <P>Mobile: <?php echo $rs_contactRows['ContactTelephone']; ?></P>
 </article>

I know I can easily mark up data for this person with JSON-LD in a separate script block on the page with something like this:

<script type='application/ld+json'> 
    {
      "@context": "http://www.schema.org",
      "@type": "person",
      "name": "Brian Keet",
      "jobTitle": "Director",
      "url": "http://tekiahfoundation.blogspot.co.za/",
      "email": "[email protected]",
      "telephone": "+27766261024"
    }
    </script>

However, if the client were to update any of those details in the DB (which they most certainly will through the sites CMS), the static JSON-LD script block above will stay... well, static. I've been googling and stack-overflowing like mad trying to find an answer on how to have a dynamic value in a JSON-LD script block,

<script type='application/ld+json'> 
    {
      "@context": "http://www.schema.org",
      "@type": "person",
      "name": "$user.name + $user.surname",
    }
    </script>

or some other approach, that passes Googles structured data testing tools checks.

I've been trying to figure out how to work with a json_encoded($dataObject) which I get using the below mysqli_query() etc in php (see below code), and maybe create a JSON-LD script block with javascript but I'm not quite sure how this would work. I also know that apparently "Google can read JSON-LD data even when it is dynamically injected into the page's contents, such as by Javascript code or embedded 'widgets'". Also, if I echo the result of the json_encoded mysqli_fetch_assoc:

<?php
   require('inc-conncvnl.php');
   require('inc-function-escapestring.php');
   $sql_contact = sprintf("SELECT * FROM tblcontact");
   $rs_contact = mysqli_query($vconncvnl, $sql_contact);
   $rs_contactRows = mysqli_fetch_assoc($rs_contact);
   $contactData = json_encode($rs_contactRows);
  echo $contactData;
?>

I get the data echoing on page, like so:json-encode echo on webpage

I'm pretty sure I'm missing some big pieces of this puzzle. If anyone can help me figure out a successful approach to solving this I'd really appreciate it.

like image 318
slinky_bass Avatar asked Oct 07 '15 13:10

slinky_bass


2 Answers

You can either just output the JSON-LD in PHP as follows:

<script type="application/ld+json"> 
{
  "@context": "http://schema.org/",
  "@type": "Person",
  "name": "<?php echo htmlentities($rs_contactRows['ContactName'], ENT_QUOTES); ?>",
  "jobTitle": "<?php echo htmlentities($rs_contactRows['ContactJobTitle'], ENT_QUOTES); ?>",
  "url": "<?php echo htmlentities($rs_contactRows['ContactUrl'], ENT_QUOTES); ?>",
  "email": "<?php echo htmlentities($rs_contactRows['ContactEmail'], ENT_QUOTES); ?>",
  "telephone": "<?php echo htmlentities($rs_contactRows['ContactTelephone'], ENT_QUOTES); ?>"
}
</script>

or you create the JSON-LD in PHP and output that:

$data = array(
  '@context' => 'http://schema.org/',
  '@type' => 'Person',
  'name' => $rs_contactRows['ContactName'],
  'jobTitle' => $rs_contactRows['ContactJobTitle'],
  'url' => $rs_contactRows['ContactUrl'],
  'email' => $rs_contactRows['ContactEmail'],
  'telephone' => $rs_contactRows['ContactTelephone']
);
echo json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

Side note: You should always use htmlentities when outputting HTML as otherwise you risk XSS security vulnerabilities.

like image 94
Markus Lanthaler Avatar answered Dec 26 '22 11:12

Markus Lanthaler


Markus answer is right way to understand the basis. Next step could be to find and use a php library that generate LD-JSON for you.

For example, let's have a look at Torann json-ld library.

You could install it using Composer by updating composer.json : add the following dependency:

"require": { "torann/json-ld": "^0.0" }

Then in you page, you could generate ld-json this way (cf. Readme or Unit tests):

$context = \JsonLd\Context::create('news_article', [
    'headline' => 'Article headline',
    'description' => 'A most wonderful article',
    'mainEntityOfPage' => [
        'url' => 'https://google.com/article',
    ],
    'image' => [
        'url' => 'https://google.com/thumbnail1.jpg',
        'height' => 800,
        'width' => 800,
    ],
    'datePublished' => '2015-02-05T08:00:00+08:00',
    'dateModified' => '2015-02-05T09:20:00+08:00',
    'author' => [
        'name' => 'John Doe',
    ],
    'publisher' => [
        'name' => 'Google',
        'logo' => [
          'url' => 'https://google.com/logo.jpg',
          'width' => 600,
          'height' => 60,
        ]
    ],
]);

echo $context; // Will output the script tag

NB/ if Person context need to be improved, open an issue with your needs (I can fix it), or submit a pull request yourself.

like image 37
boly38 Avatar answered Dec 26 '22 13:12

boly38