PHP uses curl to implement get and POST data submission. This article introduces how PHP uses curl to submit data through get and POST. For more information, see. Curl implements get to submit data code as follows: Copy the code. This article will introduce PHP to use curl to implement get and POST to submit data. For more information, see.
Curl implements get to submit data
The code is as follows: |
|
// Initialize a cURL object $ Curl = curl_init (); // Set the URL you want to capture Curl_setopt ($ curl, CURLOPT_URL, 'http: // www. bKjia. c0m '); // Set the header. the last parameter is 0, indicating that the returned value does not contain the header, and 1 indicates that the returned value contains the header. Curl_setopt ($ curl, CURLOPT_HEADER, 0 ); // Set the specific header of the browser. optional. if required Curl_setopt ($ ch, CURLOPT_HTTPHEADER, array ( "User-Agent: {'mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv: 1.9.1.6) Gecko/20091201 Firefox/3.5.6 (. net clr 3.5.30729 )'}", "Accept-Language: {en-us, en; q = 0.5 }" )); // Or set only the user-agent. optional. if required Curl_setopt ($ curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: 1.8.1.1) Gecko/20061204 Firefox/2.0.0.1 "); // Set the cURL parameter. The result must be saved to the string or output to the screen. 1 indicates that the result is saved to the string. Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 ); // When the page content is not required, set it to 1. the default value is 0. Curl_setopt ($ ch, CURLOPT_NOBODY, 1 ); // Run cURL, request webpage, save in $ data $ Data = curl_exec ($ curl ); // Close the URL request Curl_close ($ curl ); // Check for errors // During comparison, we use "= FALSE" instead of "= FALSE", because we need to distinguish 'null output' and 'Boolean false' If ($ output = FALSE ){ Echo "cURL Error:". curl_error ($ ch ); } // Obtain information $ Info = curl_getinfo ($ ch ); Echo 'get '. $ info ['URL'].' time consumed '. $ info ['total _ time']. 'second '; /*... The returned array contains the following information: "Url" // resource network address "Content_type" // content encoding "Http_code" // HTTP status code "Header_size" // header size "Request_size" // request size "Filetime" // file creation time "Ssl_verify_result" // SSL verification result "Redirect_count" // jump Technology "Total_time" // total time consumed "Namelookup_time" // DNS query time "Connect_time" // waiting for connection time "PRetransfer_time" // pre-transmission preparation time "Size_upload" // size of the uploaded data "Size_download" // size of the downloaded data "Speed_download" // download speed "Speed_upload" // upload speed "Download_content_length" // The length of the downloaded content "Upload_content_length" // length of the uploaded content "Starttransfer_time" // start time of transmission "Redirect_time" // time consumed by redirection */ |
Curl implements POST data submission
Http post implementation
The code is as follows: |
|
// Extract data from the post Extract ($ _ POST ); // Set POST variables $ Url = 'http: // www. bKjia. c0m '; $ Fields = array ( 'Lname' => urlencode ($ last_name ), 'Fname' => urlencode ($ first_name ), 'Title' => urlencode ($ title ), 'Company' => urlencode ($ institution ), 'Age' => urlencode ($ age ), 'Email '=> urlencode ($ email ), 'Phone' => urlencode ($ phone) ); // Url-ify the data for the POST Foreach ($ fields as $ key => $ value) {$ fields_string. = $ key. '='. $ value .'&';} Rtrim ($ fields_string ,'&'); // Open connection $ Ch = curl_init (); // Set the url, number of POST vars, POST data Curl_setopt ($ ch, CURLOPT_URL, $ url ); Curl_setopt ($ ch, CURLOPT_POST, count ($ fields )); Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields_string ); // Execute post $ Result = curl_exec ($ ch ); // Close connection Curl_close ($ ch ); |
Bytes. Curl implements the get data submission code as follows // first...