A little knowledge

Source: Internet
Author: User

On weekends and nights, listening to soft light music, I sorted out some of the latest PHP knowledge points.

 

The csdn editor is not very easy to use, so it won't be typographical. paste it directly :)

 

PHP to get the current date and time formatting method:

Use date () to implement

<? PHP echo $ Showtime = date ("Y-m-d h: I: s");?>

Display format: year-month-day hour: minute: Wonderful

Related time parameters:

A-"am" or "PM"

A-"am" or "PM"

D-a few days, two digits. If there are less than two digits, fill in the first zero. For example, "01" to "31"

D-the day of the week, with three English letters, for example, "fri"

F-month, full English name; for example: "January"

H-12 hours, for example, "01" to "12"

H-24 hours, for example, "00" to "23"

Hours in the G-12 hour format. Less than two hours do not fill in zero. For example: "1" to 12"

Hours in the G-24-hour format. Less than two hours do not fill in zero. For example: "0" to "23"

I-minutes; for example: "00" to "59"

J-a few days, two digits. If less than two digits are left blank, for example, "1" to "31"

L-the day of the week, full name in English; for example: "Friday"

M-month, two digits. If there are less than two digits, add zero in front, for example, "01" to "12"

N-month, two digits. If less than two digits are left blank, for example, "1" to "12"

M-month, with three English letters, for example, "Jan"

S-seconds; for example: "00" to "59"

The end of the S-character is followed by an English sequence, with two English letters, for example, "th", "nd"

T-specifies the number of days in a month, for example, "28" to "31"

U-total seconds

W-number of weeks, for example, "0" (Sunday) to "6" (Saturday)

Y-year, four digits, such as: "1999"

Y-year, two digits, for example, "99"

Z-the day of the year, for example, "0" to "365"

 

 

 

 

 

 

PHP double quotes, single quotes:

When characters such as/T/N are inserted in '', Echo only outputs the characters as strings without escaping them; when the preceding characters are inserted in "", they can be escaped.

 

 

Several Methods for PHP to call remote URLs

Example code 1: Use file_get_contents to get content in get Mode

$ Url = 'HTTP: // www.demo.com/interface ';

$ Return_val = file_get_contents ($ URL );

// Print_r ($ http_response_header );

Echo ($ return_val );

 

Example code 2: Use fopen to open a URL and get the content

$ Fp = fopen ($ URL, 'R ');

Printarr (stream_get_meta_data ($ FP ));

Printhr ();

While (! Feof ($ FP )){

$ Result. = fgets ($ FP, 1024 );

}

Echo "url body: $ result ";

Printhr ();

Fclose ($ FP );

 

Sample Code 3: Use the file_get_contents function to obtain the URL in post mode.

$ DATA = array ('foo' => 'bar ');

$ DATA = http_build_query ($ data );

$ Opts = array (

'Http' => array (

'Method' => 'post ',

'Header' => "Content-Type: Application/X-WWW-form-urlencoded/R/N ".

"Content-Length:". strlen ($ data). "/R/N ",

'Content' => $ data

),

);

$ Context = stream_context_create ($ opts );

$ Html = file_get_contents ('HTTP: // localhost/e/admin/test.html ', false, $ context );

Echo $ HTML;

 

Example code 4: Use the fsockopen function to open a URL and get complete data, including header and body

Function get_url ($ URL, $ cookie = false ){

$ Url = parse_url ($ URL );

$ Query = $ URL [path]. "? ". $ URL [query];

EC ("query:". $ query );

$ Fp = fsockopen ($ URL [host], $ URL [port]? $ URL [port]: 80, $ errno, $ errstr, 30 );

If (! $ FP ){

Return false;

} Else {

$ Request = "get $ query HTTP/1.1/R/N ";

$ Request. = "Host: $ URL [host]/R/N ";

$ Request. = "connection: Close/R/N ";

If ($ cookie) $ request. = "Cookie: $ Cookie/N ";

$ Request. = "/R/N ";

Fwrite ($ FP, $ request );

While (! @ Feof ($ FP )){

$ Result. = @ fgets ($ FP, 1024 );

}

Fclose ($ FP );

Return $ result;

}

}

// Obtain the HTML part of the URL and remove the header

Function geturlhtml ($ URL, $ cookie = false ){

$ Rowdata = get_url ($ URL, $ cookie );

If ($ rowdata ){

$ Body = stristr ($ rowdata, "/R/n/R/N ");

$ Body = substr ($ body, 4, strlen ($ body ));

Return $ body;

}

Return false;

}

 

Example code 5: Use the fsockopen function to open the URL and obtain the complete data in post mode, including the header and body

Function http_post ($ URL, $ data, $ cookie, $ referrer = ""){

// Parsing the given URL

$ Url_info = parse_url ($ URL );

// Building referrer

If ($ referrer = "") // if not given use this script as referrer

$ Referrer = "111 ";

// Making string from $ data

Foreach ($ data as $ key => $ value)

$ Values [] = "$ key =". urlencode ($ value );

$ Data_string = implode ("&", $ values );

// Find out which port is needed-if not given use standard (= 80)

If (! Isset ($ url_info ["Port"])

$ Url_info ["Port"] = 80;

// Building post-request:

$ Request. = "Post". $ url_info ["path"]. "HTTP/1.1/N ";

$ Request. = "Host:". $ url_info ["host"]. "/N ";

$ Request. = "Referer: $ Referer/N ";

$ Request. = "Content-Type: Application/X-WWW-form-urlencoded/N ";

$ Request. = "Content-Length:". strlen ($ data_string). "/N ";

$ Request. = "connection: Close/N ";

$ Request. = "Cookie: $ Cookie/N ";

$ Request. = "/N ";

$ Request. = $ data_string. "/N ";

$ Fp = fsockopen ($ url_info ["host"], $ url_info ["Port"]);

Fputs ($ FP, $ request );

While (! Feof ($ FP )){

$ Result. = fgets ($ FP, 1024 );

}

Fclose ($ FP );

Return $ result;

}

Printhr ();

 

Example code 6: Use the curl library. before using the curl library, you may need to check PHP. ini to see if the curl extension has been enabled.

About curl Library:

Http://curl.haxx.se/

Curl is a File Transfer tool using URL syntax. It supports FTP, ftps, HTTP htpps scp sftp tftp Telnet dict file and LDAP. Curl supports SSL certificates, http post, http put, and FTP uploads, kerberos, HTT-based upload, proxy, Cookie, user + password proof, file transfer recovery, HTTP Proxy channel and a large number of other useful techniques

Function printarr (array $ ARR)

{

Echo"

Row field count: ". Count ($ ARR )."

";

Foreach ($ arr as $ key => $ value)

{

Echo "$ key = $ Value

";

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.