url function in php introduction and use examples

  • 2021-01-06 00:29:29
  • OfStack

base64_encode - Encode the data using MIME base64
base64_encode() returns uses base64 to encode data. This encoding is designed so that binary data can be transmitted through a transport layer that is not pure 8-ES11en, such as the body of an E-mail message.
Base64-encoded data takes up about 33% more space than the original data.


<?php
$str = 'This is an encoded string';
// VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==
echo base64_encode($str);
?> 

base64_decode -- Decodes data encoded using MIME base64
base64_decode() decodes encoded_data and returns the original data. Failure to do so returns FALSE. The data returned may be in base 2.


<?php
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
// This is an encoded string
echo base64_decode($str);
?> 

get_headers - Gets all the headers sent by the server in response to a single HTTP request
get_headers() returns an array containing the headers sent by the server in response to an HTTP request. Failure to do so returns FALSE with an error message at E_WARNING level.
If the optional format parameter is set to 1, get_headers() parses the information and sets the array key.


<?php
$phpha1 = get_headers('https://www.ofstack.com');
$phpha2 = get_headers('https://www.ofstack.com', 1);
print_r($phpha1);
print_r($phpha2);
?>

The output is as follows:


Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Server: nginx/1.2.2
    [2] => Date: Tue, 06 Nov 2012 10:17:59 GMT
    [3] => Content-Type: text/html; charset=UTF-8
    [4] => Connection: close
    [5] => X-Powered-By: PHP/5.3.8
    [6] => X-Pingback: https://www.ofstack.com/xmlrpc.php
    [7] => Via: 10.67.15.26
    [8] => Set-Cookie: saeut=124.127.138.35.1352197078737175; path=/; max-age=311040000
)
Array
(
    [0] => HTTP/1.1 200 OK
    [Server] => nginx/1.2.2
    [Date] => Tue, 06 Nov 2012 10:17:59 GMT
    [Content-Type] => text/html; charset=UTF-8
    [Connection] => close
    [X-Powered-By] => PHP/5.3.8
    [X-Pingback] => https://www.ofstack.com/xmlrpc.php
    [Via] => 10.67.15.21
    [Set-Cookie] => saeut=124.127.138.35.1352197079055460; path=/; max-age=311040000
)

get_meta_tags -- Extract all meta tags and content attributes from a single file, returning an array
It is conceivable that some websites can easily use this function for site SEO information extraction.


<?php
// The end of the world PHP blog  https://www.ofstack.com
$phpha = get_meta_tags('https://www.ofstack.com');
print_r($phpha);
?>

The output is as follows:


Array
(
    [keywords] =>  The end of the world's blog ,PHP blog ,PHP Technology blog ,PHP Learning the blog ,PHP Development of the blog 
    [description] =>  The end of the world PHP Blog is PHP The main study blog, record PHPER The learning process, pay attention to the latest developments of the Internet. 
    [generator] => WordPress 3.2.1
)

http_build_query -- Generates the request string after URL-encode

< ?php
$url = array('c'= > 'blog', 'a'= > 'show', 'id'= > 10, 'hello', 'world');
// c=blog & a=show & id=10 & 0=hello & 1=world
echo http_build_query($url);
// c=blog & a=show & id=10 & phpha_0=hello & phpha_1=world
echo http_build_query($url, 'jb51_');
? >
[/code]

This function currently I use most of the place is to do various API, combined request url, very convenient.
As you can see, you can also specify prefixes for numeric index members within an array.

parse_url -- Parses URL to return its component parts
This function parses 1 URL and returns an associative array containing the various components that appear in URL. This function is not used to verify the validity of a given URL, but to break it down into the sections listed below. Incomplete URL is also accepted, and parse_url() will attempt to parse it as correctly as possible.


<?php
$url = 'http://tianya:ofstack.com@jb51.com/hello.php?id=10#nav';
print_r(parse_url($url));
?>
Array
(
    [scheme] => http
    [host] => phpha.com
    [user] => tianya
    [pass] => phphadotcom
    [path] => /hello.php
    [query] => id=10
    [fragment] => nav
)

rawurlencode - Encode URL according to RFC 1738
rawurldecode - Decodes the encoded URL string
urlencode -- Encodes URL strings
urldecode -- Decodes the encoded URL string


<?php
$url = 'https://www.ofstack.com tianya';
echo urlencode($url);
echo '<br />';
echo rawurlencode($url);
echo '<br />';
echo urldecode($url);
echo '<br />';
echo rawurldecode($url);
?>

The output is as follows:


http%3A%2F%2Fwww.ofstack.com+tianya
http%3A%2F%2Fwww.ofstack.com%20tianya

As you can see, the difference between urlencode and rawurlencode is:
urlencode() encodes the space as a plus sign (+), and rawurlencode() encodes the space as %20
urldecode() and rawurldecode() are the corresponding decoding functions.


Related articles: