Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2019-04-23 15:45:52

philwareham
Core designer
From: Farnham, Surrey, UK
Registered: 2009-06-11
Posts: 3,192
Website

Call a stored txp:variable inside some raw PHP

Hi again. Can someone please let me know how to call a stored <txp:variable> tag value inside some raw PHP (i.e. inside a <txp:php> container tag)? Cheers!

To replace the Textpattern tag within this…

curl_setopt($curl, CURLOPT_URL, 'https://api.github.com/graphql');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
curl_setopt($curl, CURLOPT_HTTPHEADER,
     array(
            'User-Agent: Textpattern CMS',
            'Content-Type: application/json;charset=utf-8',
            'Authorization: bearer <txp:variable name="github-api-key" />'
        )
    );

Offline

#2 2019-04-23 16:22:31

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 8,629
Website

Re: Call a stored txp:variable inside some raw PHP

This should do it:

$github_key = parse('<txp:variable name="github-api-key" />');
curl_setopt($curl, CURLOPT_HTTPHEADER,
     array(
            'User-Agent: Textpattern CMS',
            'Content-Type: application/json;charset=utf-8',
            'Authorization: bearer ' . $github_key
        )
    );

Or you should be able to use the parse() directly in the array if you prefer.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2019-04-23 17:20:24

philwareham
Core designer
From: Farnham, Surrey, UK
Registered: 2009-06-11
Posts: 3,192
Website

Re: Call a stored txp:variable inside some raw PHP

Thanks Stef! Here’s another puzzle…

As part of the API query, I want to grab two fragments from a custom field.

In the custom field is the full path of the GitHub repo, e.g. https://github.com/philwareham/hive-framework and I need to grab philwareham as organisation and hive-framework as repo for my query. How would one in php grab these fragments per article (obviously different URLs but all following same URL pattern)?

Offline

#4 2019-04-23 17:34:19

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 8,629
Website

Re: Call a stored txp:variable inside some raw PHP

One way is to use parse_url() to first grab the PHP_URL_PATH then split that at slash:

$parts = explode('/', parse_url($url, PHP_URL_PATH));

That will return three array components:

1) empty (nothing before first slash in path)
2) user name ($parts[1])
3) repo name ($parts[2])

Another way could be to use a regex directly, but it may be less robust.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#5 2019-04-23 17:39:45

philwareham
Core designer
From: Farnham, Surrey, UK
Registered: 2009-06-11
Posts: 3,192
Website

Re: Call a stored txp:variable inside some raw PHP

Great, thanks, I’ll try those code suggestions tomorrow and report back.

Offline

Board footer

Powered by FluxBB