4 common methods of grasping network data in PHP

Source: Internet
Author: User
Tags curl

The name of this section is Fsockopen,curl and file_get_contents, specifically to explore some of the three ways of network data input and output of some of the summaries. There has been a lot of talk about Fsockopen, and the following starts with the other. Here's a quick list of some common ways to crawl network data.

1. Get the content with file_get_contents:

?

1 2 3 $url = ' http://localhost/test2.php '; $html = file_get_contents ($url); Echo $html;

2. Open the URL with fopen to get the content

?

1 2 3 4 5 6 7 8 9 10 $url = ' http://localhost/test2.php '; $fp = fopen ($url, ' R '); Stream_get_meta_data ($FP); $result = '; while (!feof ($fp)) {$result. = fgets ($fp, 1024);} echo "URL body: $result"; Fclose ($FP);

3. Use the File_get_contents function to get the URL in post mode

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22-23 $data = Array (' foo ' => ' Bar '), ' baz ' => ' boom ', ' site ' => ' www.jb51.net ', ' name ' => ' Nowa Magic ');   $data = Http_build_query ($data); $postdata = Http_build_query ($data); $options = Array (' http ' => Array (' method ' => ' POST ', ' header ' => ' content-type:application/x-www-form-urlencod   Ed ', ' content ' => $data//' timeout ' => 60 * 60//Timeout (in units: s))); $url = "http://localhost/test2.php"; $context = Stream_context_create ($options);   $result = File_get_contents ($url, False, $context); echo $result;

4, use the Curl Library, before using the Curl Library, you may need to see if php.ini has opened the curl extension

?

$url = ' http://localhost/test2.php?site=jb51.net '; $ch = Curl_init (); $timeout = 5; curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout, $timeout); $file _contents = curl_exec ($ch); Curl_close ($ch); echo $file _contents;

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.