PHP uses curl to implement get and post submit data

Source: Internet
Author: User


Curl Implement get Submit data

The code is as follows Copy Code
Initializes a CURL object
$curl = Curl_init ();
Set the URL you want to crawl
curl_setopt ($curl, Curlopt_url, ' http://www.111cn.net ');
Set header, last argument is 0 indicates return value without header,1 representation with header
curl_setopt ($curl, Curlopt_header, 0);
Set the browser's specific header, optionally, if the target site is 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 only set user-agent, optional, if the target site is 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 ");
Sets the curl parameter to require the result to be saved to a string or output to the screen, 1 to save to a string
curl_setopt ($curl, Curlopt_returntransfer, 1);
When the page content is not needed, set to 1. Default is 0
curl_setopt ($ch, curlopt_nobody, 1);
Run Curl, request Web page, save in $data
$data = curl_exec ($curl);
Close URL Request
Curl_close ($curl);
Check for errors
When we compare, we use "= = False" instead of "= = false", because we have to distinguish between ' null output ' and ' Boolean false '
if ($output = = FALSE) {
echo "CURL Error:". Curl_error ($ch);
}
Can get information
$info = Curl_getinfo ($ch);
Echo ' Get '. $info [' url ']. ' Time consuming '. $info [' Total_time ']. ' Seconds ';
/* ... The following information is included in the returned array:
"url"//Resource Network Address
"Content_Type"//Content encoding
"Http_code"//http status code
Size of the "header_size"//header
Size of "request_size"//Request
"FILETIME"//File creation time
"Ssl_verify_result"//ssl validation results
"Redirect_count"//Jump Technology
"Total_time"/Total time consuming
"Namelookup_time"//dns query time consuming
"Connect_time"/Waiting for connection time consuming
"Pretransfer_time"//Pre-transport preparation time consuming
Size of "size_upload"//Uploading data
Size of "size_download"//Download Data
"Speed_download"//download speed
"Speed_upload"//upload speed
Length of "download_content_length"//download content
"Upload_content_length"//Length of uploaded content
"Starttransfer_time"//Time to start transmission
"Redirect_time"//redirect time consuming
*/

Curl Implementation Post submission data

Post implementation of HTTP

The code is as follows Copy Code

Extract data from the post
Extract ($_post);
Set POST variables
$url = ' http://www.111cn.net ';
$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);

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.