1

I'm attempting to fetch SEO meta for entries inside of an element API, roughly following the advice in Element API integration

Below is the exact code in my element-api.php:

<?php

use craft\elements\Entry;

return [
    'endpoints' => [
        'api.json' => function() {
            return [
                'elementType' => Entry::class,
                'criteria' => [],
                'transformer' => function(Entry $entry) {
                    $seo = Craft::$app->runAction('seomatic/meta-container/all-meta-containers', [
                        'uri' => $entry->uri,
                        'siteId' => null,
                        'asArray' => true,
                    ])->data;

                    return $seo;
                },
            ];
        },
    ]
];

If I hit this endpoint directly in a browser - e.g. http://my-url.test/api.json - it returns a response exactly as expected. However, if I try to hit the endpoint using an external GET requestion with Accept: application/json (I'm using https://paw.cloud, specifically), I get back an error exception of yii\\web\\ForbiddenHttpException

Is there something about runAction inside of an Element API call that isn't allowed? Is there something I need to do to allow this in outside requests?

1

Probably you need to turn on the Meta Container Endpoint Access option in SEOmatic -> Plugin Settings -> Endpoints

This is intentionally disabled by default, you need to enable it if you intend to use it.

3
  • Thanks Andrew, I ended up finding and implementing your other Stack Overflow post related to this before I saw your comment, but I'll try enabling this plugin setting and if it works I'll make yours as the correct answer. – cmal Feb 12 at 16:37
  • Enabling "Meta Container Endpoint Access" in the SEOmatic plugin settings did, indeed, do the trick. Are there any pros/cons to this approach vs. your approach in the linked stack overflow post? – cmal Feb 12 at 16:48
  • 1
    The way recommended in that other post saves you an additional and unneeded XHR so... I'd go with that. :) – andrew.welch Feb 13 at 19:55
1

I ended up using the pattern described in How to pull an entry's SEOmatic meta tags with Element API? which didn't suffer from the same issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.