When you run a program from the console

Asked 2 years ago, Updated 2 years ago, 36 views

For example, when you want to do the PHP process, you open your browser and check the results every time.
I know it can be done with $php-file.php, but sometimes I can't check it unless I give you parameters such as get request, so I end up checking it in my browser.

Also, if you use a web framework such as ruby on rails, you will need to specify the controller's actions, so you will still need to request get.

How do you solve this?

ruby php ruby-on-rails

2022-09-29 22:50

4 Answers

PHP came up with two methods:The external library is unused.
Also, I have prepared and checked the appropriate file (test.php) below.

<?php

var_dump($_GET);

1.How to overwrite $_GET etc. with parse_str() from the console
Run the following command from the console:

$QUERY_STRING="xxx=yyy&zzz=aaa"php-r'parse_str(getenv("QUERY_STRING"),$_GET);include("test.php";';

The results are as follows:

array(2){
  'xxx' = >
  US>//#string(3) "yyyy"
  'zzz' = >
  US>//#string(3) "aaa"
}

Note:
https://stackoverflow.com/questions/3848901/how-to-pass-query-string-parameters-to-the-php-binary

2. How to use a built-in web server
Start the built-in web server where test.php is located.

$php-Slocalhost:8000

Access it in some way.Here's an appropriate example:

$php-r "echo file_get_contents('http://localhost:8000/test.php?foo=bar&baz=qux');"

The results are as follows:

<preclass='xdebug-var-dump'dir='ltr'>
<b>array</b><i>(size=2)</i>
  'foo'<font color='#888a85'>=>/small>string>/small>>>font color='#cc0000'>'bar'>/font>i>(=3/)
  'baz'<font color='#888a85'>=</font>small>string>/small>>>font color='#cc0000'>'qux'>>/font>i>>(=3)

On the console side where you started the built-in web server, you should see the following output:

[Fri Feb 609:54:28 2015]::1:51546[200]:/test.php?foo=bar&baz=qux

3.Other
External libraries such as Goutte allow for more advanced execution.
https://github.com/FriendsOfPHP/Goutte


2022-09-29 22:50

It depends on what you want to check, but if you can't because you've taken out the Get or Post parameters directly, it depends on them and you can take them as a sign to separate them into functions or classes.

Separating dependencies also increases the likelihood that another boot operation can be prepared and performed from the console.

(In short, it's like a single test, so it's better to leave it to the test framework as mentioned in other answers, but I'm going to talk about it when it's too much.)

In extreme cases, the following code requires GET parameters:

  • index.php
<?php
$name = htmlspecialchars($_GET['name']);
echo<<EOHTML
<html><head>/head>
<body>
{$name}!!!<br>
</body></html>
EOHTML;

(In order to express dependency, I tried to embed $_GET into the ear document, but I quit because it was too fragile.)

Separate this into two parts as follows:

  • index.php
<?php
require_once('template.php');
show_greeting_page($_GET['name']); //isset etc. are omitted for simplicity
  • template.php
<?php
function show_greeting_page($name){
    $name = htmlspecialchars($name);
    echo<<EOHTML
    <html><head>/head>
    <body>
    {$name}!!!<br>
    </body></html>
    EOHTML;
}

This eliminates the need for GET parameters, so if you want to check on the console, you can run it with the following files:

  • inspect_template.php
<?php
require_once('template.php');
show_greeting_page('hello<taro>');

Below is the running image.

[~/somedir/]$php-f inspect_template.php
    <html><head>/head>
    <body>
    Hello <taro>!!!
    </body>
    </html> [~/somedir/]$

The above is not something to call dependency separation, but I think there will be more ways to check if it is separated into parts that do not depend on the outside.


if you want to see where the Web framework relies on features and complex objects that simply cannot reproduce the state. You can rely on testing functions for the framework and tools to automatically operate the browser.


2022-09-29 22:50

Why can't I check from my browser?
I don't understand why you're trying to perform screen-side operations from the console.
I have been developing Rails for quite a long time, but I have never felt that need.

If you don't know that, I can't say anything, but if you want to "run the screen from a place other than your browser," Rails can "run it from the test."

For example, using RSpec as an example, you can simulate browser operations from Ruby by writing a type of test called feature spec.
When combined with the debugger, you can also check the contents of the variable during the test run.

Regarding feature specs, I have written this article on Qiita before, so please refer to it.

Introduction to RSpec that can be used ·No. 4 どんなAny browser operation is free! !Back-drawing Capybara Dictionary

If you say, "No, that's not what you want to do," please explain in more detail what you want to do and where you're having trouble.

PS

I rarely do it, but there is also a way to execute get and post requests from rails console.
Please refer to the following web article for more information.


2022-09-29 22:50

With rails, you can throw a request from the rails console.Examples of get and post.To retrieve the CSRF token, refer to English question "How do I generate a CSRF token from the Rails console?".

irb(main): 011:0>app.get'/items/new'


Started GET "/items/new" for 127.0.0.1 at 2015-02-0609:45:48 +0900
Processing by ItemsController # new as HTML
  Rendered items /_form.html.erb (2.2ms)
  Rendered items/new.html.erb with in layouts/application (4.6ms)
Completed 200 OK in 91ms (Views: 89.7ms | ActiveRecord: 0.0ms)
= > 200
irb(main): 012:0>authenticity_token=app.response.body.match(/<[^<]+authenticity_token[^>]+value="([^"]+) "[^>]+>/)[1]
=>"Xt0BiLJMRo3Y2Vpd4KVUK87xcK2UFDvHvFv9Dcf/G5EKSof4udaiwqYOH/3PRld+nStCp6Z5Sn+NA7+albNltw=="
irb(main): 013:0>app.post'/items', {'item[name]':'hoge', authenticity_token:authenticity_token}


Started POST "/items" for 127.0.0.1 at 2015-02-0609:46:07 +0900
Processing by ItemsController # create as HTML
  Parameters: {"item"=>{"name"=>"hoge"}, "authenticity_token"=>"Xt0BiLJMRo3Y2Vpd4KVUK87xcK2UFDvHvFv9Dcf/G5EKSof4udaiwqYOH/3PRld+nStCp6Z5Sn+NAwal7==NAwtwal}
   (0.2ms) begin transaction
  SQL (1.1ms) INSERT INTO "items" ("name", "created_at", "updated_at") VALUES(?,?) [["name", "hoge", "created_at", "2015-02-0600:46:07.998190", "updated_at", "2015-02-06 00:46:0790"]
   (6.9ms) commit transaction
Redirected to http://www.example.com/items/1
Completed 302 Found in 18 ms (ActiveRecord: 8.2 ms)
= > 302


2022-09-29 22:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.