In php, curl implements GET and POST requests to Capture webpages or upload files

Source: Internet
Author: User
The strength of the curl function is that you can access a webpage like a user and it can be all the data submission modes accepted by php, the following is an example of a php curl that implements GET and POST requests to Capture webpages or upload files. It also supports cross... the strength of the curl function is that you can access a webpage like a user and it can be all the data submission modes accepted by php, the following is an example of using curl in php to Capture webpages or upload files using GET and POST requests. It also supports cross-project and cross-server access.

I. curl functions and parameters

Function library:

1: curl_init initializes a curl session

2: curl_close closes a curl session

3: curl_setopt sets session parameters for a curl

4: curl_error returns a string containing the current session error message.

5: curl_exec executes a curl session

6: curl_multi_add_handle adds a separate curl handle resource to the curl batch processing session

7: curl_multi_close closes a batch processing handle resource

8: curl_multi_exec parses a curl batch handle

9: curl_multi_getcontent returns the obtained output text stream

10: curl_multi_info_read

11: curl_multi_init initializes a curl batch processing handle resource

12: curl_multi_remove_handle: remove a handle resource from the curl batch processing handle.

13: curl_multi_select blocking until there is an active connection in the cURL batch processing connection

14: curl_setopt_array sets session parameters for a curl in the form of an array

15: obtain curl-related version information using curl_version

16: curl_getinfo get the information of a curl connection resource handle

17: curl_copy_handle copies all content and parameters of a curl connection resource.

18: curl_errno returns a number containing the current session error message.

Curl_setopt common configurable parameters:

Url of the CURLOPT_URL request

CURLOPT_RETURNTRANSFER sets whether to retrieve data and return data in the form of a file stream instead of a direct output.

CURLOPT_POST sets whether to POST a request. the type is application/x-www-form-urlencoded, which is the same as form submission.

CURLOPT_POSTFIELDS POST request data

When CURLOPT_HEADER is enabled, the header file information is output as a data stream.

When CURLOPT_HTTPGET is enabled, the HTTP method is set to GET. the default value is GET.

II. curl GET method

 

III. POST method

$ Url = "http: // localhost/ceshi. php "; $ post_data = array (" username "=>" ceshi "," pwd "=>" sada & 1dsw1 "," key "=>" ha "); $ ch = curl_init (); // initialize curlcurl_setopt ($ ch, CURLOPT_URL, $ url); // Set the request address curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); // return value curl_setopt ($ ch, CURLOPT_POST, 1); // Set the request method POSTcurl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data ); // the requested variable data $ output = curl_exec ($ ch); // execute the command to obtain the returned data. we recommend json_encode ($ return_data); curl_close ($ ch ); $ output = json_decode ($ output); // Parse the returned data

IV. file upload using curl POST

$ Post_data = array ("Filedata" => "@". $ image_file); // use the array to pass the value. image_file is the image address and @ cannot be less. it indicates a file $ url = "http: // localhost/ceshi. php "; $ ch = curl_init (); // initialize curlcurl_setopt ($ ch, CURLOPT_URL, $ url); // Set the link curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true ); // Set whether the returned information curl_setopt ($ ch, CURLOPT_POST, true); // Set it to the POST method curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data ); // POST data $ result = curl_exec ($ ch); // receives the returned information if (curl_errno ($ ch ) {// If an error occurs, the error message die (json_encode ($ ch);} curl_close ($ ch) is displayed ); // Close the curl link if (ord ($ result [0]) == 239 & ord ($ result [1]) = 187 & ord ($ result [2]) = 191) {$ result = substr ($ result, 3 ); // solve the bug that the json_decode header is empty. the Bom header is fixed and can be removed after detection.} $ result = json_decode ($ result);?>

V. common problems and solutions for curl POST file upload:

1: curl_setopt sets CURLOPT_POSTFIELDS as an array, and the backend cannot get the value of $ _ POST.

Error:

If $ post_data is an array (including multi-dimensional arrays), an error message "entity is too large" is displayed. The receive. php that receives the data cannot obtain the data transmitted by curl.

Cause:

When the curl POST method is used, an array is passed to CURLOPT_POSTFIELDS. curl encodes the data into multipart/form-data. if a URL-encoded string is passed, the data will be encoded into application/x-www-form-urlencoded, the multipart/form-data encoding method is equivalent to directly operating on a form like "enctype =" multipart/form-data "method =" post ".

Solution:

A:

Connect the $ post_data array with a string encoded by urlencode,

Example: $ post_data = "& name = urlencode ($ name) & pwd = usrlencode ($ pwd )"

B:

Directly use http_build_query () to concatenate parameters.

Ps:

"Multipart/form-data" sets the form to MIME encoding, which is used to transmit binary files. if you want to upload files, this encoding is required (for example, see the curl POST file upload example). However, common url data uses the "application/x-www-form-urlencoded" format.

2: The response header of the curl request contains three more bytes. the backend of the post request is returned in the json_encode data format. after json_decode is executed, the return value is always empty.

Cause: bom header ghost, bom header: in Windows with notepad and other programs to save text files as UTF-8 format, notepad will add a few invisible characters (ef bb bf) before the file header, which is called BOM (Byte order Mark ), that is, if three bytes are added, the return value is null after json_decode.

Perform bom header check on returned values:

Echo substr ($ result, 0, 1); // you can see a garbled echo substr ($ result, 0, 2); // you can see two garbled echo substr ($ result, 0, 3); // blank echo substr ($ result, 0, 4); // See o

It turns out to be a problem with the bom header.

Solution:

If (ord ($ result [0]) = 239 & ord ($ result [1]) = 187 & ord ($ result [2]) = 191) {$ result = substr ($ result, 3); // The Bom header is fixed and can be removed after detection}


Tutorial link:

Reprint at will ~ However, please keep the tutorial address★

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.