Invalid Api key using RestServer - php

I am using the codeigniter rest server api library.
When I enter http://localhost/projects/myapi/key/index_put.php and hit enter gives me the following error:
<xml>
<status>0</status>
<error>Invalid API Key</error>
</xml>
When I give a dummy string in the url, like:
http://localhost/projects/myapi/key/index_put.php?X-API-KEY=asldfj9alsdjflja97979797997
I get the same problem. Any idea?
index_put.php:
public function index_put() {
// Build a new key
$key = self::_generate_key();
// If no key level provided, give them a rubbish one
$level = $this->put('level') ? $this->put('level') : 1;
$ignore_limits = $this->put('ignore_limits') ? $this->put('ignore_limits') : 1;
// Insert the new key
if (self::_insert_key($key, array('level' => $level, 'ignore_limits' => $ignore_limits))) {
$this->response(array('status' => 1, 'key' => $key), 201); // 201 = Created
} else {
$this->response(array('status' => 0, 'error' => 'Could not save the key.'), 500); // 500 = Internal Server Error
}
}

i faced the same issue .don't mention put/get/post in URL ,RestServer itself recognizes the request nature base on parameter you pass two step required to solve your problem .
1st Step :
http://localhost/projects/myapi/key/index_put.php
must change to :
http://localhost/projects/myapi/key/index.php
2nd step:
create an api kay using sha1(max 40 character) in the keys table (table structure show in config/rest.php file),
enter 1 in is_private_key field and ::1 in ip_address field.
create the record ,and check it again .

This question is old but for those who finds this, the answer is:
The library https://github.com/chriskacerguis/codeigniter-restserver when using PUT method, the API KEY should be in the put header variables as x-www-form-urlencoded type.
Use google chrome postman and fill like image below:

Related

How can I test DELETE api in Laravel?

How can I test RESTful api of DELETE in Laravel by using Codeception?
I use the following function:
public function authenticatedUserSuccessDeleteEmployee(ApiTester $I)
{
$I->wantToTest('authenticated super user success delete employee');
// set header authorization
$I->amBearerAuthenticated($this->token);
//
$this->employee = factory(\App\Models\Employee::class)->create([
'id' => '20200100000000'
]);
// see database row is containing our expected data
$I->seeRecord('employees', ['id' => '20200100000000']);
// Send delete request
$I->sendDELETE('employees', array('id' => '20200100000000'));
// check expected response code is 200 OK
$I->seeResponseCodeIs(200);
}
But the employee does not created in the DB!
How can I create an object for testing delete API?
I found the problem. I need to send the id as part of the url!
$I->sendDELETE('employees/20200100000000');

PHP / Laravel Firebase Realtime Database updating a specific field

I'm trying to update firebase realtime database using kreait/firebase-php package.
Here's my tree:
I need to update the child of /detail/pesan/1/<unique ID> which have is_read value of false and update it to true.
Here's part of my code:
$database = $firebase->getDatabase();
$id_pesan=1;
/*
Update Firebase is_read
*/
$update = ['/is_read' => "true"];
$updatePesan_1 = $database
->getReference('chat/detail/'. $id_pesan)
->orderByChild('is_read')
->equalTo('false')
->update($update);
But i get this error :
Call to undefined method Kreait\Firebase\Database\Query::update()
when I change it to get the value first then update / set it :
$updatePesan_1 = $database
->getReference('chat/detail/'. $id_pesan)
->orderByChild('is_read')
->equalTo('false')
->getValue()
->set($update);
I get this error :
{"status":"error","error":400,"message":"Index not defined, add \".indexOn\": \"is_read\", for path \"/chat/detail/1\", to the rules"}
What I'm trying to do is filter / query database first to find child of specific tree which have is_read = false and update the value from "false" to "true".
Is it possible to do query of firebase database then updating it?
If yes how do I fix my code to achieve that?
Here's my solution
Okay so after many research and trial and error.
I did a couple of things :
Change the new rule to enable editing specific field on firebase database
Change the PHP code since I am unable to do filter / query then update. so I had to do it without filter / query and then updating data
1. Changes to Firebase database Rule
I change the rule to this :
{
"rules": {
".read": true,
".write": true,
"chat": {
"detail": {
"$id_pesan" :{
".indexOn": ["is_read"]
}
}
}
}
}
Some short explaination :
The data is stored in chat/detail///
since I need to be able to change what's inside I added ".indexOn" : ["is_read"] I change the rule and put indexing inside chat/detail/.
2. Change PHP code
At first what I want to do is this :
Initialize Firebase
Set references
Do Update call while query-ing field (in 1 call)
But it seems the package doesn't support what I wanted to do. so instead what I had to do is I have to query the field and get the unique ID value first. Then I made it into an Associative Array, then use it to update Database
here's the new code
//initialize
$database = $firebase->getDatabase();
//get value of all the unique ID
$getValue = $database
->getReference('chat/detail/'. $id_pesan)
->orderByChild('is_read')
->equalTo('false')
->getValue();
//initialize Array
$update = [];
//make the array necessary to update the firebase database
/*
What the Array should looks like
<Unique ID>/<variable I wanted to change> => "<new Value>"
Example :
"-IUAYS678/is_read" => "true"
"-JVHBY817/is_read" => "true"
*/
foreach($updatePesan_1 as $k => $v)
{
$update[$k. "/is_read"]="true";
}
//Update the Firebase Database
$updatePesan_1_process=$database->getReference('chat/detail/'. $id_pesan)
->update($update);
I hope this helps

Active Collab API: How to get projects

I'm trying out the ActiveCollab API for my first time. I had to use StackOveflow to figure out how to get the API token since the docs don't tell me this.
Below is my code:
/* GET INTENT */
$url = 'https://my.activecollab.com/api/v1/external/login';
$fields = array(
'email' => "email#email.com",
'password' => "****"
);
$intent = curl_post_connector($url, $fields);
$intent = $intent->user->intent;
/* GET TOKEN */
$url = 'https://app.activecollab.com/my_app_id/api/v1/issue-token-intent';
$fields = array(
'intent' => $intent,
'client_name' => 'My App Name',
'client_vendor' => 'My Company Name'
);
$token = curl_post_connector($url, $fields);
$token = $token->token;
Everything above works and get's the token properly. What I find really weird is that I have to use API v1 to get this, and the docs on ActiveCollab's site don't mention any URL for API v5. It seems like this is the approach everything is taking here on StackOverflow.
Now with the token, I try to get my list of projects:
/* GET PROJECT */
$url = 'https://app.activecollab.com/my_app_id/api/v1/users';
$headers = array (
"X-Angie-AuthApiToken" => $token
);
$projects = curl_get_connector($url, $headers);
var_dump($projects);
But this does not work. There is no error returned - it instead returns an array of languages for some reason! I don't want to paste the massive json object here, so instead I'll link you to a photo of it: https://www.screencast.com/t/7p5JuFB4Gu
UPDATE:
When attempting to use the SDK, it works up until I try getting the token (which is just as far as I got without the SDK). I'm getting Server Error 500, and when looking at the logs, it says:
/home/working/public_html/ac/index.php(21): ActiveCollab\SDK\Authenticator\Cloud->issueToken(123456789)
#1 {main}
thrown in /home/working/public_html/ac/SDK/Authenticator/Cloud.php on line 115
This is line 115 of Cloud.php:
throw new InvalidArgumentException("Account #{$account_id} not loaded");
I honestly don't think I did anything wrong... there must be something wrong with my account ID.
Just for kicks, I commented out that line, and the error disappears and the page loads fine - except now I have no token...

ajaxform with yii cannot find data , give me an error

I want to do an edit of some data via a pop edit window with save and delete. It worked previously with other data ( means other table) just fine.
Now I am followiing problem.
This table has two primary keys. One is 'id' and other one is 'text'.
public function actionAjaxEditForm($id) {
$data = products::model()->findbyPk($id);
echo CJSON::encode(array('data' => $data));
Yii::app()->end();}
the error ist a 500 internal error
I tried with
findbyPk(array($id => 'id', $text = 'text'));
I thought I will link the 2 primary keys..but I think the problem lies somewhere else..and I dont know where. Thanks!
update :
with
find(array($id = 'id'));
gives me different error:
CExeption:
Attribute CDbCriteria.0 is not defined

WHMCS: How to get the current client in addon module clientarea page?

Given that I have a WHMCS addon that I call 'my_addon'. I created the main addon file 'my_addon.php' which does contain nothing than:
<?php
function my_addon_clientarea($vars) {
$client = null;
return array(
'pagetitle' => 'My Addon',
'breadcrumb' => array('index.php?m=my_addon'=>'My Addon'),
'templatefile' => 'views/myaddon_view',
'vars' => array(
'client' => $client
)
);
}
This does basically work. It does give me my template file, everything is passed through. My question is: How do I get the currently logged in client from within that function?
I didn't find any API method and I can't see any constant which does hold this information.
There must be a way to get the current client within the clientarea? Thanks for your help!
For those who do come after me and have the same problem: it's easy to solve. Turned out, that I just had to think it through... I found the client id to be available in the $_SESSION-variable.
So, if you are looking for the client's id:
<?php
function my_addon_clientarea($vars) {
$clientid = $_SESSION['uid'];
// And so on...
}
The official way to get current user information is:
$currentUser = new \WHMCS\Authentication\CurrentUser;
$user = $currentUser->user();
You can find more information here

Categories