Summary of php cross-server access methods

Source: Internet
Author: User

Summary of php cross-server access methods

This example summarizes the php cross-server access method. Share it with you for your reference. The specific analysis is as follows:

Recently, cross-server access has been encountered in the project. After several days of research, it is summarized as follows:

1. Use the file_get_contents Method

?

1

2

3

$ Host = 'url ';

$ RandomNumber = file_get_contents ($ host );

Echo $ randomNumber;

2. Use Curl

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

$ Host = 'url ';

$ Ch = curl_init ();

Curl_setopt ($ ch, CURLOPT_URL, $ host );

// Return results

Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );

Curl_setopt ($ ch, CURLOPT_HEADER, 0 );

// Use POST to submit

Curl_setopt ($ ch, CURLOPT_POST, 1 );

// POST Parameters

$ Str = array ('a = 1', 'B = 2', 'c = 3 ');

Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ str );

// Result

$ Res = curl_exec ($ ch );

Curl_close ($ ch );

Before using the curl library, you may need to check php. ini to see if the curl extension has been enabled.

3. Use fopen to open the url and get the content.

?

1

2

3

4

5

6

7

8

9

<? Php

$ Url = "http://www.bkjia.net /";

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

While (! Feof ($ fp )){

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

}

Echo "$ result ";

Fclose ($ fp );

?>

I hope this article will help you with php programming.

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.