PHP Network programming

Source: Internet
Author: User
(1) Access to other websites by accessing the file:

The URL of any Web site is actually a path to the page file, so we can open the file to open the Web page file.

We can then write the contents into our own files. In this way we can search for and save content from multiple sites that we are interested in to our own documents.

For example, the following methods can be implemented:

$url _list = File ("http://www.baidu.com/"), $demo _web = fopen ("demo_web.php", ' a '); foreach ($url _list as $one _list) {    fwrite ($demo _web, $one _list);} Fclose ($url _list);

(2) PHP parsing of URLs:

Basic components of a URL: protocol name (scheme), hostname (host), port number (port), file path (path), query parameters

PHP can use the Parse_url () function to parse the URL and pass the URL that needs to be parsed as an argument to the function, which changes the URL into an array.

The name of each key in the array and the corresponding meaning is the "basic ingredient" in the column above.

such as: $demo = Parse_url ("http://www.example.com/index.php");

$demo [' Host '] is "www.example.com" $demo [' path '] is index.php

(3) Phpsocket programming:

The basic mechanism of socket operation:

You can communicate by building a socket on both the server and the client.

Server-side: The server initializes the socket, then binds the port, then listens to the port, and finally calls accept to block the server-side socket, waiting for the client socket connection.

Client: First initialize a socket, then connect to the blocking server-side socket, if the connection is successful, the client-server connection is established.

The client sends a data request, the server receives and processes the request, then sends the response data to the client, the client reads the data, and finally closes the connection, so that the interaction ends.

In PHP, if you want to open a socket, you need to use the Fsockopen () function.

The parameters of the Fsockopen () function are: URL, port number, a variable that holds the error number, a variable that holds the error message string, and a timeout wait time. (only the first parameter is required).

This fsockopen () function is similar to the fopen () function, resulting in a file pointer, so you can also use a function such as fwrite () to manipulate the file.

After you have established a connection using fsockopen (), use the fwrite () function to write some data to the server, such as:

$fp = Fsockopen ("www.example.com", $errno, $errstr, +), if (! $fp) {    echo "Connection failed";} else{    $send = "head/http/1.1\r\n";    $send. = "host:www.example.com\r\n";    $send. = "connection:close\r\n\r\n";    Fwrite ($fp, $send);        $data = Fgets ($fp, +);  Retrieves 128 strings of HTTP headers    fclose ($fp);  Close        the socket//Use the EXPLODE function to break the $data from space to two strings, then assign the previous string to the $response variable by the list function and assign the following string to the $code string.    list ($response, $code) = Explode (", $data);}

About the above code:

1. The $send string written to $fp is actually the use of "a series of HTTP protocol headers to initiate the communication process", such as "head/http/1.1\r\n" where the head is the request type, of course, can also be requested with a get or post, and the back of the \r\ n is just to satisfy the format. (Similarly, the following two strings added to the $send are also HTTP protocol headers)

2, after sending the HTTP header through Fwrite, the $FP will return its own HTTP header, through the fgets () function to read 128 of the string, and assign the value to $data, this $data the string after the space is assigned to $code.

And this $code is the HTTP status code, only when the status code is 200, indicating that the URL is available, any other numeric status code indicates that there is a problem.

(4) Using Curl:

Curl's role is to "simulate browser requests," and we can refine our HTTP request functionality with curl, rather than just manually browsing a page.

PHP can call curl through shell_exec () and other functions, and even a Curl class library: Libcurl, the following steps:

1. Call Curl_init () to start a curl transaction with the URL you want to access as a parameter

$cur = Curl_init (' www.example.com ');

The function returns a pointer to the curl transaction, and the next thing we do is a pointer to the transaction.

2, next we want to call the curl_setopt () function more than once to set the option of the transaction (there are too many options to set, which is not listed here), the syntax is as follows:

curl_setopt ($curl, "configurable Options", value);

3. After setting all the options, use the curl_exec () function to execute the transaction and assign the returned data to a variable:

$result = curl_exec ($curl);

4. Finally, close the link:

Curl_close ($curl);

Here are a few common settings options:

1, tell curl, if an error occurs, directly stop the operation:

curl_setopt ($curl, curlopt_failonerror,1);

2. Set timeout time:

curl_setopt ($curl, curloapt_timeout,5);

3. Set Curl to submit data using the Post method:

curl_setopt ($curl, curloapt_post,1);

4, set the post submitted data:

curl_setopt ($curl, Curloapt_postfields, ' name= xiaoming &age=12 ');

(The Tip:curl tool can also transmit, receive cookies, process file uploads, work over SSL connections, process FTP files)

  • Related Article

    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.