_javascript techniques for several classic examples of curl in PHP

Source: Internet
Author: User

1, the basic steps of Curl Request:

(1) initialization
(2) Setting options, including URLs
(3) Execute and get the contents of the HTML document
(4) Release curl handle

Copy Code code as follows:

<?php
1. Initialization
$ch = Curl_init ();

2, set options, including URL
curl_setopt ($ch, Curlopt_url, "http://www.cnblogs.com/it-cen/");

Returns the information obtained by CURL_EXEC () as a file stream instead of directly outputting
curl_setopt ($ch, Curlopt_returntransfer, 1);

The header file's information is output as a data stream at startup
curl_setopt ($ch, Curlopt_header, 1);

3. Execute and get HTML document content
Curl_exec ($ch);

4. Release handle
Curl_close ($ch);

echo $ch;

?>

Note: The second step is the most important, namely the curl_setopt () function

We can add a section to check the wrong statement, we should pay attention to the use of "===false", which is to distinguish between null output and Boolean value false

Copy Code code as follows:

$output = curl_exec ($ch);

if ($output = = False) {
echo "CURL Error:". Curl_error ($ch);
}

The Curl_getinfo () function returns information that is relevant to the request after execution of curl, which is useful for debugging and scheduling errors:

Copy Code code as follows:

Curl_exec ($ch);
$info = Curl_getinfo ($ch);
Echo ' <pre> ';
Print_r ($info);
Echo ' </pre> ';

The returned data

Copy Code code as follows:

Array
(
[url] => http://www.cnblogs.com/it-cen/
[Content_Type] => text/html; Charset=utf-8
[Http_code] => 200
[Header_size] => 312
[Request_size] => 61
[FILETIME] =>-1
[Ssl_verify_result] => 0
[Redirect_count] => 0
[Total_time] => 0.172
[Namelookup_time] => 0.016
[Connect_time] => 0.063
[Pretransfer_time] => 0.063
[Size_upload] => 0
[Size_download] => 14658 <span style= "color: #ff0000;" >//Requested data size </span>
[Speed_download] => 85220
[Speed_upload] => 0
[Download_content_length] => 14658
[Upload_content_length] => 0
[Starttransfer_time] => 0.125
[Redirect_time] => 0
[Certinfo] => Array
(
)

[Redirect_url] =>
)

2, this information in debugging is very useful , for example, in the curl crawl, may be due to the network and other reasons, often a crawl data incomplete situation, which we can be obtained through the calculation of the data filesize, and then and Curl_getinfo () obtained by comparison, If the size is equal, it is assumed that the download is correct, otherwise try again.

Let's look at an example of grabbing a picture:

Copy Code code as follows:

<?php
Header ("Content-type:image/png");

1. Initialization
$ch = Curl_init ();

2, set options, including URL
curl_setopt ($ch,, Curlopt_url, "http://img04.taobaocdn.com/tfscom/TB1omaTHXXXXXajXVXXtKXbFXXX.png");
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_header, 1);

3. Execute and get content
$res = curl_exec ($ch);

Get information
$info = Curl_getinfo ($ch);

4. Releasing Resources
Curl_close ($ch);

File_put_contents ("D:/aa.png", $res);

$size = FileSize ("D:/aa.png");

if ($size!= $info [' size_download ']) {
echo "Download the data is incomplete, please download again";
} else {
echo "Download data complete";
}


?>

3. Send data by post method in Curl

Copy Code code as follows:

<?php
$ch = Curl_init ();

$data = Array (' name ' => ' Kelly ', ' Age ' =>27, ' sex ' =>1);

curl_setopt ($ch, Curlopt_url, "http://localhost.post.php");
curl_setopt ($ch, Curlopt_returntransfer, 1);

Set to post
curl_setopt ($ch, Curlopt_post, 1);
Add a post variable to the
curl_setopt ($ch, Curlopt_postfields, $data);

$res = curl_exec ($ch);

Curl_close ($ch);

Echo $res;


?>

This method can simulate the message, or can sit on the irrigation robot, the idea is the same

4, Upload files with curl

Copy Code code as follows:

<?php
Asking for uploaded data
$data = Array (' name ' => ' Beauty ', "Upload" => "@a.zip");

$ch = Curl_init ();

curl_setopt ($ch, Curlopt_url, "http://127.0.0.1/Socket/upload_file.php");
curl_setopt ($ch, Curlopt_returntransfer, 1);

curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_postfields, $data);

$res = curl_exec ($ch);

Curl_close ($ch);

Echo $res;

?>

Note: To send a file, precede the filename with the @ prefix and use the full path

5, Curl Set item

In fact, Curl has many configuration options, these are the curl of the soul, through the setopt () settings, the following summary of several more common and important configuration items, I hope that readers in the future to use the curl will have some help:


Curlopt_autoreferer: Automatically set the Referer in header when based on location: Redirect: Information

Curlopt_cookiesession: When enabled curl will tightly pass a sessioncookie, ignoring other cookies

Curlopt_header: The header file's information is output as a data stream

Curlopt_infilesize: Set the size of the upload file in bytes

Curlopt_maxconnects: Maximum number of connections allowed

Curlopt_maxredirs: Specifies the maximum number of HTTP redirects

Curlopt_cookie: Set the contents of the "COOKIE:" section of the HTTP request, multiple cookies with a semicolon followed by a semicolon with a space

Curlopt_postfields: All data is sent with the "POST" action in the HTTP protocol to send the file, preceded by the @ prefix with the filename and using the full path

.......

For more configuration items please refer to the PHP manual

The Curl feature is powerful, it is a general-purpose library, not PHP unique.

I hope the reader can gain by learning some of the classic curl examples of this blog post.

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.