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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)
