DEV Community

Cover image for Backend Operations in PHP from 0 to Hero(Pt. 2 passing variables and user input)
Code_Jedi
Code_Jedi

Posted on • Edited on

Backend Operations in PHP from 0 to Hero(Pt. 2 passing variables and user input)

In the last tutorial, we made a PHP script that performs simple backend operations.

In part 2, we'll be making a PHP script that will perform customizable backend operations by getting user input and passing it to our backend script


Let's get started!

First, let's create a form that will accept a url and a tag parameter:

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Simple operations</title>
  </head>
  <body>
  <form method="post" action="results.php">
  url: <input type="text" name="url"><br>
  tag: <input type="text" name="tag"><br>
  <button type="submit" >Submit</button>
  </form>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Once these parameters are accepted, they will passed-on to results.php, here's how it will look like:

<?php
$url = $_POST["url"];
$tag = $_POST["tag"];
$output = exec("python3 op1.py $url $tag");
echo("<h1>$output</h1>");
?>
Enter fullscreen mode Exit fullscreen mode

What's going on here?

  • First, results.php defines the $url and $tag variables as the ones passed-on by the form we made earlier.
  • It then executes op1.py using the exec function and passes the $url and $tag variables to op1.py.
  • It finally echos the output of this exec function inside an <h1> tag.

Here's where the magic happens

results.php passes the $url and $tag variables to op1.py, executes the python script and returns it's output.

One big question you might have is:
"How is python able to work with PHP variables?"
What?

Answer: Using the sys library.
Let me show you:

import sys
import requests
from bs4 import BeautifulSoup

url=sys.argv[1]
response = requests.get(url)

soup = BeautifulSoup(response.text, 'html.parser')
element = soup.find('body').find(sys.argv[2])
print(element)
Enter fullscreen mode Exit fullscreen mode

This is a python script which scrapes webpages for certain HTML tags such as h1 div and p, and outputs the text contents within the first occurrence of the specified HTML tag within the webpage.

As you might remember, results.php passed first the $url variable, then the $tag variable, therefor, when we define the url variable in our python script, we call sys.argv[1] to in turn get the first variable that was passed by the PHP script, which is the $url variable.


Next, when defining the tag which we want to scrape for text contents within the specified URL's webpage, we call sys.argv[2] to get the second variable passed by results.php, which is the $tag variable.

The same can be done with Node.js like so:

results.php

<?php
$url = $_POST["url"];
$tag = $_POST["tag"];
$output = exec("url=$url tag=$tag node op1.js");
echo("<h1>$output</h1>");
?>
Enter fullscreen mode Exit fullscreen mode

op1.js

console.log('URL: ' + process.env.url + ' | tag: ' + process.env.tag)
Enter fullscreen mode Exit fullscreen mode

Result
Node.js output



Full code:

index.php

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Simple operations</title>
  </head>
  <body>
  <form method="post" action="results.php">
  url: <input type="text" name="url"><br>
  tag: <input type="text" name="tag"><br>
  <button type="submit" >Submit</button>
  </form>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

results.php

<?php
$url = $_POST["url"];
$tag = $_POST["tag"];
$output = exec("python3 op1.py $url $tag");
echo("<h1>$output</h1>");
?>
Enter fullscreen mode Exit fullscreen mode

op1.py

import sys
import requests
from bs4 import BeautifulSoup

url=sys.argv[1]
response = requests.get(url)

soup = BeautifulSoup(response.text, 'html.parser')
element = soup.find('body').find(sys.argv[2])
print(element)
Enter fullscreen mode Exit fullscreen mode

Now if you open a terminal in your project's directory, run php -S localhost:8000 and open the respective url in your browser, here's how our code should work:
index.php
These form parameters are passed on to results.php which the user is then redirected to
results.php then takes those parameters, passes them to op1.py, executes the respective python script and returns it's output inside an <h1> tag which in this case is "PHP tutorial".

results.php


If you were to change the tag parameter to p:
p parameter
op1.py will then scrape the first <p> tag within the specified URL:
p element


You can now experiment!

You can now implement this method of performing user customizable backend operations into your projects and PHP scripts when trying to perform backend operations.


Stick around for part 3 to learn about more complex, memory and time intensive backend operations in PHP!


Byeeee👋

Top comments (0)