Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
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
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
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
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
Re: Call a stored txp:variable inside some raw PHP
Great, thanks, I’ll try those code suggestions tomorrow and report back.
Offline