heinkem0
(A. S.)
June 11, 2022, 2:39pm
1
Hi,
short information befor: im a new with typo3.
For my use case, i need access to the basic php $_POST and $_GET values over php or typoscript.
I was try to use a ext for the restapi function but in my taks both ext are not working… I try it with t3api and nnrestapi.
Now i will try it with my own small ext to create a simple rest api for post and get.
My Template TypoScript are this
# Default PAGE object:
page = PAGE
page.10 = TEXT
//page.10 < styles.content.get
page.20 = USER_INT
page.20 {
userFunc = Rest\Fheprotimb\Domain\Model\RestAPI->test
}
And my RestAPI php file are this
class RestAPI extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
//public $myVar = \TYPO3\CMS\Core\Utility\GeneralUtility
public function test(string $content, array $conf) //: string
{
$post;
try{
$post = \TYPO3\CMS\Core\Utility\GeneralUtility::_POST(); //_Post()
}
catch(Exception $error) {
$post['name'] = "error :" . $error->getMessage();
}
return "PostValue Name : " . $post['name'];
}
}
The php script works well, but the data from $_POST and $_GET are not available…
I hope i can fix this with the help from the typo3 community
krueml
(Chris Müller)
June 11, 2022, 3:18pm
2
You should consider using a middleware:
https://docs.typo3.org/m/typo3/reference-coreapi/11.5/en-us/ApiOverview/RequestHandling/Index.html
There you’re getting a PSR-7 request object which holds the information you need.
When you want to go the user function way, you can use the setter “setContentObjectRenderer” in your RestAPI class:
https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/11.4/Deprecation-94956-PublicCObj.html
The content object renderer has a getRequest() method, where you can get a PSR-7 request object with the information you need.
I assume you are using TYPO3 v11.
1 Like
heinkem0
(A. S.)
June 11, 2022, 7:12pm
3
Hi Chris,
i use the version 10.4 and i would like prefer the user function way.
I not understand the documentation about the $cObj. I add the function in my class and call them in my typoscript. But its not working, what i do wrong ?
typoscript
# Default PAGE object:
page = PAGE
page.10 = TEXT
//page.10 < styles.content.get
page.20 = USER_INT
page.20 {
userFunc = Rest\Fheprotimb\Domain\Model\RestAPI->callUserFunction
userFunc = Rest\Fheprotimb\Domain\Model\RestAPI->test
}
php class
class RestAPI extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
public $cObj;
public function setContentObjectRenderer(ContentObjectRenderer $cObj): void
{
$this->cObj = $cObj;
}
public function test(string $content, array $conf) //: string
{
$post;
try{
$post = \TYPO3\CMS\Core\Utility\GeneralUtility::_POST(); //_Post()
}
catch(Exception $error) {
$post['name'] = "error :" . $error->getMessage();
}
$returnValue = "OwnPHP: PostValue : ";
return "PostValue Name : " . $this->cObj; //$post['name'];
}
}
The error from site:
Oops, an error occurred!
Object of class TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer could not be converted to string
krueml
(Chris Müller)
June 11, 2022, 7:54pm
4
Sadly, the getRequest method is not available in ContentObjectRenderer in v10.4. And you can’t concetenate a string with an object which hasn’t a __toString() method, hence the error.
GeneralUtility::_POST() should work, are you sure, you have POST data available? The method is pretty straightforward and should return the global $_POST variable:
}
/**
* Returns the global $_POST array (or value from) normalized to contain un-escaped values.
*
* @param string $var Optional pointer to value in POST array (basically name of POST var)
* @return mixed If $var is set it returns the value of $_POST[$var]. If $var is NULL (default), returns $_POST itself.
* @see _GET()
* @see _GP()
*/
public static function _POST($var = null)
{
$value = $var === null ? $_POST : (empty($var) || !isset($_POST[$var]) ? null : $_POST[$var]);
// This is there for backwards-compatibility, in order to avoid NULL
if (isset($value) && !is_array($value)) {
$value = (string)$value;
}
return $value;
}
/*************************
Try a
var_dump($post);
die();
directly after your
$post = \TYPO3\CMS\Core\Utility\GeneralUtility::_POST();
Also, _POST() does not throw an exception, so the try/catch is superfluous.
1 Like
heinkem0
(A. S.)
June 11, 2022, 8:13pm
5
nice to know,
for the GeneralUtility::_POST() , this was my first try but every try the post request was empty.
But the function GeneralyUtility::_GET() is working.
GET request is working
curl -X GET “http://localhost/backend/pro-tim-b/?testdata=test&backdata=other ”
POST request not working
curl -X POST -H “Content-Type: application/json” -d ‘{“testdata”: “test”, “backdata”: “testdata”}’ http://localhost/backend/pro-tim-b/
im not sure, is the post request wrong or is something blocking from typo my post request ?
PHP Class
class RestAPI extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
public $cObj;
public function setContentObjectRenderer(ContentObjectRenderer $cObj): void
{
$this->cObj = $cObj;
}
public function test(string $content, array $conf) //: string
{
$post = \TYPO3\CMS\Core\Utility\GeneralUtility::_POST();
var_dump($post);
die();
return "PostValue Name : ";
}
}
krueml
(Chris Müller)
June 12, 2022, 7:14am
6
Now we are getting closer: JSON-encoded payload is not available through global _POST variable in PHP. You have to retrieve and process the body content yourself. Have a look here for a solution:
php, json, post
1 Like
heinkem0
(A. S.)
June 12, 2022, 7:24am
7
a warm thank you Chris for your support !
This fix my Post over JSON
PHP Class test(){
echo file_get_contents('php://input');
}
in compination of
die();
this script do what it do must