I have an application running in different platforms (Windows, Linux) that is mainly executing a series of CURL using as a target a server where there is a PHP app that is processing the requests.
The idea is to substitute the PHP app with a YII based app (console?) but the big limitation is that there is no chance to change the target server and the receiving php name (of course I can play with alias).
Do you want to know how to run curl inside Yii?
or do you want to change your sendPing function in index.php to be run-able from cli ?
(example: $ yii send-ping or $ php yiic.php sendping )
from what I see on this request is it is a POST request to index.php protected behind basic/digest http auth
so you should just learn the basic of GETTING DATA FROM USERS
You already gave me a couple of leads but there is still something I am not getting so let me try to summarize splitting the CURL parameters into groups:
This should be https://www.myserver.com/backend/web/index.php
-F switch is the same as if you put an input-field
so -F max=32 in HTML is equal to <input name=max value=32 /> which mean you can handle it by simply using a Model or Yii::$app->request->post('max')
for example here is an action
public function actionIndex(){
$model = new MyModel();
//read everything and load it to model
if($model->load(Yii::$app->request->post(), '') && $model->validate()){
if($model->function == 'sendPing')
//run your sendping
}
//or read it one by one
$function = Yii::$app->request->post('function');//sendPing
$average = Yii::$app->request->post('avg');//27
}
with model:
<?php
class MyModel extends \yii\base\Model
{
public $function;
public $time;
public $loss;
public $min;
public $avg;
public $max;
public function rules()
{
return [
[['function','time','loss','min','avg','max'[,'required']
// define validation rules here
];
}
}
I can confirm the above modification is able to get the username and password sent by the curl command 'auth' => function ($username, $password) {error_log($username); error_log($password);
Now what?
I mainly need to process the following parameters (-F…) depending on the function.
Do I have to invoke and action in the behaviors? or what ?
Sorry but getting to that it became again confused
I think I have it now, some “heads” against the wall but working…
public function behaviors()
{
return [
'basicAuth' => [
'class' => \yii\filters\auth\HttpBasicAuth::className(),
'auth' => function ($username, $password) {
$pp = Yii::$app->request->post();
if ($pp) {
$agent = Agent::find()->where(['username' => $username])->one();
if ($agent) {
switch (true) {
case ($pp['function'] == 'get_settings') :
error_log($this->readSettings($agent));
return $this->readSettings($agent);
break;
}
}
} else {
return null;
}
},
],
];
}
There is only one issue which I am not yet able to understand…
If the device sends a “function request” named “get_settings” I can see that the SiteController private action readSettings is working correctly since the error_log is giving me back the right JSON string, but…
…the device that is doing the CURL request is NOT getting this “ANSWER”.
This is the result I can see in the error_log [{“target”:“example.com”,“speed_test”:“0”,“reboot_now”:“0”}]
which is exactly what the device is waiting for