CURL and YII - Migrate from PHP to YII

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).

Taking the following command as an example:

curl --cacert /etc/ssl/certs/ca-certificates.crt -m 30 -s -u user:password -o /dev/null -w %{http_code} --connect-timeout 15 -X POST https://www.myserver.com/folder/index.php -F function=sendPing -F time=2018-10-03 21:18:08 -F loss=4 -F min=23 -F avg=27 -F max=32

I need a lead to understand:

  1. What is the way to “read” such a request (app->request ???) and all the variable parameters
  2. How yii can process the certificate (never worked with yii this way!)

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 )

None of the two.

I cannot change the curl command that is executed by the remote devices, but I want YII to be able to understand those requests

so you mean replacing the https://www.myserver.com/folder/index.php

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

for the http authentication you can use https://www.yiiframework.com/doc/api/2.0/yii-filters-auth-httpbasicauth

as for the cacert is of no concern to yii because it should be the webserver responsibility

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:

:+1:t4:Following is managed by the webserver:

--cacert /etc/ssl/certs/ca-certificates.crt

:+1:t4:Following are just for curl:

-m 30
-s
-o /dev/null
--connect-timeout 15
-w %{http_code}

:+1:t4:Following can be managed by the link you gave me to change the authentication since I am not going to use the User entity (https://www.yiiframework.com/doc/api/2.0/yii-filters-auth-httpbasicauth)

-u user:password

:-1:t4:Following should be https://www.myserver.com/folder/backend/web/index.php ? or console?

-X POST https://www.myserver.com/folder/index.php

:-1:t4:Not clear yet how the back-end (if, it is the back-end) can read the following parameters

-F function=sendPing 
-F time=2018-10-03 21:18:08 
-F loss=4 
-F min=23 
-F avg=27 
-F max=32

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        
     ];    
 }
}

:pray:t4:
I will immediately put myself at work and try your suggestions.
Will keep this uptodate as soon as I have some results (or new questions…)

Here’s the results of my trials:

  1. I have been able to setup a new folder with a YII2 advanced template :+1:
  2. I was able to setup GII to create the model of the table that will be used to authenticate the device :+1:
  3. I modified the SiteController.php of the backend to accomplish what you suggested in this link https://www.yiiframework.com/doc/api/2.0/yii-filters-auth-httpbasicauth :+1:
  4. 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); :+1:
  5. Now what? :-1:

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

What am I missing here ?

Make sure you return an instance of IdentityInterface in case of successful authentication.

My previous assumption were wrong, so could not get to a point this whole thing was working.

Had to review the logic behind and came to the conclusion that this cannot be done since the request

https://www.myserver.com/folder/index.php

is not specifying a controller/action to be executed after the basic HTTP authentication

If I am wrong please help/reply!