How can i access to $_POST and $_GET from TypoScript/ USerFunc

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 :slight_smile:

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.

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

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:

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

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 : ";
        }
}

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:

1 Like

a warm thank you Chris for your support ! :slight_smile:

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