4 Common methods for capturing network data in PHP, and php4 methods for capturing data _ PHP Tutorial

Source: Internet
Author: User
In PHP, four common methods are used to capture network data, and php4 is used to capture data. Four common methods for capturing network data in PHP, php4 for capturing data. in this section, the names are fsockopen, curl and file_get_contents, specifically, we will explore these three methods to input network data into PHP, four common methods for capturing network data, and php4 for capturing data.

This section is named fsockopen, curl and file_get_contents. Specifically, we will discuss these three methods to summarize network data input and output. I have talked a lot about fsockopen before. next I will start to transfer it to others. Here are some common methods for capturing network data.

1. use file_get_contents to get the content in get mode:

$url = 'http://localhost/test2.php';$html = file_get_contents($url);echo $html;

2. use fopen to open the url and get the content.

$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 obtain the url in post mode.

$ 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-urlencoded ', 'content' => $ data // 'timeout' => 60*60 // timeout (unit: s ))); $ url = "http: // localhost/test2.php"; $ context = stream_context_create ($ options); $ result = file_get_contents ($ url, false, $ context); echo $ result;

4. before using the curl library, you may need to check whether php. ini has enabled 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;

In this section, curl and file_get_contents are named fsockopen. Specifically, these three methods are used to input network data...

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.