Home  >  Article  >  Backend Development  >  How to cancel the port number in PHP (solution analysis)

How to cancel the port number in PHP (solution analysis)

PHPz
PHPzOriginal
2023-04-06 09:14:50624browse

With the continuous development of Internet technology, our website front-end and back-end are becoming more and more complex. During this process, many problems may arise. For example, we want to cancel the port number when using PHP to access the website, which will be a troublesome problem. But don’t worry, we have a solution for this problem.

First, let us understand the role of port numbers. A port number is an endpoint identifier formed by a paired set of communications in the TCP/IP protocol and is used to identify a specific service in a process or application. A port number is usually a 16-bit integer, ranging from 0 to 65535.

When we enter the URL in the browser, port 80 will be used by default. For example, if you enter http://www.test.com, your browser will default to port 80 and access http://www.test.com:80 directly. Every website has a default port number. Usually, the HTTP protocol uses port 80 and the HTTPS protocol uses port 443.

In the PHP program, if we want to use cURL to access the website, and the website uses a non-standard port number, for example, when accessing GitHub, its non-standard port number is 443, then we need to change the The port number is set to 443. Otherwise, our access will be redirected to the default port of port 80.

So how to cancel the port number in PHP? Here are a few possible solutions.

Option 1: Use the gethostbyname function

When using cURL to access the website, we usually use the URL in the code, for example:

$url = "https://github.com";
$ch = curl_init($url);

If we want to cancel the port number , we can use the gethostbyname function to convert the URL to an IP address:

$url = "https://github.com";
$ip = gethostbyname(parse_url($url, PHP_URL_HOST));
$ch = curl_init("https://$ip");

Use the gethostbyname function to convert the URL to an IP address, thereby canceling the port number. However, this function may fail in some environments, such as when IPv6 addresses are used.

Option 2: Use the stream_context_create function

We can set context parameters in the PHP program through the stream_context_create function. We can cancel the port number by modifying the context parameters.

The following is a sample code:

$url = "https://github.com";
$options = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true,
        'peer_name' => parse_url($url, PHP_URL_HOST),
        'peer' => parse_url($url, PHP_URL_HOST) . ':443'
    )
);
$context = stream_context_create($options);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 2000);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 2000);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, fopen('php://output', 'w'));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);

Use the stream_context_create function to create context parameters, set the peer parameter to the host name and port number of the website, and then cancel the port number.

Summary

There are many ways to cancel the port number in PHP. Here are only two common methods. In actual use, the appropriate solution can be selected according to the specific situation.

It is recommended that when using cURL to access the website, set the verify_peer and verify_peer_name parameters to ensure security. If you are using an older version of PHP, it is recommended to consider upgrading your PHP version for better security and stability.

The above is the detailed content of How to cancel the port number in PHP (solution analysis). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn