jkphl / micrometa

A meta parser for extracting micro information out of web documents, currently supporting Microformats 1+2, HTML Microdata, RDFa Lite 1.1, JSON-LD and Link Types, written in PHP
http://micrometa.jkphl.is
MIT License
115 stars 39 forks source link

Parse ld+json wrong #65

Open valeriy-efimov opened 3 years ago

valeriy-efimov commented 3 years ago

Hi i have like this json

  {
    "@context": "https://schema.org/",
    "@type": "Product",
    "name": "Extra",
    "image": "https://www..jpg",
    "category": [
      "category",
    ],
    "description": "This Stun",
    "SKU": "11111",
    "Offers": {
      "@type": "Offer",
      "priceCurrency": "GBP",
      "price": "509.99",
      "itemcondition": "http://schema.org/NewCondition",
      "availability": "https://schema.org/PreOrder",
      "url": "https://www."
    },
  }

I try to get offers but immutableName wrong and i get error OutOfBoundsException| image

if i change manually Offers to offers all works image

change json i cant it external and use like this i think bad idea str_replace("Offers", "offers", "json")because json on HTML page

What else can I do?

rvanlaak commented 3 years ago

Can you provide us with a code snippet that demonstrates the problem?

valeriy-efimov commented 3 years ago

@rvanlaak

use Jkphl\Micrometa\Ports\Parser;
$html = "
  {
    "@context": "https://schema.org/",
    "@type": "Product",
    "name": "Extra",
    "image": "https://www..jpg",
    "category": [
      "category",
    ],
    "description": "This Stun",
    "SKU": "11111",
    "Offers": {
      "@type": "Offer",
      "priceCurrency": "GBP",
      "price": "509.99",
      "itemcondition": "http://schema.org/NewCondition",
      "availability": "https://schema.org/PreOrder",
      "url": "https://www."
    },
  }
"

$url = 'https://example.com';
$parser = new Parser();
        $parsed = $parser($url, $html);

              foreach ($parsed->getItems() as $item) {
            /** @var \Jkphl\Micrometa\Domain\Item\Iri[] $types */
            $types = $item->getType();
            foreach ($types as $type) {
                if ($type->name === 'Product') {

                   $offers = $this->getPropertyValue($item, 'offers'); 
                }
            }
        }  

Cant find offers because https://schema.org/offers lover case and parser not correct set immutableName and got error OutOfBoundsException