Get extension key inside of post processor - php

How can I fetch the extension key in my post processor?
I tried it like this like suggested here
public function returnExtkey() {
return t3lib_div::camelCaseToLowerCaseUnderscored($this->extensionName);
}
However, I get:
Fatal error: Class 'MyCompany\MyExtension\PostProcess\t3lib_div' not found
I also tried to call it without the function camelCaseToLowerCaseUnderscored:
echo "EXTNAME = '".$this->extensionName."'";
But I get an empty string as result.
How can I solve this?

I suggest to ask the request object for the extension key:
$extName = $this->request->getControllerExtensionKey()
by the way: t3lib_div was replaced by \TYPO3\CMS\Core\Utility\GeneralUtility

Related

Argument value not being passed while creating class object in PHP

I am new to PHP and am facing the below issue. There are two files:
In the first PHP file I am writing the below code to create an object
$oNotas = new gcibjdnf($sFiltro, false,'apcconc.nm_apelido', $bBuscarPorChassi);
In the second file-gcibjdnf.php, I have the below code for the constructor
public function __construct($sFiltro = '', $bPaginar = false, $sOrdem= 'apcconc.nm_apelido, danfe', $bBuscarPorChassi = false) {
...}
However,
when I print $sOrdem from the gcibjdnf.php file, I am getting "apcconc.nm_apelido, danfe" as an output.
According to me it should print "apcconc.nm_apelido". But it is printing the default parameter instead of the value passed.
I am using PHP 5.6 version. Please let me know in case anyone has any idea on this.

{"status":false,"error":"Unknown method"} error CodeIgniter RESTful API

<?php
include(APPPATH.'/libraries/REST_Controller.php');
class Quiz extends REST_Controller{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function user_get()
{
$this->load->model('Quizmodel');
$data = $this->Quizmodel->getAll();
$this->response($data, 200);
}
function restclient()
{
$this->load->library('rest', array(
'server' => 'http://localhost/CodeIg/index.php/quiz/'
));
$userr = $this->rest->get('user','','json');
echo $userr;
}
}
?>
I am able to get JSON output if I type http://localhost/CodeIg/index.php/quiz/user in my browser, however if I type http://localhost/CodeIg/index.php/quiz/restclient it gives this error: {"status":false,"error":"Unknown method"}
I tried changing get to post but still the same error.
I referred this page https://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814 to do it.
You pinged me on GitHub, even though I haven't used or even thought about this code in at least 4 years.
https://github.com/chriskacerguis/codeigniter-restserver/blob/d19dc77f03521c7a725a4555407e1e4e7a85f6e1/application/libraries/REST_Controller.php#L680
This is where that error is being triggered. Throw a few breakpoints in there or var_dump()'s until you see what is causing the trouble.
You probably want to get off CodeIgniter though, and use something more actively maintained like SlimPHP or Lumen.
firstly I want as you have loaded rest api and created your controller quiz as an api to call , where you can only create your functions like user_get or restclient_get and access them the same manner you are doing.Just change you function name restclient to restclient_get then it will call instead it is even not running at this moment.

Site token with codeigniter rest.

So I am trying to interact with the https://www.gosquared.com api with documentation at:
https://www.gosquared.com/developer/api/now/v3/aggregateStats/
Now I am using codeigniter with REST and CURL I have the following code:
public function __construct()
{
parent::__construct();
$this->load->library('admin/curl');
$this->load->library('admin/rest');
$this->rest->initialize(array(
'server' => 'https://api.gosquared.com/now/v3',
));
$this->rest->api_key('0000000000000000');
$this->rest->site_token('FFF-000000-F');
}
public function index()
{
$result = $this->rest->get('aggregateStats');
var_dump($result);
}
So, this is interacting with the service like it should, except on top of an api key this service also requires a site_token in order to access my information.
With the code I have now $this->rest->site_token('FFF-000000-F'); I get this specific error when I load the page:
Fatal error: Call to undefined method REST::site_token()
If I remove the line of code causing that error, I get this as a result of var_dump($result); (because the site token is not getting passed!)
object(stdClass)#27 (2) { ["code"]=> int(4000) ["message"]=> string(162) "You must specify a site token. It looks like GSN-1234567-X and can be found in the developer section of your account area https://www.gosquared.com/home/developer" }
So my question is:
How do I pass an extra variable site_token into the rest library in codeigniter without receiving any error?
According to the documentation a valid url for interacting with the api would look something like the following (for what it's worth)
https://api.gosquared.com/now/v3/aggregateStats?api_key=0000000000000000&site_token=FFF-000000-F
By placing the api key and the site_key into an array within the actual request, I was able to make it work.
// Removed these lines of code:
$this->rest->api_key('0000000000000000');
$this->rest->site_token('FFF-000000-F');
// Replaced the $result = $this->rest->get('aggregateStats'); with the following:
$data['result'] = $this->rest->get('aggregateStats', array('api_key' => '0000000000000000','site_token' => 'FFF-000000-F'));
Just a side note, may consider placing the api key and the site token into a config file for easier updating in the future.

When I move code into a function, I get a Fatal error. How can I call code from function

There are two pieces to this code:
One that adds documents to an index to be searched, which works fine, and a crawl() function that is a web-crawler that gets the contents of a page, which also works fine.
But, I need to add a document from inside the crawl() function.
When I move the code that adds a document inside the crawl() function, I get a Fatal Error:
Fatal Error: call to member function addDocument() on a non-object.
I am wondering how I can access the member function addDocument() from inside the crawl function?
Right now, I have a working version where the crawl() function returns what it has crawled in the form of a variable and then the addDocument code, outside the crawl() function, also has access to the returned variable and adds the document to the index that way.
But, that only (logically) works when I am crawling one page or a page with no links to follow. As the function only returns when it is done and since it is recursive to follow a page's links, the only content it will return is the content of the last-page.
Where I need the content of each page to be added each as a new document in the index.
Here is the working code, described above, commented as much as I could: http://pastebin.com/5ngcucDp
and here is the non-working code where I try to move the addDocument() inside the crawl() function: http://pastebin.com/mUEwQJTG
If you have a solution that involves how to access the addDocument() function from inside the crawl() function, then please share.
Or if you have a solution that involves modifying the working code so that it returns the contents of each page it crawls instead of the last-page, please share.
If you have any solutions, please share as I am absolutely exhausted and have tried everything I know.
When moving code to a function, you are completely removing its ability to access variables in the same scope. In this case, you probably (not going to go looking through your off-site code) have something like $someObject = new myClass();, then are trying to access $someObject->addDocument() on it from within the function.
You need to pass $someObject as a parameter to the function, or you could use global $someObject inside the function, though it's not as good an idea.
You have specified that:
// The below line is where the error takes place.
$elasticaType->addDocument($document);
Is your error line. Now, PHP is trying to access a class linked to $elasticaType If you have a linked class then use:
$elasticaType = new ClassName();
If not then you should create a class:
class Name {
public function addDocument ($document){
//Add document code
return $somevar;
}
}
$elasticaType = new Name();
$elasticaType->addDocument($document);

CodeIgniter Twitter API (Elliot Haughin's) capturing any URL get parameters with "oauth_token" in it

Seems a little strange.
I am using CodeIgniter with Elliot Haughin's Twitter library. It's an excellent API by the way.
However, I autoload this library in "autoload.php" in the config folder and I noticed ANY URL that has "oauth_token" URL parameter is captured by this library.
For example, when I type
http://localhost/myapp/index.php/controller?oauth_token=1
Then it throws up an error
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: libraries/Tweet.php
Line Number: 205
I went through the library and found that the following constructor calling a method that checks the GET parameters.
class tweetOauth extends tweetConnection {
function __construct()
{
parent::__construct();
..
..
..
$this->_checkLogin();
}
and the method "_checkLogin()" does the following
private function _checkLogin()
{
if ( isset($_GET['oauth_token']) )
{
$this->_setAccessKey($_GET['oauth_token']);
$token = $this->_getAccessToken();
$token = $token->_result;
...
...
What would be the best way to fix this?
Why do you have oauth_token in the querystring if you're not checking for a valid oauth_token?
I'm assuming at some point you want to state that a oauth_token has been set and you're just using oauth_token=1 as the parameter?
The library is set to always test against oauth_token, and it's not really a feasible tweak to do it any other way if you're autoloading it. You'd need a whitelist/blacklist of controllers (and maybe methods) it runs on, which pretty much defeats the point of autoloading.
If REALLY need to use oauth_token=1, you could just change it to
if ( isset($_GET['oauth_token']) && $_GET['oauth_token']!==1)
If you were using more than one value for oauth_token (or if your worry is that you can trigger an error by appending oauth_token=X to a URL) you could try and use a regex instead, assuming that all oauth_tokens follow a pattern (32 characters long etc).
Alternatively you could also probably just exit/return false depending on what is returned in $token = $this->_getAccessToken(). Depends what happens elsewhere in the code. Looks like returning false should just work.

Categories