Windows.  Viruses.  Notebooks.  Internet.  office.  Utilities.  Drivers

I decided to make myself a cheat sheet about the main methods of sending and receiving data from a web server using php and js. And so this note was born. In it, I reviewed the most commonly used methods of data exchange between a web client and a web server that are used by php developers.

The top favorites for sending data are php get request and php post request, followed by AJAX technology. So what are the differences between get and post requests.

  • get request is passed in the address bar (in the http header) and is visible to the user
  • post request is passed in the body of the document
  • the size of the get request is limited (maximum 276 characters)
  • get method doesn't transfer files
  • post method uploads files
php get request

To make a php get request, we first need to create html form with data entry field. In the form tag, we indicate which file on the server to access and what method to send. Also do not forget to set the field name attribute, this will be the name of the array cell GET $_GET["data"]; from which we will get the data into a file on the server. How to send get data through the form, I think it's clear, now let's try to pass it through address bar. index.php?data=1 if we type such a request into the address bar and send it, then in the file in index.php we will get the value 1 from $_GET["data"] . With this, I think everything is clear, now an example.

Task: there is an index.php file with a form for sending a get request to another file, for example get.php. When a get request is received, the get.php file must pass an array with data to index.php. In index.php the resulting array should be displayed.

Implementation. index.php file

//In the form, using the get method, send the value 1 through the hidden field Send a request

get.php file

php post request

The php post request is also passed through the form, only the attribute in the form tag will be method="post" . And we will receive data in the specified file from the post $_POST["search"] array and immediately give an example.

Task: send data from index.php using the POST method to the server to the get.php file and get it back, if the data sending field is empty, display a message about the empty field. Let's start the solution with index.php

Search query

Fill in the request field

Request entered:

Submit an inquiry

get.php file

AJAX Array Passing

Let's do the same task with ajax. In general, ajax is a technology related to javascript, but as part of the note on transferring data to the server, we will also touch on this technology. Of course ajax is a topic for a separate article. During the request, we will also display the loading indicator gif. We also have two files index.php and get.php. index.php file. Don't forget to include jquery. Pay attention, to hide the gif on the first page load, assign the style display: none to the box block

$(document).ready(function()( //hang an event on the submit button $(".submit").click(function()( $.ajax(( //how we will send data type: "GET", / /where we pass url: "get.php", //what data we pass data: (flag: 1), //event before sending ajax beforeSend: function()( //display GIF $(".box").show( ); ), //event after receiving the response, //get the array in data success: function(data)( //hide the GIF $(".box").hide(); var html = ""; //f- i JSON.parse translates json data into object var dt=JSON.parse(data); for (var i = 0; i

get.php file. We get data from the get array into the file and pass the array from php to javascript. What to convey php array in js, it needs to be converted to json format. For this we use php json_encode. After conversion, json is sent php means, and specifically we call echo.

The first method to make a PHP POST request is to use file_get_contents . The second method will use fread in combination with a couple of other functions. Both options use the stream_context_create function to fill in the required request header fields.

Code explanation

The $sPD variable contains the data to be sent. It must be in HTTP request string format, so some Special symbols must be encoded.

In both the file_get_contents function and the fread function, we have two new parameters. The first one is use_include_path . Since we are making an HTTP request, in both examples it will have false. When using true to read a local resource, the function will look for a file at include_path .

The second parameter, context , is populated with the return value of stream_context_create , which takes the value of the $aHTTP array.

Using file_get_contents to make POST requests

In PHP, to send a POST request using file_get_contents , you need to use stream_context_create to manually fill in the header fields and specify which "wrapper" will be used - in this case http:

$sURL = "http://brugbart.com/Examples/http-post.php"; // POST URL $sPD = "name=Jacob&bench=150"; // POST data $aHTTP = array("http" => // Wrapper to be used array("method" => "POST", // Request method // Request headers are set below "header" => "Content- type: application/x-www-form-urlencoded", "content" => $sPD)); $context = stream_context_create($aHTTP); $contents = file_get_contents($sURL, false, $context); echo $contents;

Using fread to make POST requests

You can use the fread function to make POST requests. The following example uses stream_context_create to compose the required HTTP request headers:

$sURL = "http://brugbart.com/Examples/http-post.php"; // POST URL $sPD = "name=Jacob&bench=150"; // POST data $aHTTP = array("http" => // Wrapper to be used array("method" => "POST", // Request Method // Request headers are set below "header" => "Content- type: application/x-www-form-urlencoded", "content" => $sPD)); $context = stream_context_create($aHTTP); $handle = fopen($sURL, "r", false, $context); $contents = ""; while (!feof($handle)) ( $contents .= fread($handle, 8192); ) fclose($handle); echo $contents;

Making GET requests with PHP

We will now turn our attention to using fread and file_get_contents to download content from the internet via HTTP and HTTPS . To use the methods described in this article, the fopen wrappers option must be enabled. To do this, in the php.ini file, you need to set the allow_url_fopen parameter to On .

PHP POST and GET requests are used to log in to websites, retrieve web page content, or check for new versions of applications. We will cover how to make simple HTTP requests.

Using fread to download or receive files over the internet

Remember that reading a web page is limited to the accessible part of the packet. So one should use the stream_get_contents function (similar to file_get_contents ) or while loop to read the content in smaller chunks until the end of the file is reached:

In this case, processing a PHP POST request, the last argument to the fread function is equal to the size of the fragment. It should generally not be greater than 8192 (8*1024 ).

I have a URL structure with a query string and a parameter called position ,

http://computerhelpwanted.com/jobs/?occupation=administrator&position=network+administrator

I also have a form select dropdown with the form select name position .

Select Occupation Administrator Select Position Network Administrator

When the user makes a selection, they send the option values ​​to the action attribute with select name="position" as the parameter to use in the query string.

My question is, how do I access the form select values ​​separately from the query string values?

I am using the _GET method to call the value from a query string parameter.

$position = isset($_GET["position"]) ? ($_GET["position"]) : "";

Obviously, this is getting the value from the URL structure, not the form element. Or maybe it is, not sure. But by testing it, I seem to have come to the conclusion that it is getting it by url, not by form.

How can I access the form select value when comparing in my PHP?

Refresh

I have a problem with the canonical url given in the header.

That is how it should be

the only difference is the - and + in the query string.

Not all of my query strings have +. Some have -. But I'm showing content on both URLs, whether it has - or +. Either way, both URLs get the same page content.

But since the canonical is dynamically generated from the URI and not from the value of the form element, there are 2 different canonical characters on both content pages.

Using _Get('value') retrieves the value from the query string instead of the form element. I know this because the value of the form element has a space between network administrator which gets urlencoded when the form submits as network+administrator . So if I can compare against the value of the form element I can set the correct canonical.

If you notice an error, select a piece of text and press Ctrl + Enter
SHARE: