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

Source: Internet
Author: User
Tags curl file upload ord urlencode

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


$ Ch = curl_init ();
// Set option parameters
Curl_setopt ($ ch, CURLOPT_URL, http://www.111cn.net );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // sets the returned data
Curl_setopt ($ ch, CURLOPT_HEADER, 0); // you can specify that the header is not executed.
$ Output = curl_exec ($ ch); // execute
Curl_close ($ ch); // release the curl handle
Var_dump ($ output );


III. POST method

$ Url = "http: // localhost/ceshi. php ";
$ Post_data = array ("username" = & gt; "ceshi", "pwd" = & gt; "sada & amp; 1dsw1", "key" = & gt; "ha ");
$ Ch = curl_init (); // initialize curl
Curl_setopt ($ ch, CURLOPT_URL, $ url); // you can specify the request address.
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // return value
Curl_setopt ($ ch, CURLOPT_POST, 1); // Set the request method POST
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data); // The requested variable data
$ Output = curl_exec ($ ch); // execute the following command to obtain the returned data: 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" = & gt; "@". $ image_file); // use an array to transmit values. image_file is the image address, and @ cannot be less. It indicates a file.
$ Url = "http: // localhost/ceshi. php ";
$ Ch = curl_init (); // initialize curl
Curl_setopt ($ ch, CURLOPT_URL, $ url); // Set the link
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // Sets whether to return information.
Curl_setopt ($ ch, CURLOPT_POST, true); // Set it to POST
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data); // POST data
$ Result = curl_exec ($ ch); // receives the returned information
If (curl_errno ($ ch) {// error message displayed
Die (json_encode ($ ch ));
}
Curl_close ($ ch); // Close the curl link
If (ord ($ result [0]) = 239 & amp; ord ($ result [1]) = 187 & amp; ord ($ result [2]) = 191 ){
$ Result = substr ($ result, 3); // solves the bug where json_decode in the Bom 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), the error message "entity is too large" appears. 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 as MIME encoding to transfer 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 string.
Echo substr ($ result, 0, 2); // you can see two garbled characters.
Echo substr ($ result, 0, 3); // blank
Echo substr ($ result, 0, 4); // view o
It turns out to be a problem with the bom header.

Solution:


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

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.